Skip to content

Commit

Permalink
path_node{}
Browse files Browse the repository at this point in the history
  • Loading branch information
danielaparker committed Nov 14, 2023
1 parent 6938723 commit be0548b
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 43 deletions.
14 changes: 1 addition & 13 deletions include/jsoncons_ext/jsonpath/expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2258,18 +2258,6 @@ namespace detail {
return ptr;
}

const path_node_type& root_path_node() const
{
static path_node_type root('$');
return root;
}

const path_node_type& current_path_node() const
{
static path_node_type root('@');
return root;
}

const string_type& length_label() const
{
return length_label_;
Expand Down Expand Up @@ -3379,7 +3367,7 @@ namespace detail {
//}
//std::cout << "selector item: " << *ptr << "\n";

reference val = tok.selector_->evaluate(resources, root, resources.current_path_node(), item.value(), options, ec);
reference val = tok.selector_->evaluate(resources, root, path_node_type{}, item.value(), options, ec);

stack.pop_back();
stack.emplace_back(stack_item_type(std::addressof(val)));
Expand Down
10 changes: 5 additions & 5 deletions include/jsoncons_ext/jsonpath/json_query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ namespace jsonpath {
{
v = std::forward<T>(new_value);
};
expr.evaluate_with_replacement(resources, instance, resources.root_path_node(), instance, callback);
expr.evaluate_with_replacement(resources, instance, path_node_type{}, instance, callback);
}

template<class Json, class T, class TempAllocator>
Expand All @@ -105,7 +105,7 @@ namespace jsonpath {
{
v = Json(std::forward<T>(new_value), semantic_tag::none);
};
expr.evaluate_with_replacement(resources, instance, resources.root_path_node(), instance, callback);
expr.evaluate_with_replacement(resources, instance, path_node_type{}, instance, callback);
}

template<class Json, class BinaryCallback>
Expand All @@ -131,7 +131,7 @@ namespace jsonpath {
{
callback(to_jsonpath(path), val);
};
expr.evaluate_with_replacement(resources, instance, resources.root_path_node(), instance, f);
expr.evaluate_with_replacement(resources, instance, path_node_type{}, instance, f);
}

template<class Json, class BinaryCallback, class TempAllocator>
Expand All @@ -158,7 +158,7 @@ namespace jsonpath {
{
callback(to_jsonpath(path), val);
};
expr.evaluate_with_replacement(resources, instance, resources.root_path_node(), instance, f);
expr.evaluate_with_replacement(resources, instance, path_node_type{}, instance, f);
}

// Legacy replace function
Expand All @@ -183,7 +183,7 @@ namespace jsonpath {
{
v = callback(v);
};
expr.evaluate_with_replacement(resources, instance, resources.root_path_node(), instance, f);
expr.evaluate_with_replacement(resources, instance, path_node_type{}, instance, f);
}

} // namespace jsonpath
Expand Down
14 changes: 7 additions & 7 deletions include/jsoncons_ext/jsonpath/jsonpath_expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2563,7 +2563,7 @@ namespace detail {
{
callback(to_jsonpath(path), val);
};
expr_.evaluate(resources, instance, resources.root_path_node(), instance, f, options);
expr_.evaluate(resources, instance, path_node_type{}, instance, f, options);
}

Json evaluate(reference instance, result_options options = result_options()) const
Expand All @@ -2577,13 +2577,13 @@ namespace detail {
{
result.emplace_back(to_jsonpath(path));
};
expr_.evaluate(resources, instance, resources.root_path_node(), instance, callback, options);
expr_.evaluate(resources, instance, path_node_type(), instance, callback, options);
return result;
}
else
{
jsoncons::jsonpath::detail::dynamic_resources<Json,reference> resources{alloc_};
return expr_.evaluate(resources, instance, resources.current_path_node(), instance, options);
return expr_.evaluate(resources, instance, path_node_type(), instance, options);
}
}
};
Expand Down Expand Up @@ -2629,7 +2629,7 @@ namespace detail {
select(reference instance, BinaryCallback callback, result_options options = result_options()) const
{
jsoncons::jsonpath::detail::dynamic_resources<value_type,reference> resources{alloc_};
expr_.evaluate(resources, instance, resources.root_path_node(), instance, callback, options);
expr_.evaluate(resources, instance, path_node_type{}, instance, callback, options);
}

value_type evaluate(reference instance, result_options options = result_options()) const
Expand All @@ -2643,13 +2643,13 @@ namespace detail {
{
result.emplace_back(to_jsonpath(p));
};
expr_.evaluate(resources, instance, resources.root_path_node(), instance, callback, options);
expr_.evaluate(resources, instance, path_node_type(), instance, callback, options);
return result;
}
else
{
jsoncons::jsonpath::detail::dynamic_resources<value_type,reference> resources{alloc_};
return expr_.evaluate(resources, instance, resources.current_path_node(), instance, options);
return expr_.evaluate(resources, instance, path_node_type(), instance, options);
}
}

