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/10/06 14:51:17 UTC

[GitHub] [nifi-minifi-cpp] szaszm commented on a diff in pull request #1391: MINIFICPP-1846 - Json configuration support part 1

szaszm commented on code in PR #1391:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1391#discussion_r989134695


##########
libminifi/include/core/json/JsonNode.h:
##########
@@ -0,0 +1,248 @@
+/**
+ *
+ * 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.
+ */
+
+#pragma once
+
+#include <string>
+#include <utility>
+#include <memory>
+
+#include "core/flow/Node.h"
+#include "rapidjson/document.h"
+#include "utils/gsl.h"
+#include "utils/ValueCaster.h"
+
+namespace org::apache::nifi::minifi::core {
+
+class JsonNode : public flow::Node::NodeImpl {

Review Comment:
   We should separate the roles of tree node and tagged union. This class should only be a tree node, and not handle multiple types. Just return whatever we get, and convert it to std::variant somewhere, so we don't need to reinvent the tagged union (again).



##########
libminifi/include/core/json/JsonNode.h:
##########
@@ -0,0 +1,248 @@
+/**
+ *
+ * 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.
+ */
+
+#pragma once
+
+#include <string>
+#include <utility>
+#include <memory>
+
+#include "core/flow/Node.h"
+#include "rapidjson/document.h"
+#include "utils/gsl.h"
+#include "utils/ValueCaster.h"
+
+namespace org::apache::nifi::minifi::core {
+
+class JsonNode : public flow::Node::NodeImpl {
+ public:
+  explicit JsonNode(const rapidjson::Value* node): node_(node) {}
+
+  explicit operator bool() const override {
+    return node_ != nullptr;
+  }
+  bool isSequence() const override {
+    return node_ ? node_->IsArray() : false;
+  }
+  bool isMap() const override {
+    return node_ ? node_->IsObject() : false;
+  }
+  bool isNull() const override {
+    return node_ ? node_->IsNull() : false;
+  }
+
+  nonstd::expected<std::string, std::exception_ptr> getString() const override {
+    try {
+      if (!node_) {
+        throw std::runtime_error("Cannot get string of invalid json value");
+      }
+      if (!node_->IsString()) {
+        throw std::runtime_error("Cannot get string of non-string json value");
+      }
+      return std::string{node_->GetString(), node_->GetStringLength()};
+    } catch (...) {
+      return nonstd::make_unexpected(std::current_exception());

Review Comment:
   Why not error codes instead? I don't see the point of abusing exceptions as error codes that are harder to use.



##########
libminifi/include/core/yaml/YamlNode.h:
##########
@@ -0,0 +1,176 @@
+/**
+ *
+ * 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.
+ */
+
+#pragma once
+
+#include <memory>
+#include <string>
+#include <utility>
+
+#include "yaml-cpp/yaml.h"
+#include "core/flow/Node.h"
+#include "utils/gsl.h"
+
+
+namespace org::apache::nifi::minifi::core {
+
+class YamlNode : public flow::Node::NodeImpl {

Review Comment:
   Same here: separate tree node from tagged union. 
   https://en.wikipedia.org/wiki/Single-responsibility_principle



##########
libminifi/include/core/json/JsonNode.h:
##########
@@ -0,0 +1,248 @@
+/**
+ *
+ * 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.
+ */
+
+#pragma once
+
+#include <string>
+#include <utility>
+#include <memory>
+
+#include "core/flow/Node.h"
+#include "rapidjson/document.h"
+#include "utils/gsl.h"
+#include "utils/ValueCaster.h"
+
+namespace org::apache::nifi::minifi::core {
+
+class JsonNode : public flow::Node::NodeImpl {
+ public:
+  explicit JsonNode(const rapidjson::Value* node): node_(node) {}
+
+  explicit operator bool() const override {
+    return node_ != nullptr;
+  }
+  bool isSequence() const override {
+    return node_ ? node_->IsArray() : false;
+  }
+  bool isMap() const override {
+    return node_ ? node_->IsObject() : false;
+  }
+  bool isNull() const override {
+    return node_ ? node_->IsNull() : false;
+  }
+
+  nonstd::expected<std::string, std::exception_ptr> getString() const override {
+    try {
+      if (!node_) {
+        throw std::runtime_error("Cannot get string of invalid json value");
+      }
+      if (!node_->IsString()) {
+        throw std::runtime_error("Cannot get string of non-string json value");
+      }
+      return std::string{node_->GetString(), node_->GetStringLength()};
+    } catch (...) {
+      return nonstd::make_unexpected(std::current_exception());
+    }
+  }
+
+  nonstd::expected<int, std::exception_ptr> getInt() const override {
+    return getNumber<int>("int");
+  }
+  nonstd::expected<unsigned int, std::exception_ptr> getUInt() const override {
+    return getNumber<unsigned int>("unsigned int");
+  }
+  nonstd::expected<int64_t, std::exception_ptr> getInt64() const override {
+    return getNumber<int64_t>("int64_t");
+  }
+  nonstd::expected<uint64_t, std::exception_ptr> getUInt64() const override {
+    return getNumber<uint64_t>("uint64_t");
+  }
+
+  nonstd::expected<bool, std::exception_ptr> getBool() const override {
+    try {
+      if (!node_) {
+        throw std::runtime_error("Cannot get bool of invalid json value");
+      }
+      if (!node_->IsBool()) {
+        throw std::runtime_error("Cannot get bool of non-bool json value");
+      }
+      return node_->GetBool();
+    } catch (...) {
+      return nonstd::make_unexpected(std::current_exception());
+    }
+  }
+
+  std::string getDebugString() const override {
+    if (!node_) return "<invalid>";
+    if (node_->IsObject()) return "<Map>";
+    if (node_->IsArray()) return "<Array>";
+    if (node_->IsNull()) return "null";
+    if (node_->IsInt()) return std::to_string(node_->GetInt());
+    if (node_->IsUint()) return std::to_string(node_->GetUint());
+    if (node_->IsInt64()) return std::to_string(node_->GetInt64());
+    if (node_->IsUint64()) return std::to_string(node_->GetUint64());

Review Comment:
   I think we should represent these using a single type.



-- 
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