You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by GitBox <gi...@apache.org> on 2022/12/07 12:43:13 UTC

[GitHub] [nifi-minifi-cpp] adamdebreceni commented on a diff in pull request #1413: MINIFICPP-1917 - Generate flow config JSON schema

adamdebreceni commented on code in PR #1413:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1413#discussion_r1042158041


##########
minifi_main/JsonSchema.cpp:
##########
@@ -0,0 +1,436 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "JsonSchema.h"
+
+#include <string>
+#include <unordered_map>
+#include <vector>
+
+#include "agent/agent_version.h"
+#include "agent/build_description.h"
+#include "rapidjson/document.h"
+#include "rapidjson/prettywriter.h"
+#include "RemoteProcessorGroupPort.h"
+#include "utils/gsl.h"
+
+#include "range/v3/view/filter.hpp"
+#include "range/v3/view/transform.hpp"
+#include "range/v3/view/join.hpp"
+#include "range/v3/range/conversion.hpp"
+
+namespace org::apache::nifi::minifi::docs {
+
+static std::string escape(std::string str) {
+  utils::StringUtils::replaceAll(str, "\"", "\\\"");
+  utils::StringUtils::replaceAll(str, "\n", "\\n");
+  return str;
+}
+
+static std::string prettifyJson(const std::string& str) {
+  rapidjson::Document doc;
+  rapidjson::ParseResult res = doc.Parse(str.c_str(), str.length());
+  gsl_Assert(res);
+
+  rapidjson::StringBuffer buffer;
+
+  rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
+  doc.Accept(writer);
+
+  return std::string{buffer.GetString(), buffer.GetSize()};
+}
+
+void writePropertySchema(const core::Property& prop, std::ostream& out) {
+  out << "\"" << escape(prop.getName()) << "\" : {";
+  out << R"("description": ")" << escape(prop.getDescription()) << "\"";
+  if (const auto& values = prop.getAllowedValues(); !values.empty()) {
+    out << R"(, "enum": [)"
+        << (values
+            | ranges::views::transform([] (auto& val) {return '"' + escape(val.to_string()) + '"';})
+            | ranges::views::join(std::string{','})
+            | ranges::to<std::string>())
+        << "]";
+  }
+  if (const auto& def_value = prop.getDefaultValue(); !def_value.empty()) {
+    const auto& type = def_value.getTypeInfo();
+    // order is important as both DataSizeValue and TimePeriodValue's type_id is uint64_t
+    if (std::dynamic_pointer_cast<core::DataSizeValue>(def_value.getValue())
+        || std::dynamic_pointer_cast<core::TimePeriodValue>(def_value.getValue())) { // NOLINT(bugprone-branch-clone)
+      // special value types
+      out << R"(, "type": "string", "default": ")" << escape(def_value.to_string()) << "\"";
+    } else if (type == state::response::Value::INT_TYPE
+        || type == state::response::Value::INT64_TYPE
+        || type == state::response::Value::UINT32_TYPE
+        || type == state::response::Value::UINT64_TYPE) {
+      out << R"(, "type": "integer", "default": )" << static_cast<int64_t>(def_value);
+    } else if (type == state::response::Value::DOUBLE_TYPE) {
+      out << R"(, "type": "number", "default": )" << static_cast<double>(def_value);
+    } else if (type == state::response::Value::BOOL_TYPE) {
+      out << R"(, "type": "boolean", "default": )" << (static_cast<bool>(def_value) ? "true" : "false");
+    } else {
+      out << R"(, "type": "string", "default": ")" << escape(def_value.to_string()) << "\"";
+    }
+  } else {
+    // no default value, no type information, fallback to string
+    out << R"(, "type": "string")";
+  }
+  out << "}";  // property.getName()
+}
+
+template<typename PropertyContainer>
+void writeProperties(const PropertyContainer& props, bool supports_dynamic, std::ostream& out) {
+  out << R"("Properties": {)"
+        << R"("type": "object",)"
+        << R"("additionalProperties": )" << (supports_dynamic? "true" : "false") << ","
+        << R"("required": [)"
+        << (props
+            | ranges::views::filter([] (auto& prop) {return prop.getRequired() && prop.getDefaultValue().empty();})
+            | ranges::views::transform([] (auto& prop) {return '"' + escape(prop.getName()) + '"';})
+            | ranges::views::join(std::string{','})
+            | ranges::to<std::string>())
+        << "]";
+
+  out << R"(, "properties": {)";
+  for (size_t prop_idx = 0; prop_idx < props.size(); ++prop_idx) {
+    const auto& property = props[prop_idx];
+    if (prop_idx != 0) out << ",";
+    writePropertySchema(property, out);
+  }
+  out << "}";  // "properties"
+  out << "}";  // "Properties"
+}
+
+static std::string buildSchema(const std::unordered_map<std::string, std::string>& relationships, const std::string& processors, const std::string& controller_services) {
+  std::stringstream all_rels;
+  for (const auto& [name, rels] : relationships) {
+    all_rels << "\"relationships-" << escape(name) << "\": " << rels << ", ";
+  }
+
+  std::stringstream remote_port_props;
+  writeProperties(minifi::RemoteProcessorGroupPort::properties(), minifi::RemoteProcessorGroupPort::SupportsDynamicProperties, remote_port_props);
+
+  std::string process_group_properties = R"(
+    "Processors": {
+      "type": "array",
+      "items": {"$ref": "#/$defs/processor"}
+    },
+    "Connections": {
+      "type": "array",
+      "items": {"$ref": "#/$defs/connection"}
+    },
+    "Controller Services": {
+      "type": "array",
+      "items": {"$ref": "#/$defs/controller_service"}
+    },
+    "Remote Process Groups": {
+      "type": "array",
+      "items": {"$ref": "#/$defs/remote_process_group"}
+    },
+    "Process Groups": {
+      "type": "array",
+      "items": {"$ref": "#/$defs/simple_process_group"}
+    },
+    "Funnels": {
+      "type": "array",
+      "items": {"$ref": "#/$defs/funnel"}
+    }
+  )";
+
+  std::stringstream cron_pattern;
+  {
+    const char* all = "\\\\*";
+    const char* any = "\\\\?";
+    const char* increment = "(-?[0-9]+)";
+    const char* secs = "([0-5]?[0-9])";
+    const char* mins = "([0-5]?[0-9])";
+    const char* hours = "(1?[0-9]|2[0-3])";
+    const char* days = "([1-2]?[0-9]|3[0-1])";
+    const char* months = "([0-9]|1[0-2]|jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)";
+    const char* weekdays = "([0-7]|sun|mon|tue|wed|thu|fri|sat)";
+    const char* years = "([0-9]+)";
+
+    auto makeCommon = [&] (const char* pattern) {
+      std::stringstream common;
+      common << all << "|" << any
+        << "|" << pattern << "(," << pattern << ")*"
+        << "|" << pattern << "-" << pattern
+        << "|" << "(" << all << "|" << pattern << ")" << "/" << increment;
+      return std::move(common).str();
+    };
+
+    cron_pattern << "(?i)^"
+      << "(" << makeCommon(secs) << ")"
+      << " (" << makeCommon(mins) << ")"
+      << " (" << makeCommon(hours) << ")"
+      << " (" << makeCommon(days) << "|LW|L|L-" << days << "|" << days << "W" << ")"
+      << " (" << makeCommon(months) << ")"
+      << " (" << makeCommon(weekdays) << "|" << weekdays << "?L|" << weekdays << "#" << "[1-5]" << ")"
+      << "( (" << makeCommon(years) << "))?"
+      << "$";
+
+  }
+
+  return prettifyJson(R"(
+{
+  "$schema": "http://json-schema.org/draft-07/schema",

Review Comment:
   yes, good idea, added in [f939b06](https://github.com/apache/nifi-minifi-cpp/pull/1413/commits/f939b06657939ba54c325fbf462c4a04bc8286da)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org