Skip to content

Commit

Permalink
Implement proper custom resolution support
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <[email protected]>
  • Loading branch information
jviotti committed May 28, 2024
1 parent 55c6daa commit fd6710f
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 48 deletions.
2 changes: 1 addition & 1 deletion DEPENDENCIES
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
vendorpull https://github.com/sourcemeta/vendorpull dea311b5bfb53b6926a4140267959ae334d3ecf4
noa https://github.com/sourcemeta/noa 5ff4024902642afc9cc2f9a9e02ae9dff9d15d4f
jsontoolkit https://github.com/sourcemeta/jsontoolkit 162faf07f453c88849f8259bdfa8a6d74c2f279b
jsontoolkit https://github.com/sourcemeta/jsontoolkit dc7e1e21853f2b26cac572d5dc4c3dc50eeda935
41 changes: 12 additions & 29 deletions src/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,41 +119,24 @@ auto pretty_evaluate_callback(

auto resolver(const std::map<std::string, std::vector<std::string>> &options)
-> sourcemeta::jsontoolkit::SchemaResolver {
if (!options.contains("resolve") && !options.contains("r")) {
return sourcemeta::jsontoolkit::official_resolver;
}
sourcemeta::jsontoolkit::MapSchemaResolver dynamic_resolver{
sourcemeta::jsontoolkit::official_resolver};

std::map<std::string, sourcemeta::jsontoolkit::JSON> schemas;
const std::string option{options.contains("resolve") ? "resolve" : "r"};
for (const auto &schema_path : options.at(option)) {
const auto schema{sourcemeta::jsontoolkit::from_file(schema_path)};
// TODO: Use the current resolver as its building up
const auto id{sourcemeta::jsontoolkit::id(
schema, sourcemeta::jsontoolkit::official_resolver)
.get()};
if (!id.has_value()) {
std::ostringstream error;
error << "Cannot determine the identifier of the schema: " << schema_path;
throw std::runtime_error(error.str());
if (options.contains("resolve")) {
for (const auto &schema_path : options.at("resolve")) {
log_verbose(options) << "Loading schema: " << schema_path << "\n";
dynamic_resolver.add(sourcemeta::jsontoolkit::from_file(schema_path));
}

// TODO: Throw if we are overriding with a duplicate id
// TODO: We need to frame to add subschemas too?
schemas.insert({id.value(), schema});
log_verbose(options) << "Loading schema: " << schema_path << "\n";
}

return [schemas](std::string_view identifier)
-> std::future<std::optional<sourcemeta::jsontoolkit::JSON>> {
const std::string string_identifier{identifier};
if (schemas.contains(string_identifier)) {
std::promise<std::optional<sourcemeta::jsontoolkit::JSON>> promise;
promise.set_value(schemas.at(string_identifier));
return promise.get_future();
if (options.contains("r")) {
for (const auto &schema_path : options.at("r")) {
log_verbose(options) << "Loading schema: " << schema_path << "\n";
dynamic_resolver.add(sourcemeta::jsontoolkit::from_file(schema_path));
}
}

return sourcemeta::jsontoolkit::official_resolver(identifier);
};
return dynamic_resolver;
}

auto log_verbose(const std::map<std::string, std::vector<std::string>> &options)
Expand Down
1 change: 1 addition & 0 deletions vendor/jsontoolkit/Makefile.uk

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/jsontoolkit/src/jsonschema/CMakeLists.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 19 additions & 16 deletions vendor/jsontoolkit/src/jsonschema/reference.cc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

88 changes: 88 additions & 0 deletions vendor/jsontoolkit/src/jsonschema/resolver.cc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fd6710f

Please sign in to comment.