Skip to content

Commit

Permalink
Revert evaluate returns pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
danielaparker committed Dec 12, 2024
1 parent 97b326b commit 2bf9be6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 108 deletions.
123 changes: 18 additions & 105 deletions doc/ref/jmespath/jmespath.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,24 +175,24 @@ public:
0, // number of arguments
[](const jsoncons::span<const jmespath::parameter<Json>> params,
jmespath::dynamic_resources<Json>& resources,
std::error_code& ec) -> pointer
std::error_code& ec) -> reference
{
auto result = resources.make_json(current_index);
return result;
auto result = resources.create_json(current_index);
return *result;
}
);
this->register_function("generate_array", // function name
4, // number of arguments
[](const jsoncons::span<const jmespath::parameter<Json>> params,
jmespath::dynamic_resources<Json>& resources,
std::error_code& ec) -> pointer
std::error_code& ec) -> reference
{
JSONCONS_ASSERT(4 == params.size());

if (!(params[0].is_value() && params[2].is_expression()))
{
ec = jmespath::jmespath_errc::invalid_argument;
return resources.make_null();
return resources.null_value();
}

reference context = params[0].value();
Expand All @@ -203,64 +203,64 @@ public:
if (!countValue.is_number())
{
ec = jmespath::jmespath_errc::invalid_argument;
return resources.make_null();
return resources.null_value();
}

auto result = resources.make_json(jsoncons::json_array_arg);
auto result = resources.create_json(jsoncons::json_array_arg);
int count = countValue.template as<int>();
for (size_t i = 0; i < count; i++)
{
current_index = i;
std::error_code ec2;

auto ele = expr.evaluate(context, resources, ec2);
reference ele = expr.evaluate(context, resources, ec2);

if (ele->is_null())
if (ele.is_null())
{
auto defaultVal = get_value(context, resources, argDefault);
result->emplace_back(defaultVal);
}
else
{
result->emplace_back(jsoncons::json_const_pointer_arg, ele);
result->emplace_back(ele);
}
}
current_index = 0;

return result;
return *result;
}
);
this->register_function("add", // function name
2, // number of arguments
[](jsoncons::span<const jmespath::parameter<Json>> params,
jmespath::dynamic_resources<Json>& resources,
std::error_code& ec) -> pointer
std::error_code& ec) -> reference
{
JSONCONS_ASSERT(2 == params.size());

if (!(params[0].is_value() && params[1].is_value()))
{
ec = jmespath::jmespath_errc::invalid_argument;
return resources.make_null();
return resources.null_value();
}

reference arg0 = params[0].value();
reference arg1 = params[1].value();
if (!(arg0.is_number() && arg1.is_number()))
{
ec = jmespath::jmespath_errc::invalid_argument;
return resources.make_null();
return resources.null_value();
}

if (arg0.is<int64_t>() && arg1.is<int64_t>())
{
int64_t v = arg0.template as<int64_t>() + arg1.template as<int64_t>();
return resources.make_json(v);
return *resources.create_json(v);
}
else
{
double v = arg0.template as<double>() + arg1.template as<double>();
return resources.make_json(v);
return *resources.create_json(v);
}
}
);
Expand All @@ -273,8 +273,8 @@ public:
{
const auto& expr = param.expression();
std::error_code ec;
auto value = expr.evaluate(context, resources, ec);
return *value;
reference value = expr.evaluate(context, resources, ec);
return value;
}
else
{
Expand Down Expand Up @@ -327,91 +327,4 @@ int main()
}
```
Output:
```
[
{
"id": "id-xxx",
"position": 1,
"state": 1
},
{
"id": "",
"position": 2,
"state": 0
},
{
"id": "",
"position": 3,
"state": 0
},
{
"id": "",
"position": 4,
"state": 0
},
{
"id": "id-yyy",
"position": 5,
"state": 1
},
{
"id": "",
"position": 6,
"state": 0
},
{
"id": "",
"position": 7,
"state": 0
},
{
"id": "",
"position": 8,
"state": 0
},
{
"id": "id-mmm",
"position": 9,
"state": 2
},
{
"id": "",
"position": 10,
"state": 0
},
{
"id": "",
"position": 11,
"state": 0
},
{
"id": "",
"position": 12,
"state": 0
},
{
"id": "",
"position": 13,
"state": 0
},
{
"id": "",
"position": 14,
"state": 0
},
{
"id": "",
"position": 15,
"state": 0
},
{
"id": "",
"position": 16,
"state": 0
}
]
```
Credit to [PR #560](https://github.com/danielaparker/jsoncons/pull/560) for this example
4 changes: 3 additions & 1 deletion examples/src/jmespath_custom_function_examples.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ class my_custom_functions : public jmespath::custom_functions<Json>
}
else
{
result->emplace_back(ele);
result->emplace_back(ele);
//result->emplace_back(jsoncons::json_const_pointer_arg, &ele);
//result->emplace_back(*resources.create_json(deep_copy(ele)));
}
}
current_index = 0;
Expand Down
4 changes: 2 additions & 2 deletions include/jsoncons_ext/jmespath/jmespath.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ namespace jmespath {
public:
using reference = const Json&;

virtual ~expr_base() = default;

virtual reference evaluate(reference val, dynamic_resources<Json>& resources, std::error_code& ec) const = 0;
};

Expand Down Expand Up @@ -247,8 +249,6 @@ namespace jmespath {
return is_projection_;
}

virtual ~expr_base_impl() = default;

virtual void add_expression(std::unique_ptr<expr_base_impl>&& expressions) = 0;

virtual std::string to_string(std::size_t = 0) const
Expand Down

0 comments on commit 2bf9be6

Please sign in to comment.