Skip to content

Commit

Permalink
Fixed issue with length and recursive select
Browse files Browse the repository at this point in the history
  • Loading branch information
danielaparker committed Jan 24, 2025
1 parent 69250a1 commit b03e0bb
Show file tree
Hide file tree
Showing 11 changed files with 259 additions and 272 deletions.
2 changes: 1 addition & 1 deletion include/jsoncons_ext/jsonpath/json_query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <jsoncons/tag_type.hpp>
#include <jsoncons/utility/extension_traits.hpp>

#include <jsoncons_ext/jsonpath/expression.hpp>
#include <jsoncons_ext/jsonpath/token_evaluator.hpp>
#include <jsoncons_ext/jsonpath/jsonpath_expression.hpp>
#include <jsoncons_ext/jsonpath/jsonpath_parser.hpp>
#include <jsoncons_ext/jsonpath/path_node.hpp>
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 @@ -20,7 +20,7 @@
#include <jsoncons/utility/extension_traits.hpp>
#include <jsoncons/tag_type.hpp>

#include <jsoncons_ext/jsonpath/expression.hpp>
#include <jsoncons_ext/jsonpath/token_evaluator.hpp>
#include <jsoncons_ext/jsonpath/json_location.hpp>
#include <jsoncons_ext/jsonpath/jsonpath_parser.hpp>
#include <jsoncons_ext/jsonpath/path_node.hpp>
Expand Down
16 changes: 8 additions & 8 deletions include/jsoncons_ext/jsonpath/jsonpath_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include <jsoncons/tag_type.hpp>
#include <jsoncons/utility/unicode_traits.hpp>

