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


##########
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())) {
+      // 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()) << "\"";  // NOLINT(bugprone-branch-clone)
+    }
+  } else {
+    // no default value, no type information, fallback to string
+    out << R"(, "type": "string")";
+  }

Review Comment:
   for the clang tidy error
   ```suggestion
     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")";
     }
   ```



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to