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 aeaebba commit 97b326b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
40 changes: 20 additions & 20 deletions examples/src/jmespath_custom_function_examples.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,35 +27,35 @@ class my_custom_functions : public jmespath::custom_functions<Json>
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 now = std::chrono::system_clock::now();
auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
return resources.make_json(milliseconds.count());
return *resources.create_json(milliseconds.count());
}
);
this->register_function("current_index", // function name
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 @@ -66,64 +66,64 @@ class my_custom_functions : public jmespath::custom_functions<Json>
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 @@ -136,8 +136,8 @@ class my_custom_functions : public jmespath::custom_functions<Json>
{
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
5 changes: 3 additions & 2 deletions include/jsoncons_ext/jmespath/jmespath.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ namespace jmespath {

reference evaluate(reference val, dynamic_resources<Json>& resources, std::error_code& ec) const
{
return expr_->evaluate(val, resources, ec);
return *resources.create_json(deep_copy(expr_->evaluate(val, resources, ec)));
}
};

Expand Down Expand Up @@ -449,7 +449,8 @@ namespace jmespath {
{
}

reference evaluate(const std::vector<parameter_type>& params, dynamic_resources<Json>& resources,
reference evaluate(const std::vector<parameter_type>& params,
dynamic_resources<Json>& resources,
std::error_code& ec) const override
{
return f_(params, resources, ec);
Expand Down

0 comments on commit 97b326b

Please sign in to comment.