Skip to content

Commit

Permalink
Generalize transformer .at() into a schema getter (#484)
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 52a2363 commit 29165db
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,8 @@ class SOURCEMETA_JSONTOOLKIT_JSONSCHEMA_EXPORT SchemaTransformer {
/// Construct a transformer given a schema
SchemaTransformer(JSON &schema);

/// Proxy to sourcemeta::jsontoolkit::JSON::at
auto at(const JSON::Array::size_type index) const -> const JSON &;
/// Proxy to sourcemeta::jsontoolkit::JSON::at
auto at(const JSON::String &key) const -> const JSON &;
/// Get the underlying schema
auto schema() const -> const JSON &;
/// Proxy to sourcemeta::jsontoolkit::JSON::into_object
auto into_object() -> void;
/// Proxy to sourcemeta::jsontoolkit::JSON::erase
Expand Down
11 changes: 2 additions & 9 deletions src/jsonschema/transformer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,9 @@ sourcemeta::jsontoolkit::SchemaTransformer::SchemaTransformer(
sourcemeta::jsontoolkit::JSON &schema)
: data{schema} {}

auto sourcemeta::jsontoolkit::SchemaTransformer::at(
const sourcemeta::jsontoolkit::JSON::Array::size_type index) const
auto sourcemeta::jsontoolkit::SchemaTransformer::schema() const
-> const sourcemeta::jsontoolkit::JSON & {
return this->data.at(index);
}

auto sourcemeta::jsontoolkit::SchemaTransformer::at(
const sourcemeta::jsontoolkit::JSON::String &key) const
-> const sourcemeta::jsontoolkit::JSON & {
return this->data.at(key);
return this->data;
}

auto sourcemeta::jsontoolkit::SchemaTransformer::into_object() -> void {
Expand Down
11 changes: 11 additions & 0 deletions test/jsonschema/jsonschema_transformer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@
#include <sourcemeta/jsontoolkit/jsonpointer.h>
#include <sourcemeta/jsontoolkit/jsonschema.h>

TEST(JSONSchema_transformer, schema) {
sourcemeta::jsontoolkit::JSON document =
sourcemeta::jsontoolkit::parse("[ 1, 2, 3 ]");
sourcemeta::jsontoolkit::SchemaTransformer transformer{document};
const sourcemeta::jsontoolkit::JSON expected =
sourcemeta::jsontoolkit::parse("[ 1, 2, 3 ]");
EXPECT_EQ(transformer.schema(), document);
EXPECT_EQ(transformer.schema(), expected);
EXPECT_EQ(expected, document);
}

TEST(JSONSchema_transformer, into_object) {
sourcemeta::jsontoolkit::JSON document =
sourcemeta::jsontoolkit::parse("[ 1, 2, 3 ]");
Expand Down

0 comments on commit 29165db

Please sign in to comment.