Skip to content

Suffix PointerTemplate conditionals with keyword names #1603

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

Merged
merged 1 commit into from
Apr 8, 2025
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
Suffix PointerTemplate conditionals with keyword names
Signed-off-by: Juan Cruz Viotti <[email protected]>
  • Loading branch information
jviotti committed Apr 8, 2025
commit 99fa3c68bbb5afabc44f338d03f39b0c6c9ff3ba
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <algorithm> // std::copy, std::all_of
#include <cassert> // assert
#include <iterator> // std::back_inserter
#include <optional> // std::optional, std::nullopt
#include <variant> // std::variant, std::holds_alternative, std::get
#include <vector> // std::vector

Expand All @@ -21,6 +22,7 @@ template <typename PointerT> class GenericPointerTemplate {
struct Condition {
auto operator==(const Condition &) const noexcept -> bool = default;
auto operator<(const Condition &) const noexcept -> bool { return false; }
std::optional<typename PointerT::Value::String> suffix = std::nullopt;
};
struct Negation {
auto operator==(const Negation &) const noexcept -> bool = default;
Expand Down
6 changes: 6 additions & 0 deletions src/core/jsonpointer/stringify.h
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,12 @@ auto stringify(const PointerT &pointer,
stream.put(internal::token_pointer_slash<CharT>);
stream.put(internal::token_pointer_tilde<CharT>);
stream.put('?');
const auto &value{std::get<typename PointerT::Condition>(token)};
if (value.suffix.has_value()) {
stream.write(value.suffix->c_str(),
static_cast<std::streamsize>(value.suffix->size()));
}

stream.put(internal::token_pointer_tilde<CharT>);
} else if (std::holds_alternative<typename PointerT::Negation>(token)) {
stream.put(internal::token_pointer_slash<CharT>);
Expand Down
36 changes: 18 additions & 18 deletions src/core/jsonschema/walker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ auto walk(const std::optional<sourcemeta::core::Pointer> &parent,
new_pointer.emplace_back(pair.first);
auto new_instance_location{instance_location};
new_instance_location.emplace_back(
sourcemeta::core::PointerTemplate::Condition{});
sourcemeta::core::PointerTemplate::Condition{pair.first});
new_instance_location.emplace_back(
sourcemeta::core::PointerTemplate::Wildcard::Property);
walk(pointer, new_pointer, new_instance_location,
{sourcemeta::core::PointerTemplate::Condition{},
{sourcemeta::core::PointerTemplate::Condition{pair.first},
sourcemeta::core::PointerTemplate::Wildcard::Property},
subschemas, pair.second, walker, resolver, new_dialect, type,
level + 1, orphan);
Expand Down Expand Up @@ -99,11 +99,11 @@ auto walk(const std::optional<sourcemeta::core::Pointer> &parent,
new_pointer.emplace_back(pair.first);
auto new_instance_location{instance_location};
new_instance_location.emplace_back(
sourcemeta::core::PointerTemplate::Condition{});
sourcemeta::core::PointerTemplate::Condition{pair.first});
new_instance_location.emplace_back(
sourcemeta::core::PointerTemplate::Wildcard::Item);
walk(pointer, new_pointer, new_instance_location,
{sourcemeta::core::PointerTemplate::Condition{},
{sourcemeta::core::PointerTemplate::Condition{pair.first},
sourcemeta::core::PointerTemplate::Wildcard::Item},
subschemas, pair.second, walker, resolver, new_dialect, type,
level + 1, orphan);
Expand Down Expand Up @@ -144,11 +144,11 @@ auto walk(const std::optional<sourcemeta::core::Pointer> &parent,
new_pointer.emplace_back(pair.first);
auto new_instance_location{instance_location};
new_instance_location.emplace_back(
sourcemeta::core::PointerTemplate::Condition{});
sourcemeta::core::PointerTemplate::Condition{pair.first});
walk(pointer, new_pointer, new_instance_location,
{sourcemeta::core::PointerTemplate::Condition{}}, subschemas,
pair.second, walker, resolver, new_dialect, type, level + 1,
orphan);
{sourcemeta::core::PointerTemplate::Condition{pair.first}},
subschemas, pair.second, walker, resolver, new_dialect, type,
level + 1, orphan);
} break;

case sourcemeta::core::SchemaKeywordType::ApplicatorElementsTraverseItem:
Expand Down Expand Up @@ -189,11 +189,11 @@ auto walk(const std::optional<sourcemeta::core::Pointer> &parent,
new_pointer.emplace_back(index);
auto new_instance_location{instance_location};
new_instance_location.emplace_back(
sourcemeta::core::PointerTemplate::Condition{});
sourcemeta::core::PointerTemplate::Condition{pair.first});
walk(pointer, new_pointer, new_instance_location,
{sourcemeta::core::PointerTemplate::Condition{}}, subschemas,
pair.second.at(index), walker, resolver, new_dialect, type,
level + 1, orphan);
{sourcemeta::core::PointerTemplate::Condition{pair.first}},
subschemas, pair.second.at(index), walker, resolver,
new_dialect, type, level + 1, orphan);
}
}

