szaszm commented on code in PR #1391: URL: https://github.com/apache/nifi-minifi-cpp/pull/1391#discussion_r991592543
########## 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: Just use int64_t to represent all integers in the config. I think JSON only has one number type, and that's floating point, but maybe having some special handling for integer types makes sense. But supporting 4 separate integer types probably adds more complexity than value. -- 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]
