Skip to content

Commit

Permalink
Overload FlatFileSchemaResolver::add to manually take file contents
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <[email protected]>
  • Loading branch information
jviotti committed Jan 15, 2025
1 parent c7d91b8 commit 3df64e6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ class SOURCEMETA_JSONTOOLKIT_JSONSCHEMA_EXPORT FlatFileSchemaResolver {

/// Register a schema to the flat file resolver, returning the detected
/// identifier for the schema
auto add(const std::filesystem::path &path, const JSON &schema,
const std::optional<std::string> &default_dialect = std::nullopt,
const std::optional<std::string> &default_id = std::nullopt)
-> const std::string &;

/// Read and register a schema to the flat file resolver, returning the
/// detected identifier for the schema
auto add(const std::filesystem::path &path,
const std::optional<std::string> &default_dialect = std::nullopt,
const std::optional<std::string> &default_id = std::nullopt)
Expand Down
11 changes: 9 additions & 2 deletions src/jsonschema/resolver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,10 @@ static auto to_lowercase(const std::string_view input) -> std::string {
}

auto FlatFileSchemaResolver::add(
const std::filesystem::path &path,
const std::filesystem::path &path, const JSON &schema,
const std::optional<std::string> &default_dialect,
const std::optional<std::string> &default_id) -> const std::string & {
const auto canonical{std::filesystem::canonical(path)};
const auto schema{sourcemeta::jsontoolkit::from_file(canonical)};
assert(sourcemeta::jsontoolkit::is_schema(schema));
const auto identifier{sourcemeta::jsontoolkit::identify(
schema, *this, IdentificationStrategy::Loose, default_dialect,
Expand Down Expand Up @@ -123,6 +122,14 @@ auto FlatFileSchemaResolver::add(
return result.first->first;
}

auto FlatFileSchemaResolver::add(
const std::filesystem::path &path,
const std::optional<std::string> &default_dialect,
const std::optional<std::string> &default_id) -> const std::string & {
return this->add(path, sourcemeta::jsontoolkit::from_file(path),
default_dialect, default_id);
}

auto FlatFileSchemaResolver::reidentify(const std::string &schema,
const std::string &new_identifier)
-> void {
Expand Down
13 changes: 13 additions & 0 deletions test/jsonschema/jsonschema_flat_file_resolver_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ TEST(JSONSchema_FlatFileSchemaResolver, single_schema) {
sourcemeta::jsontoolkit::from_file(schema_path));
}

TEST(JSONSchema_FlatFileSchemaResolver, single_schema_manual_read) {
sourcemeta::jsontoolkit::FlatFileSchemaResolver resolver;
const auto schema_path{std::filesystem::path{SCHEMAS_PATH} /
"2020-12-id.json"};
const auto &identifier{resolver.add(
schema_path, sourcemeta::jsontoolkit::from_file(schema_path))};
EXPECT_EQ(identifier, "https://www.sourcemeta.com/2020-12-id.json");
EXPECT_TRUE(
resolver("https://www.sourcemeta.com/2020-12-id.json").has_value());
EXPECT_EQ(resolver("https://www.sourcemeta.com/2020-12-id.json").value(),
sourcemeta::jsontoolkit::from_file(schema_path));
}

TEST(JSONSchema_FlatFileSchemaResolver, single_schema_with_default_dialect) {
sourcemeta::jsontoolkit::FlatFileSchemaResolver resolver;
const auto schema_path{std::filesystem::path{SCHEMAS_PATH} / "only-id.json"};
Expand Down

0 comments on commit 3df64e6

Please sign in to comment.