From 54e57c38edd642988e0f7acaa18bd98f59ccf50d Mon Sep 17 00:00:00 2001 From: Daniel Parker Date: Tue, 10 Dec 2024 11:11:03 -0500 Subject: [PATCH] Make jsonschema custom_functions like jsonpath --- include/jsoncons_ext/jsonpath/expression.hpp | 45 +++++++++++-------- .../jsonpath/jsonpath_expression.hpp | 2 +- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/include/jsoncons_ext/jsonpath/expression.hpp b/include/jsoncons_ext/jsonpath/expression.hpp index be3457de3..1b31ee26b 100644 --- a/include/jsoncons_ext/jsonpath/expression.hpp +++ b/include/jsoncons_ext/jsonpath/expression.hpp @@ -397,7 +397,8 @@ namespace detail { return is_right_associative_; } - virtual Json evaluate(JsonReference, std::error_code&) const = 0; + virtual Json evaluate(JsonReference, + std::error_code&) const = 0; }; template @@ -424,7 +425,8 @@ namespace detail { : unary_operator(1, true) {} - Json evaluate(JsonReference val, std::error_code&) const override + Json evaluate(JsonReference val, + std::error_code&) const override { return is_false(val) ? Json(true, semantic_tag::none) : Json(false, semantic_tag::none); } @@ -438,7 +440,8 @@ namespace detail { : unary_operator(1, true) {} - Json evaluate(JsonReference val, std::error_code&) const override + Json evaluate(JsonReference val, + std::error_code&) const override { if (val.is_int64()) { @@ -471,7 +474,8 @@ namespace detail { regex_operator(regex_operator&&) = default; regex_operator& operator=(regex_operator&&) = default; - Json evaluate(JsonReference val, std::error_code&) const override + Json evaluate(JsonReference val, + std::error_code&) const override { if (!val.is_string()) { @@ -503,7 +507,10 @@ namespace detail { return is_right_associative_; } - virtual Json evaluate(JsonReference, JsonReference, std::error_code&) const = 0; + virtual Json evaluate(JsonReference, + JsonReference, + + std::error_code&) const = 0; virtual std::string to_string(int = 0) const { @@ -1019,7 +1026,7 @@ namespace detail { } virtual value_type evaluate(const std::vector& args, - std::error_code& ec) const = 0; + std::error_code& ec) const = 0; virtual std::string to_string(int level = 0) const { @@ -1035,7 +1042,7 @@ namespace detail { }; template - class function_wrapper : public function_base + class decorator_function : public function_base { public: using value_type = Json; @@ -1045,7 +1052,7 @@ namespace detail { private: function_type f_; public: - function_wrapper(jsoncons::optional arity, + decorator_function(jsoncons::optional arity, const function_type& f) : function_base(arity), f_(f) { @@ -1072,7 +1079,7 @@ namespace detail { } value_type evaluate(const std::vector& args, - std::error_code& ec) const override + std::error_code& ec) const override { if (args.size() != *this->arity()) { @@ -1140,7 +1147,7 @@ namespace detail { } value_type evaluate(const std::vector& args, - std::error_code& ec) const override + std::error_code& ec) const override { if (args.size() != *this->arity()) { @@ -2357,7 +2364,7 @@ namespace detail { using string_type = typename Json::string_type; using value_type = Json; using reference = JsonReference; - using function_type = function_base; + using function_base_type = function_base; using selector_type = jsonpath_selector; struct MyHash @@ -2381,8 +2388,8 @@ namespace detail { std::vector> temp_json_values_; std::vector>> unary_operators_; - std::unordered_map,MyHash> functions_; - std::unordered_map,MyHash> custom_functions_; + std::unordered_map,MyHash> functions_; + std::unordered_map,MyHash> custom_functions_; static_resources(const allocator_type& alloc = allocator_type()) : alloc_(alloc) @@ -2430,7 +2437,7 @@ namespace detail { for (const auto& item : functions) { custom_functions_.emplace(item.name(), - jsoncons::make_unique>(item.arity(),item.function())); + jsoncons::make_unique>(item.arity(),item.function())); } } @@ -2452,7 +2459,7 @@ namespace detail { { } - const function_type* get_function(const string_type& name, std::error_code& ec) const + const function_base_type* get_function(const string_type& name, std::error_code& ec) const { auto it = functions_.find(name); if (it == functions_.end()) @@ -3239,10 +3246,10 @@ namespace detail { expression& operator=(expression&& expr) = default; value_type evaluate(dynamic_resources& resources, - reference root, - reference current, - result_options options, - std::error_code& ec) const override + reference root, + reference current, + result_options options, + std::error_code& ec) const override { std::vector stack; std::vector arg_stack; diff --git a/include/jsoncons_ext/jsonpath/jsonpath_expression.hpp b/include/jsoncons_ext/jsonpath/jsonpath_expression.hpp index 97e974c12..7b2910afc 100644 --- a/include/jsoncons_ext/jsonpath/jsonpath_expression.hpp +++ b/include/jsoncons_ext/jsonpath/jsonpath_expression.hpp @@ -78,7 +78,7 @@ namespace jsonpath { jsonpath_expression& operator=(const jsonpath_expression&) = delete; jsonpath_expression& operator=(jsonpath_expression&&) = default; - template + template typename std::enable_if::value,void>::type evaluate(const_reference root, BinaryCallback callback, result_options options = result_options()) const {