Expand All @@ -2658,7 +2658,7 @@ namespace detail {
update(reference instance, BinaryCallback callback) const
{
jsoncons::jsonpath::detail::dynamic_resources<value_type,reference> resources{alloc_};
expr_.evaluate_with_replacement(resources, instance, resources.root_path_node(), instance, callback);
expr_.evaluate_with_replacement(resources, instance, path_node_type{}, instance, callback);
}
};

Expand Down
16 changes: 7 additions & 9 deletions include/jsoncons_ext/jsonpath/path_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,34 +32,33 @@ namespace jsonpath {
const basic_path_node* parent_;
std::size_t size_;
location_element_kind node_kind_;
char_type root_;
string_view_type name_;
std::size_t index_;

public:
basic_path_node(char_type root)
basic_path_node()
: parent_(nullptr), size_(1),
node_kind_(location_element_kind::root),
root_{root}, name_(&root_,1), index_(0)
index_(0)
{
}

basic_path_node(const basic_path_node* parent, const string_view_type& name)
: parent_(parent), size_(parent == nullptr ? 1 : parent->size()+1),
node_kind_(location_element_kind::name), root_(0), name_(name), index_(0)
node_kind_(location_element_kind::name), name_(name), index_(0)
{
}

basic_path_node(const basic_path_node* parent, std::size_t index)
: parent_(parent), size_(parent == nullptr ? 1 : parent->size()+1),
node_kind_(location_element_kind::index), root_(0), index_(index)
node_kind_(location_element_kind::index), index_(index)
{
}

basic_path_node(const basic_path_node& other)
: parent_(other.parent_), size_(other.size()),
node_kind_(other.node_kind_),
root_(other.root_), name_(other.node_kind_ == location_element_kind::root ? string_view_type(&root_, 1) : other.name_),
name_(other.name_),
index_(other.index_)
{
}
Expand All @@ -70,8 +69,7 @@ namespace jsonpath {
size_ = other.size();
node_kind_ = other.node_kind_;
index_ = other.index_;
root_ = other.root_;
name_ = other.node_kind_ == location_element_kind::root ? string_view_type(&root_, 1) : other.name_;
name_ = other.name_;
return *this;
}

Expand Down Expand Up @@ -299,7 +297,7 @@ namespace jsonpath {
switch (node->node_kind())
{
case location_element_kind::root:
buffer.append(node->name().data(), node->name().size());
buffer.push_back('$');
break;
case location_element_kind::name:
buffer.push_back('[');
Expand Down
18 changes: 9 additions & 9 deletions test/jsonpath/src/path_node_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ namespace jsonpath = jsoncons::jsonpath;

TEST_CASE("test json_location equals")
{
jsonpath::path_node a1('$');
jsonpath::path_node a1{};
jsonpath::path_node a2(&a1,"foo");
jsonpath::path_node a3(&a2,"bar");
jsonpath::path_node a4(&a3,0);

jsonpath::path_node b1('$');
jsonpath::path_node b1{};
jsonpath::path_node b2(&b1,"foo");
jsonpath::path_node b3(&b2,"bar");
jsonpath::path_node b4(&b3,0);
Expand All @@ -29,7 +29,7 @@ TEST_CASE("test json_location equals")

TEST_CASE("test json_location with solidus to_string")
{
jsonpath::path_node a1('$');
jsonpath::path_node a1{};
jsonpath::path_node a2(&a1,"foo's");
jsonpath::path_node a3(&a2,"bar");
jsonpath::path_node a4(&a3,0);
Expand All @@ -41,12 +41,12 @@ TEST_CASE("test path_node less")
{
SECTION("test rhs < lhs")
{
jsonpath::path_node a1('$');
jsonpath::path_node a1{};
jsonpath::path_node a2(&a1,"foo");
jsonpath::path_node a3(&a2,"bar");
jsonpath::path_node a4(&a3,0);

jsonpath::path_node b1('$');
jsonpath::path_node b1{};
jsonpath::path_node b2(&b1,"baz");
jsonpath::path_node b3(&b2,"bar");
jsonpath::path_node b4(&b3,0);
Expand All @@ -65,12 +65,12 @@ TEST_CASE("test path_node less")

SECTION("test rhs < lhs 2")
{
jsonpath::path_node a1('$');
jsonpath::path_node a1{};
jsonpath::path_node a2(&a1,"foo");
jsonpath::path_node a3(&a2,"bar");
jsonpath::path_node a4(&a3,0);

jsonpath::path_node b1('$');
jsonpath::path_node b1{};
jsonpath::path_node b2(&b1,"baz");
jsonpath::path_node b3(&b2,"g");
jsonpath::path_node b4(&b3,0);
Expand All @@ -89,12 +89,12 @@ TEST_CASE("test path_node less")

SECTION("test rhs == lhs")
{
jsonpath::path_node a1('$');
jsonpath::path_node a1{};
jsonpath::path_node a2(&a1,"foo");
jsonpath::path_node a3(&a2,"bar");
jsonpath::path_node a4(&a3,0);

jsonpath::path_node b1('$');
jsonpath::path_node b1{};
jsonpath::path_node b2(&b1,"foo");
jsonpath::path_node b3(&b2,"bar");
jsonpath::path_node b4(&b3,0);
Expand Down

0 comments on commit be0548b

Please sign in to comment.