Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helpers to directly push properties and indexes to pointers #1121

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,71 @@ template <typename PropertyT> class GenericPointer {
std::back_inserter(this->data));
}

/// Push a property token into the back of a JSON Pointer.
/// For example:
///
/// ```cpp
/// #include <sourcemeta/jsontoolkit/jsonpointer.h>
/// #include <cassert>
///
/// sourcemeta::jsontoolkit::Pointer pointer{"foo"};
/// const sourcemeta::jsontoolkit::Pointer other{"bar"};
/// pointer.push_back(other.back().to_property());
/// assert(pointer.size() == 2);
///
/// assert(pointer.at(0).is_property());
/// assert(pointer.at(1).is_property());
///
/// assert(pointer.at(0).to_property() == "foo");
/// assert(pointer.at(1).to_property() == "bar");
/// ```
auto push_back(const typename Token::Property &property) -> void {
this->data.emplace_back(property);
}

/// Move a property token into the back of a JSON Pointer.
/// For example:
///
/// ```cpp
/// #include <sourcemeta/jsontoolkit/jsonpointer.h>
/// #include <cassert>
///
/// sourcemeta::jsontoolkit::Pointer pointer{"foo"};
/// pointer.push_back("bar");
/// assert(pointer.size() == 2);
///
/// assert(pointer.at(0).is_property());
/// assert(pointer.at(1).is_property());
///
/// assert(pointer.at(0).to_property() == "foo");
/// assert(pointer.at(1).to_property() == "bar");
/// ```
auto push_back(typename Token::Property &&property) -> void {
this->data.emplace_back(std::move(property));
}

/// Push an index token into the back of a JSON Pointer.
/// For example:
///
/// ```cpp
/// #include <sourcemeta/jsontoolkit/jsonpointer.h>
/// #include <cassert>
///
/// sourcemeta::jsontoolkit::Pointer pointer{"foo"};
/// const sourcemeta::jsontoolkit::Pointer other{0};
/// pointer.push_back(other.back().to_index());
/// assert(pointer.size() == 2);
///
/// assert(pointer.at(0).is_property());
/// assert(pointer.at(1).is_index());
///
/// assert(pointer.at(0).to_property() == "foo");
/// assert(pointer.at(1).to_index() == 0);
/// ```
auto push_back(const typename Token::Index &index) -> void {
this->data.emplace_back(index);
}

/// Remove the last token of a JSON Pointer. For example:
///
/// ```cpp
Expand Down
42 changes: 42 additions & 0 deletions test/jsonpointer/jsonpointer_pointer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,45 @@ TEST(JSONPointer_pointer, initial_with_three_tokens) {
EXPECT_TRUE(pointer.at(1).is_property());
EXPECT_EQ(pointer.at(1).to_property(), "bar");
}

TEST(JSONPointer_pointer, push_back_property_copy) {
sourcemeta::jsontoolkit::Pointer pointer{"foo"};
const sourcemeta::jsontoolkit::Pointer other{"bar"};
pointer.push_back(other.back().to_property());
EXPECT_EQ(pointer.size(), 2);
EXPECT_TRUE(pointer.at(0).is_property());
EXPECT_EQ(pointer.at(0).to_property(), "foo");
EXPECT_TRUE(pointer.at(1).is_property());
EXPECT_EQ(pointer.at(1).to_property(), "bar");
}

TEST(JSONPointer_pointer, push_back_property_move) {
sourcemeta::jsontoolkit::Pointer pointer{"foo"};
pointer.push_back("bar");
EXPECT_EQ(pointer.size(), 2);
EXPECT_TRUE(pointer.at(0).is_property());
EXPECT_EQ(pointer.at(0).to_property(), "foo");
EXPECT_TRUE(pointer.at(1).is_property());
EXPECT_EQ(pointer.at(1).to_property(), "bar");
}

TEST(JSONPointer_pointer, push_back_index_copy) {
sourcemeta::jsontoolkit::Pointer pointer{"foo"};
const sourcemeta::jsontoolkit::Pointer other{0};
pointer.push_back(other.back().to_index());
EXPECT_EQ(pointer.size(), 2);
EXPECT_TRUE(pointer.at(0).is_property());
EXPECT_EQ(pointer.at(0).to_property(), "foo");
EXPECT_TRUE(pointer.at(1).is_index());
EXPECT_EQ(pointer.at(1).to_index(), 0);
}

TEST(JSONPointer_pointer, push_back_index_move) {
sourcemeta::jsontoolkit::Pointer pointer{"foo"};
pointer.push_back(0);
EXPECT_EQ(pointer.size(), 2);
EXPECT_TRUE(pointer.at(0).is_property());
EXPECT_EQ(pointer.at(0).to_property(), "foo");
EXPECT_TRUE(pointer.at(1).is_index());
EXPECT_EQ(pointer.at(1).to_index(), 0);
}
Loading