Skip to content

Commit

Permalink
Generalize transformer .replace() to take pointers (#487)
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <[email protected]>
  • Loading branch information
jviotti authored Feb 7, 2024
1 parent ff17d4d commit 40c8b28
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,27 @@ class SOURCEMETA_JSONTOOLKIT_JSONSCHEMA_EXPORT SchemaTransformer {

/// Get the underlying schema
auto schema() const -> const JSON &;
/// Replace a schema with another value
auto replace(const JSON &value) -> void;
/// Replace a schema with another value
auto replace(JSON &&value) -> void;
/// Trace the operations applied to the schema
auto traces() const -> const std::vector<SchemaTransformerOperation> &;

/// Replace a subschema with another value
auto replace(const Pointer &path, const JSON &value) -> void;
/// Replace a subschema with another value
auto replace(const Pointer &path, JSON &&value) -> void;

/// Proxy to sourcemeta::jsontoolkit::JSON::erase
auto erase(const JSON::String &key) -> void;
/// Proxy to sourcemeta::jsontoolkit::JSON::assign
auto assign(const JSON::String &key, const JSON &value) -> void;
/// Proxy to sourcemeta::jsontoolkit::JSON::assign
auto assign(const JSON::String &key, JSON &&value) -> void;
/// Trace the operations applied to the schema
auto traces() const -> const std::vector<SchemaTransformerOperation> &;

// For convenience

/// Replace a schema with another value
auto replace(const JSON &value) -> void;
/// Replace a schema with another value
auto replace(JSON &&value) -> void;

private:
JSON &data;
Expand Down
24 changes: 18 additions & 6 deletions src/jsonschema/transformer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,29 @@ auto sourcemeta::jsontoolkit::SchemaTransformer::schema() const
}

auto sourcemeta::jsontoolkit::SchemaTransformer::replace(
const sourcemeta::jsontoolkit::Pointer &path,
const sourcemeta::jsontoolkit::JSON &value) -> void {
this->data = value;
this->operations.push_back(SchemaTransformerOperationReplace{
sourcemeta::jsontoolkit::empty_pointer});
// TODO: Check that the path exists with an assert
sourcemeta::jsontoolkit::set(this->data, path, value);
this->operations.push_back(SchemaTransformerOperationReplace{path});
}

auto sourcemeta::jsontoolkit::SchemaTransformer::replace(
const sourcemeta::jsontoolkit::Pointer &path,
sourcemeta::jsontoolkit::JSON &&value) -> void {
this->data = std::move(value);
this->operations.push_back(SchemaTransformerOperationReplace{
sourcemeta::jsontoolkit::empty_pointer});
// TODO: Check that the path exists with an assert
sourcemeta::jsontoolkit::set(this->data, path, std::move(value));
this->operations.push_back(SchemaTransformerOperationReplace{path});
}

auto sourcemeta::jsontoolkit::SchemaTransformer::replace(
const sourcemeta::jsontoolkit::JSON &value) -> void {
this->replace(sourcemeta::jsontoolkit::empty_pointer, value);
}

auto sourcemeta::jsontoolkit::SchemaTransformer::replace(
sourcemeta::jsontoolkit::JSON &&value) -> void {
this->replace(sourcemeta::jsontoolkit::empty_pointer, std::move(value));
}

auto sourcemeta::jsontoolkit::SchemaTransformer::erase(
Expand Down
20 changes: 20 additions & 0 deletions test/jsonschema/jsonschema_transformer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,26 @@ TEST(JSONSchema_transformer, replace_with_empty_object) {
.pointer.empty());
}

TEST(JSONSchema_transformer, replace_subschema_with_empty_object) {
sourcemeta::jsontoolkit::JSON document =
sourcemeta::jsontoolkit::parse("[ 1, 2, 3 ]");
sourcemeta::jsontoolkit::SchemaTransformer transformer{document};
transformer.replace({1}, sourcemeta::jsontoolkit::JSON::make_object());

const sourcemeta::jsontoolkit::JSON expected =
sourcemeta::jsontoolkit::parse("[ 1, {}, 3 ]");
const auto &traces{transformer.traces()};

EXPECT_EQ(document, expected);
EXPECT_EQ(traces.size(), 1);

using namespace sourcemeta::jsontoolkit;
EXPECT_TRUE(
std::holds_alternative<SchemaTransformerOperationReplace>(traces.at(0)));
EXPECT_EQ(std::get<SchemaTransformerOperationReplace>(traces.at(0)).pointer,
sourcemeta::jsontoolkit::Pointer({1}));
}

TEST(JSONSchema_transformer, assign) {
sourcemeta::jsontoolkit::JSON document =
sourcemeta::jsontoolkit::parse("{ \"foo\": 1 }");
Expand Down

0 comments on commit 40c8b28

Please sign in to comment.