Skip to content

Commit

Permalink
Make jsonschema custom_functions like jsonpath
Browse files Browse the repository at this point in the history
  • Loading branch information
danielaparker committed Dec 10, 2024
1 parent eb5eff1 commit 54e57c3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
45 changes: 26 additions & 19 deletions include/jsoncons_ext/jsonpath/expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename Json>
Expand All @@ -424,7 +425,8 @@ namespace detail {
: unary_operator<Json,JsonReference>(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);
}
Expand All @@ -438,7 +440,8 @@ namespace detail {
: unary_operator<Json,JsonReference>(1, true)
{}

Json evaluate(JsonReference val, std::error_code&) const override
Json evaluate(JsonReference val,
std::error_code&) const override
{
if (val.is_int64())
{
Expand Down Expand Up @@ -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())
{
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -1019,7 +1026,7 @@ namespace detail {
}

virtual value_type evaluate(const std::vector<parameter_type>& args,
std::error_code& ec) const = 0;
std::error_code& ec) const = 0;

virtual std::string to_string(int level = 0) const
{
Expand All @@ -1035,7 +1042,7 @@ namespace detail {
};

template <typename Json>
class function_wrapper : public function_base<Json>
class decorator_function : public function_base<Json>
{
public:
using value_type = Json;
Expand All @@ -1045,7 +1052,7 @@ namespace detail {
private:
function_type f_;
public:
function_wrapper(jsoncons::optional<std::size_t> arity,
decorator_function(jsoncons::optional<std::size_t> arity,
const function_type& f)
: function_base<Json>(arity), f_(f)
{
Expand All @@ -1072,7 +1079,7 @@ namespace detail {
}

value_type evaluate(const std::vector<parameter_type>& args,
std::error_code& ec) const override
std::error_code& ec) const override
{
if (args.size() != *this->arity())
{
Expand Down Expand Up @@ -1140,7 +1147,7 @@ namespace detail {
}

value_type evaluate(const std::vector<parameter_type>& args,
std::error_code& ec) const override
std::error_code& ec) const override
{
if (args.size() != *this->arity())
{
Expand Down Expand Up @@ -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<Json>;
using function_base_type = function_base<Json>;
using selector_type = jsonpath_selector<Json,JsonReference>;

struct MyHash
Expand All @@ -2381,8 +2388,8 @@ namespace detail {
std::vector<std::unique_ptr<Json>> temp_json_values_;
std::vector<std::unique_ptr<unary_operator<Json,JsonReference>>> unary_operators_;

std::unordered_map<string_type,std::unique_ptr<function_type>,MyHash> functions_;
std::unordered_map<string_type,std::unique_ptr<function_type>,MyHash> custom_functions_;
std::unordered_map<string_type,std::unique_ptr<function_base_type>,MyHash> functions_;
std::unordered_map<string_type,std::unique_ptr<function_base_type>,MyHash> custom_functions_;

static_resources(const allocator_type& alloc = allocator_type())
: alloc_(alloc)
Expand Down Expand Up @@ -2430,7 +2437,7 @@ namespace detail {
for (const auto& item : functions)
{
custom_functions_.emplace(item.name(),
jsoncons::make_unique<function_wrapper<Json>>(item.arity(),item.function()));
jsoncons::make_unique<decorator_function<Json>>(item.arity(),item.function()));
}
}

Expand All @@ -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())
Expand Down Expand Up @@ -3239,10 +3246,10 @@ namespace detail {
expression& operator=(expression&& expr) = default;

value_type evaluate(dynamic_resources<Json,reference>& 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_item_type> stack;
std::vector<parameter_type> arg_stack;
Expand Down
2 changes: 1 addition & 1 deletion include/jsoncons_ext/jsonpath/jsonpath_expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ namespace jsonpath {
jsonpath_expression& operator=(const jsonpath_expression&) = delete;
jsonpath_expression& operator=(jsonpath_expression&&) = default;

template <typename Json, typename BinaryCallback>
template <typename BinaryCallback>
typename std::enable_if<extension_traits::is_binary_function_object<BinaryCallback,const string_type&,const_reference>::value,void>::type
evaluate(const_reference root, BinaryCallback callback, result_options options = result_options()) const
{
Expand Down

0 comments on commit 54e57c3

Please sign in to comment.