#include <jsoncons_ext/jsonpath/expression.hpp>
#include <jsoncons_ext/jsonpath/token_evaluator.hpp>
#include <jsoncons_ext/jsonpath/jsonpath_error.hpp>
#include <jsoncons_ext/jsonpath/path_node.hpp>
#include <jsoncons_ext/jsonpath/jsonpath_selector.hpp>
Expand Down Expand Up @@ -118,7 +118,7 @@ namespace detail {
using pointer = typename path_value_pair_type::value_pointer;
using token_type = token<Json,JsonReference>;
using path_expression_type = path_expression<Json,JsonReference>;
using expression_type = expression<Json,JsonReference>;
using token_evaluator_type = token_evaluator<Json,JsonReference>;
using path_node_type = basic_path_node<typename Json::char_type>;
using selector_type = jsonpath_selector<Json,JsonReference>;

Expand Down Expand Up @@ -2199,11 +2199,11 @@ namespace detail {

if (!output_stack_.empty() && output_stack_.back().is_path())
{
output_stack_.back().selector_->append_selector(resources.new_selector(filter_selector<Json,JsonReference>(expression_type(std::move(toks)))));
output_stack_.back().selector_->append_selector(resources.new_selector(filter_selector<Json,JsonReference>(token_evaluator_type(std::move(toks)))));
}
else
{
output_stack_.emplace_back(token_type(resources.new_selector(filter_selector<Json,JsonReference>(expression_type(std::move(toks))))));
output_stack_.emplace_back(token_type(resources.new_selector(filter_selector<Json,JsonReference>(token_evaluator_type(std::move(toks))))));
}
//std::cout << "push_token end_filter 2\n";
//for (const auto& tok2 : output_stack_)
Expand Down Expand Up @@ -2249,11 +2249,11 @@ namespace detail {

if (!output_stack_.empty() && output_stack_.back().is_path())
{
output_stack_.back().selector_->append_selector(resources.new_selector(index_expression_selector<Json,JsonReference>(expression_type(std::move(toks)))));
output_stack_.back().selector_->append_selector(resources.new_selector(index_expression_selector<Json,JsonReference>(token_evaluator_type(std::move(toks)))));
}
else
{
output_stack_.emplace_back(token_type(resources.new_selector(index_expression_selector<Json,JsonReference>(expression_type(std::move(toks))))));
output_stack_.emplace_back(token_type(resources.new_selector(index_expression_selector<Json,JsonReference>(token_evaluator_type(std::move(toks))))));
}
break;
}
Expand Down Expand Up @@ -2285,7 +2285,7 @@ namespace detail {
std::reverse(toks.begin(), toks.end());
++it;
output_stack_.erase(it.base(),output_stack_.end());
output_stack_.emplace_back(token_type(jsoncons::make_unique<expression_type>(std::move(toks))));
output_stack_.emplace_back(token_type(jsoncons::make_unique<token_evaluator_type>(std::move(toks))));
break;
}
case jsonpath_token_kind::selector:
Expand Down Expand Up @@ -2390,7 +2390,7 @@ namespace detail {

if (!output_stack_.empty() && output_stack_.back().is_path())
{
output_stack_.back().selector_->append_selector(resources.new_selector(function_selector<Json,JsonReference>(expression_type(std::move(toks)))));
output_stack_.back().selector_->append_selector(resources.new_selector(function_selector<Json,JsonReference>(token_evaluator_type(std::move(toks)))));
}
else
{
Expand Down
20 changes: 12 additions & 8 deletions include/jsoncons_ext/jsonpath/jsonpath_selector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include <jsoncons/config/jsoncons_config.hpp>
#include <jsoncons/tag_type.hpp>

#include <jsoncons_ext/jsonpath/expression.hpp>
#include <jsoncons_ext/jsonpath/token_evaluator.hpp>
#include <jsoncons_ext/jsonpath/path_node.hpp>

namespace jsoncons {
Expand Down Expand Up @@ -815,7 +815,11 @@ namespace detail {
auto jptr = context.create_json(json_array_arg, semantic_tag::none, context.get_allocator());
json_array_receiver<Json,JsonReference> receiver(jptr);
select(context, root, last, current, receiver, options);
return *jptr;
if (jptr->empty())
{
return context.null_value();
}
return jptr->size() == 1 ? (*jptr)[0] : *jptr;
}

std::string to_string(int level) const override
Expand Down Expand Up @@ -923,7 +927,7 @@ namespace detail {
{
using supertype = base_selector<Json,JsonReference>;

expression<Json,JsonReference> expr_;
token_evaluator<Json,JsonReference> expr_;

public:
using value_type = typename supertype::value_type;
Expand All @@ -934,7 +938,7 @@ namespace detail {
using path_generator_type = path_generator<Json,JsonReference>;
using node_receiver_type = typename supertype::node_receiver_type;

filter_selector(expression<Json,JsonReference>&& expr)
filter_selector(token_evaluator<Json,JsonReference>&& expr)
: base_selector<Json,JsonReference>(), expr_(std::move(expr))
{
}
Expand Down Expand Up @@ -1013,7 +1017,7 @@ namespace detail {
using allocator_type = typename Json::allocator_type;
using string_type = typename Json::string_type;

expression<Json,JsonReference> expr_;
token_evaluator<Json,JsonReference> expr_;

public:
using value_type = typename supertype::value_type;
Expand All @@ -1024,7 +1028,7 @@ namespace detail {
using path_generator_type = path_generator<Json,JsonReference>;
using node_receiver_type = typename supertype::node_receiver_type;

index_expression_selector(expression<Json,JsonReference>&& expr)
index_expression_selector(token_evaluator<Json,JsonReference>&& expr)
: base_selector<Json,JsonReference>(), expr_(std::move(expr))
{
}
Expand Down Expand Up @@ -1194,7 +1198,7 @@ namespace detail {
{
using supertype = base_selector<Json,JsonReference>;

expression<Json,JsonReference> expr_;
token_evaluator<Json,JsonReference> expr_;

public:
using value_type = typename supertype::value_type;
Expand All @@ -1205,7 +1209,7 @@ namespace detail {
using path_generator_type = path_generator<Json,JsonReference>;
using node_receiver_type = typename supertype::node_receiver_type;

function_selector(expression<Json,JsonReference>&& expr)
function_selector(token_evaluator<Json,JsonReference>&& expr)
: base_selector<Json,JsonReference>(), expr_(std::move(expr))
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

// See https://github.com/danielaparker/jsoncons for latest version

#ifndef JSONCONS_EXT_JSONPATH_EXPRESSION_HPP
#define JSONCONS_EXT_JSONPATH_EXPRESSION_HPP
#ifndef JSONCONS_EXT_JSONPATH_TOKEN_EVALUATOR_HPP
#define JSONCONS_EXT_JSONPATH_TOKEN_EVALUATOR_HPP

#include <cstddef>
#include <cstdint>
Expand Down Expand Up @@ -1947,7 +1947,7 @@ namespace detail {
{
case json_type::object_value:
case json_type::array_value:
std::cout << "LENGTH ARG: " << arg0 << "\n";
//std::cout << "LENGTH ARG: " << arg0 << "\n";
return value_type(arg0.size(), semantic_tag::none);
case json_type::string_value:
{
Expand Down Expand Up @@ -3269,7 +3269,7 @@ namespace detail {
};

template <typename Json,typename JsonReference>
class expression : public expression_base<Json,JsonReference>
class token_evaluator : public expression_base<Json,JsonReference>
{
public:
using path_value_pair_type = path_value_pair<Json,JsonReference>;
Expand All @@ -3293,22 +3293,22 @@ namespace detail {
std::vector<token_type> token_list_;
public:

expression()
token_evaluator()
{
}

expression(const expression& expr) = delete;
expression(expression&& expr) = default;
token_evaluator(const token_evaluator& expr) = delete;
token_evaluator(token_evaluator&& expr) = default;

expression(std::vector<token_type>&& token_stack)
token_evaluator(std::vector<token_type>&& token_stack)
: token_list_(std::move(token_stack))
{
}

expression& operator=(const expression& expr) = delete;
expression& operator=(expression&& expr) = default;
token_evaluator& operator=(const token_evaluator& expr) = delete;
token_evaluator& operator=(token_evaluator&& expr) = default;

~expression() = default;
~token_evaluator() = default;

value_type evaluate(eval_context<Json,reference>& context,
reference root,
Expand Down Expand Up @@ -3369,7 +3369,6 @@ namespace detail {
stack.emplace_back(std::addressof(root));
break;
case jsonpath_token_kind::current_node:
std::cout << "CURRENT NODE: " << current << "\n";
stack.emplace_back(std::addressof(current));
break;
case jsonpath_token_kind::argument:
Expand Down Expand Up @@ -3427,7 +3426,6 @@ namespace detail {
//std::cout << "selector item: " << *ptr << "\n";

reference val = tok.selector_->evaluate(context, root, path_node_type{}, item.value(), options, ec);
std::cout << "SELECTOR RESULT: " << val << "\n";

stack.pop_back();
stack.emplace_back(stack_item_type(std::addressof(val)));
Expand All @@ -3454,7 +3452,7 @@ namespace detail {
s.append("\n");
s.append(std::size_t(level)*2, ' ');
}
s.append("expression ");
s.append("token_evaluator ");
for (const auto& item : token_list_)
{
s.append(item.to_string(level+1));
Expand All @@ -3470,4 +3468,4 @@ namespace detail {
} // namespace jsonpath
} // namespace jsoncons

#endif // JSONCONS_EXT_JSONPATH_EXPRESSION_HPP
#endif // JSONCONS_EXT_JSONPATH_TOKEN_EVALUATOR_HPP
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

// See https://github.com/danielaparker/jsoncons for latest version

#ifndef JSONCONS_EXT_JSONSCHEMA_COMMON_EVALUATION_CONTEXT_HPP
#define JSONCONS_EXT_JSONSCHEMA_COMMON_EVALUATION_CONTEXT_HPP
#ifndef JSONCONS_EXT_JSONSCHEMA_COMMON_EVAL_CONTEXT_HPP
#define JSONCONS_EXT_JSONSCHEMA_COMMON_EVAL_CONTEXT_HPP

#include <cstddef>
#include <string>
Expand Down Expand Up @@ -63,31 +63,31 @@ namespace jsonschema {
}

template <typename Json>
class evaluation_context
class eval_context
{
private:
std::vector<const schema_validator<Json>*> dynamic_scope_;
jsonpointer::json_pointer eval_path_;
evaluation_flags flags_;
public:
evaluation_context()
eval_context()
: flags_{}
{
}

evaluation_context(const evaluation_context& other)
eval_context(const eval_context& other)
: dynamic_scope_ { other.dynamic_scope_}, eval_path_{other.eval_path_},
flags_(other.flags_)
{
}

evaluation_context(evaluation_context&& other) noexcept
eval_context(eval_context&& other) noexcept
: dynamic_scope_{std::move(other.dynamic_scope_)},eval_path_{std::move(other.eval_path_)},
flags_(other.flags_)
{
}

evaluation_context(const evaluation_context& parent, const schema_validator<Json> *validator)
eval_context(const eval_context& parent, const schema_validator<Json> *validator)
: dynamic_scope_ { parent.dynamic_scope_ }, eval_path_{ parent.eval_path_ },
flags_(parent.flags_)
{
Expand All @@ -97,7 +97,7 @@ namespace jsonschema {
}
}

evaluation_context(const evaluation_context& parent, const schema_validator<Json> *validator,
eval_context(const eval_context& parent, const schema_validator<Json> *validator,
evaluation_flags flags)
: dynamic_scope_ { parent.dynamic_scope_ }, eval_path_{ parent.eval_path_ },
flags_(flags)
Expand All @@ -108,27 +108,27 @@ namespace jsonschema {
}
}

evaluation_context(const evaluation_context& parent, const std::string& name)
eval_context(const eval_context& parent, const std::string& name)
: dynamic_scope_{parent.dynamic_scope_}, eval_path_(parent.eval_path() / name),
flags_(parent.flags_)

{
}

evaluation_context(const evaluation_context& parent, const std::string& name,
eval_context(const eval_context& parent, const std::string& name,
evaluation_flags flags)
: dynamic_scope_{parent.dynamic_scope_}, eval_path_(parent.eval_path() / name),
flags_(flags)
{
}

evaluation_context(const evaluation_context& parent, std::size_t index)
eval_context(const eval_context& parent, std::size_t index)
: dynamic_scope_{parent.dynamic_scope_}, eval_path_(parent.eval_path() / index),
flags_(parent.flags_)
{
}

evaluation_context(const evaluation_context& parent, std::size_t index,
eval_context(const eval_context& parent, std::size_t index,
evaluation_flags flags)
: dynamic_scope_{parent.dynamic_scope_}, eval_path_(parent.eval_path() / index),
flags_(flags)
Expand Down
Loading

0 comments on commit b03e0bb

Please sign in to comment.