Skip to content

Commit

Permalink
basic_path_node
Browse files Browse the repository at this point in the history
  • Loading branch information
danielaparker committed Nov 10, 2023
1 parent 612cb16 commit 967e5e0
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 52 deletions.
22 changes: 11 additions & 11 deletions include/jsoncons_ext/jsonpath/expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2069,7 +2069,7 @@ namespace detail {
using value_type = Json;
using reference = JsonReference;
using value_pointer = typename std::conditional<std::is_const<typename std::remove_reference<JsonReference>::type>::value,typename Json::const_pointer,typename Json::pointer>::type;
using path_node_type = path_node<typename Json::char_type>;
using path_node_type = basic_path_node<typename Json::char_type>;
using json_location_type = basic_json_location<char_type,typename Json::allocator_type>;
using path_pointer = const path_node_type*;

Expand Down Expand Up @@ -2130,7 +2130,7 @@ namespace detail {
using value_type = Json;
using reference = JsonReference;
using value_pointer = typename std::conditional<std::is_const<typename std::remove_reference<JsonReference>::type>::value,typename Json::const_pointer,typename Json::pointer>::type;
using path_node_type = path_node<typename Json::char_type>;
using path_node_type = basic_path_node<typename Json::char_type>;
using json_location_type = basic_json_location<char_type,typename Json::allocator_type>;
using path_pointer = const path_node_type*;
private:
Expand Down Expand Up @@ -2160,7 +2160,7 @@ namespace detail {
using char_type = typename Json::char_type;
using string_type = typename Json::string_type;
using reference = JsonReference;
using path_node_type = path_node<typename Json::char_type>;
using path_node_type = basic_path_node<typename Json::char_type>;

virtual ~node_receiver() noexcept = default;

Expand All @@ -2176,7 +2176,7 @@ namespace detail {
using reference = JsonReference;
using char_type = typename Json::char_type;
using string_type = typename Json::string_type;
using path_node_type = path_node<typename Json::char_type>;
using path_node_type = basic_path_node<typename Json::char_type>;
using json_location_type = basic_json_location<char_type,allocator_type>;
using path_value_pair_type = path_value_pair<Json,JsonReference>;

Expand All @@ -2202,7 +2202,7 @@ namespace detail {
using reference = JsonReference;
using char_type = typename Json::char_type;
using string_type = typename Json::string_type;
using path_node_type = path_node<typename Json::char_type>;
using path_node_type = basic_path_node<typename Json::char_type>;
using path_component_value_pair_type = path_component_value_pair<Json,JsonReference>;

std::vector<path_component_value_pair_type> nodes;
Expand All @@ -2222,7 +2222,7 @@ namespace detail {
using string_type = typename Json::string_type;
using reference = JsonReference;
using pointer = typename std::conditional<std::is_const<typename std::remove_reference<reference>::type>::value,typename Json::const_pointer,typename Json::pointer>::type;
using path_node_type = path_node<typename Json::char_type>;
using path_node_type = basic_path_node<typename Json::char_type>;
using path_component_value_pair_type = path_component_value_pair<Json,JsonReference>;

allocator_type alloc_;
Expand Down Expand Up @@ -2319,7 +2319,7 @@ namespace detail {
using reference = JsonReference;
using pointer = typename std::conditional<std::is_const<typename std::remove_reference<JsonReference>::type>::value,typename Json::const_pointer,typename Json::pointer>::type;
using path_value_pair_type = path_value_pair<Json,JsonReference>;
using path_node_type = path_node<typename Json::char_type>;
using path_node_type = basic_path_node<typename Json::char_type>;
using json_location_type = basic_json_location<char_type,typename Json::allocator_type>;
using node_receiver_type = node_receiver<Json,JsonReference>;
using selector_type = jsonpath_selector<Json,JsonReference>;
Expand Down Expand Up @@ -2623,7 +2623,7 @@ namespace detail {
using reference = JsonReference;
using pointer = typename std::conditional<std::is_const<typename std::remove_reference<JsonReference>::type>::value,typename Json::const_pointer,typename Json::pointer>::type;
using path_value_pair_type = path_value_pair<Json,JsonReference>;
using path_node_type = path_node<typename Json::char_type>;
using path_node_type = basic_path_node<typename Json::char_type>;

virtual ~expression_base() noexcept = default;

Expand Down Expand Up @@ -3004,7 +3004,7 @@ namespace detail {
using reference = JsonReference;
using char_type = typename Json::char_type;
using string_type = typename Json::string_type;
using path_node_type = path_node<typename Json::char_type>;
using path_node_type = basic_path_node<typename Json::char_type>;
using json_location_type = basic_json_location<char_type,typename Json::allocator_type>;
private:
allocator_type alloc_;
Expand Down Expand Up @@ -3040,7 +3040,7 @@ namespace detail {
using token_type = token<Json,JsonReference>;
using reference_arg_type = typename std::conditional<std::is_const<typename std::remove_reference<JsonReference>::type>::value,
const_reference_arg_t,reference_arg_t>::type;
using path_node_type = path_node<typename Json::char_type>;
using path_node_type = basic_path_node<typename Json::char_type>;
using json_location_type = basic_json_location<char_type,allocator_type>;
using selector_type = jsonpath_selector<Json,JsonReference>;
private:
Expand Down Expand Up @@ -3238,7 +3238,7 @@ namespace detail {
using token_type = token<Json,reference>;
using reference_arg_type = typename std::conditional<std::is_const<typename std::remove_reference<reference>::type>::value,
const_reference_arg_t,reference_arg_t>::type;
using path_node_type = path_node<typename Json::char_type>;
using path_node_type = basic_path_node<typename Json::char_type>;
using stack_item_type = value_or_pointer<Json,JsonReference>;
private:
std::vector<token_type> token_list_;
Expand Down
40 changes: 21 additions & 19 deletions include/jsoncons_ext/jsonpath/json_location.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace jsonpath {
enum class path_node_kind { root, index, name };

template <class CharT>
class path_node
class basic_path_node
{
template <class Ch,class Allocator> friend class basic_json_location;
public:
Expand All @@ -35,30 +35,30 @@ namespace jsonpath {
private:

char_type root_;
const path_node* parent_;
const basic_path_node* parent_;
path_node_kind node_kind_;
string_view_type name_;
std::size_t index_;

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

path_node(const path_node* parent, const string_view_type& name)
basic_path_node(const basic_path_node* parent, const string_view_type& name)
: root_(0), parent_(parent), node_kind_(path_node_kind::name), name_(name), index_(0)
{
}

path_node(const path_node* parent, std::size_t index)
basic_path_node(const basic_path_node* parent, std::size_t index)
: root_(0), parent_(parent), node_kind_(path_node_kind::index), index_(index)
{
}

path_node(const path_node& other)
basic_path_node(const basic_path_node& other)
: root_(other.root_),
parent_(other.parent_),
node_kind_(other.node_kind_),
Expand All @@ -67,7 +67,7 @@ namespace jsonpath {
{
}

path_node(path_node&& other)
basic_path_node(basic_path_node&& other)
: root_(other.root_),
parent_(other.parent_),
node_kind_(other.node_kind_),
Expand All @@ -76,7 +76,7 @@ namespace jsonpath {
{
}

path_node& operator=(const path_node& other)
basic_path_node& operator=(const basic_path_node& other)
{
root_ = other.root_;
parent_ = other.parent_;
Expand All @@ -86,7 +86,7 @@ namespace jsonpath {
return *this;
}

path_node& operator=(path_node&& other)
basic_path_node& operator=(basic_path_node&& other)
{
root_ = other.root_;
parent_ = other.parent_;
Expand All @@ -96,7 +96,7 @@ namespace jsonpath {
return *this;
}

const path_node* parent() const { return parent_;}
const basic_path_node* parent() const { return parent_;}

path_node_kind node_kind() const
{
Expand Down Expand Up @@ -124,7 +124,7 @@ namespace jsonpath {
return index_;
}

void swap(path_node& node)
void swap(basic_path_node& node)
{
std::swap(parent_, node.parent_);
std::swap(node_kind_, node.node_kind_);
Expand All @@ -141,7 +141,7 @@ namespace jsonpath {
return h;
}

int compare_node(const path_node& other) const
int compare_node(const basic_path_node& other) const
{
int diff = 0;
if (node_kind_ != other.node_kind_)
Expand Down Expand Up @@ -248,7 +248,7 @@ namespace jsonpath {
using allocator_type = Allocator;
using char_allocator_type = typename std::allocator_traits<allocator_type>:: template rebind_alloc<char_type>;
using string_type = std::basic_string<char_type,std::char_traits<char_type>,char_allocator_type>;
using path_node_type = path_node<char_type>;
using path_node_type = basic_path_node<char_type>;
using path_element_type = basic_path_element<CharT>;
using path_element_allocator_type = typename std::allocator_traits<allocator_type>:: template rebind_alloc<path_element_type>;
private:
Expand Down Expand Up @@ -384,23 +384,23 @@ namespace jsonpath {
Json* select(Json& root, const basic_json_location<typename Json::char_type,typename Json::allocator_type>& path)
{
Json* current = std::addressof(root);
for (const auto& path_node : path)
for (const auto& basic_path_node : path)
{
if (path_node.node_kind() == path_node_kind::index)
if (basic_path_node.node_kind() == path_node_kind::index)
{
if (current->type() != json_type::array_value || path_node.index() >= current->size())
if (current->type() != json_type::array_value || basic_path_node.index() >= current->size())
{
return nullptr;
}
current = std::addressof(current->at(path_node.index()));
current = std::addressof(current->at(basic_path_node.index()));
}
else if (path_node.node_kind() == path_node_kind::name)
else if (basic_path_node.node_kind() == path_node_kind::name)
{
if (current->type() != json_type::object_value)
{
return nullptr;
}
auto it = current->find(path_node.name());
auto it = current->find(basic_path_node.name());
if (it == current->object_range().end())
{
return nullptr;
Expand All @@ -415,6 +415,8 @@ namespace jsonpath {
using wjson_location = basic_json_location<wchar_t>;
using path_element = basic_path_element<char>;
using wpath_element = basic_path_element<wchar_t>;
using path_node = basic_path_node<char>;
using wpath_node = basic_path_node<wchar_t>;

} // namespace jsonpath
} // namespace jsoncons
Expand Down
6 changes: 3 additions & 3 deletions include/jsoncons_ext/jsonpath/jsonpath_expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ namespace detail {
using path_expression_type = path_expression<Json,JsonReference>;
using expression_type = expression<Json,JsonReference>;
using json_location_type = basic_json_location<char_type,allocator_type>;
using path_node_type = path_node<typename Json::char_type>;
using path_node_type = basic_path_node<typename Json::char_type>;
using selector_type = jsonpath_selector<Json,JsonReference>;

private:
Expand Down Expand Up @@ -2496,7 +2496,7 @@ namespace detail {
using pointer = typename std::conditional<std::is_const<typename std::remove_reference<reference>::type>::value, typename Json::const_pointer, typename Json::pointer>::type;
using allocator_type = typename value_type::allocator_type;
using evaluator_type = typename jsoncons::jsonpath::detail::jsonpath_evaluator<value_type, reference>;
using path_node_type = path_node<typename Json::char_type>;
using path_node_type = basic_path_node<typename Json::char_type>;
using json_location_type = basic_json_location<typename Json::char_type,typename Json::allocator_type>;
using path_expression_type = jsoncons::jsonpath::detail::path_expression<value_type,reference>;
using path_pointer = const path_node_type*;
Expand All @@ -2515,7 +2515,7 @@ namespace detail {
using pointer = typename std::conditional<std::is_const<typename std::remove_reference<reference>::type>::value, typename Json::const_pointer, typename Json::pointer>::type;
using allocator_type = typename value_type::allocator_type;
using evaluator_type = typename jsoncons::jsonpath::detail::jsonpath_evaluator<value_type, reference>;
using path_node_type = path_node<typename Json::char_type>;
using path_node_type = basic_path_node<typename Json::char_type>;
using json_location_type = basic_json_location<typename Json::char_type,typename Json::allocator_type>;
using path_expression_type = jsoncons::jsonpath::detail::path_expression<value_type,reference>;
using path_pointer = const path_node_type*;
Expand Down
4 changes: 2 additions & 2 deletions include/jsoncons_ext/jsonpath/jsonpath_selector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ namespace detail {
using reference = JsonReference;
using char_type = typename Json::char_type;
using string_type = typename Json::string_type;
using path_node_type = path_node<typename Json::char_type>;
using path_node_type = basic_path_node<typename Json::char_type>;

Json* val;

Expand All @@ -135,7 +135,7 @@ namespace detail {
using char_type = typename Json::char_type;
using string_view_type = typename Json::string_view_type;
using string_type = typename Json::string_type;
using path_node_type = path_node<typename Json::char_type>;
using path_node_type = basic_path_node<typename Json::char_type>;

static const path_node_type& generate(dynamic_resources<Json,JsonReference>& resources,
const path_node_type& last,
Expand Down
34 changes: 17 additions & 17 deletions test/jsonpath/src/json_location_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@
#include <catch/catch.hpp>
#include <iostream>

using path_node = jsoncons::jsonpath::path_node<char>;
using basic_path_node = jsoncons::jsonpath::basic_path_node<char>;
using jsoncons::jsonpath::json_location;

TEST_CASE("test json_location equals")
{
path_node component1('$');
path_node component2(&component1,"foo");
path_node component3(&component2,"bar");
path_node component4(&component3,0);
basic_path_node component1('$');
basic_path_node component2(&component1,"foo");
basic_path_node component3(&component2,"bar");
basic_path_node component4(&component3,0);

path_node component11('$');
path_node component12(&component11,"foo");
path_node component13(&component12,"bar");
path_node component14(&component13,0);
basic_path_node component11('$');
basic_path_node component12(&component11,"foo");
basic_path_node component13(&component12,"bar");
basic_path_node component14(&component13,0);

json_location path1(component4);
json_location path2(component14);
Expand All @@ -32,10 +32,10 @@ TEST_CASE("test json_location equals")

TEST_CASE("test json_location to_string")
{
path_node component1('$');
path_node component2(&component1,"foo");
path_node component3(&component2,"bar");
path_node component4(&component3,0);
basic_path_node component1('$');
basic_path_node component2(&component1,"foo");
basic_path_node component3(&component2,"bar");
basic_path_node component4(&component3,0);

json_location path1(component4);

Expand All @@ -44,10 +44,10 @@ TEST_CASE("test json_location to_string")

TEST_CASE("test json_location with solidus to_string")
{
path_node component1('$');
path_node component2(&component1,"foo's");
path_node component3(&component2,"bar");
path_node component4(&component3,0);
basic_path_node component1('$');
basic_path_node component2(&component1,"foo's");
basic_path_node component3(&component2,"bar");
basic_path_node component4(&component3,0);

json_location path1(component4);

Expand Down

0 comments on commit 967e5e0

Please sign in to comment.