Expand All @@ -208,11 +208,11 @@ auto walk(const std::optional<sourcemeta::core::Pointer> &parent,
new_pointer.emplace_back(index);
auto new_instance_location{instance_location};
new_instance_location.emplace_back(
sourcemeta::core::PointerTemplate::Condition{});
sourcemeta::core::PointerTemplate::Condition{pair.first});
new_instance_location.emplace_back(
sourcemeta::core::PointerTemplate::Negation{});
walk(pointer, new_pointer, new_instance_location,
{sourcemeta::core::PointerTemplate::Condition{},
{sourcemeta::core::PointerTemplate::Condition{pair.first},
sourcemeta::core::PointerTemplate::Negation{}},
subschemas, pair.second.at(index), walker, resolver,
new_dialect, type, level + 1, orphan);
Expand Down Expand Up @@ -263,11 +263,11 @@ auto walk(const std::optional<sourcemeta::core::Pointer> &parent,
new_pointer.emplace_back(subpair.first);
auto new_instance_location{instance_location};
new_instance_location.emplace_back(
sourcemeta::core::PointerTemplate::Condition{});
sourcemeta::core::PointerTemplate::Condition{pair.first});
walk(pointer, new_pointer, new_instance_location,
{sourcemeta::core::PointerTemplate::Condition{}}, subschemas,
subpair.second, walker, resolver, new_dialect, type, level + 1,
orphan);
{sourcemeta::core::PointerTemplate::Condition{pair.first}},
subschemas, subpair.second, walker, resolver, new_dialect,
type, level + 1, orphan);
}
}

Expand Down
32 changes: 32 additions & 0 deletions test/jsonpointer/jsonpointer_template_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,26 @@ TEST(JSONPointer_template, equality_with_key_wildcard_false) {
EXPECT_NE(left, right);
}

TEST(JSONPointer_template, equality_with_condition_suffix_true) {
sourcemeta::core::PointerTemplate left;
left.emplace_back(sourcemeta::core::PointerTemplate::Condition{"foo"});

sourcemeta::core::PointerTemplate right;
right.emplace_back(sourcemeta::core::PointerTemplate::Condition{"foo"});

EXPECT_EQ(left, right);
}

TEST(JSONPointer_template, equality_with_condition_suffix_false) {
sourcemeta::core::PointerTemplate left;
left.emplace_back(sourcemeta::core::PointerTemplate::Condition{"foo"});

sourcemeta::core::PointerTemplate right;
right.emplace_back(sourcemeta::core::PointerTemplate::Condition{"bar"});

EXPECT_NE(left, right);
}

TEST(JSONPointer_template, equality_with_condition_wildcard_true) {
const sourcemeta::core::Pointer prefix{"foo", "bar"};
const sourcemeta::core::Pointer suffix{"baz"};
Expand Down Expand Up @@ -336,6 +356,18 @@ TEST(JSONPointer_template, stringify_condition) {
EXPECT_EQ(stream.str(), "/foo/bar/~?~");
}

TEST(JSONPointer_template, stringify_condition_with_suffix) {
const sourcemeta::core::Pointer prefix{"foo", "bar"};

sourcemeta::core::PointerTemplate pointer{prefix};
pointer.emplace_back(sourcemeta::core::PointerTemplate::Condition{"hello"});

std::ostringstream stream;
sourcemeta::core::stringify(pointer, stream);

EXPECT_EQ(stream.str(), "/foo/bar/~?hello~");
}

TEST(JSONPointer_template, stringify_negation) {
const sourcemeta::core::Pointer prefix{"foo", "bar"};

Expand Down
Loading