Skip to content

Commit

Permalink
[WIP] Implement an identify command to print schema ids
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <[email protected]>
  • Loading branch information
jviotti committed Jul 25, 2024
1 parent 17fe2a4 commit 3405644
Show file tree
Hide file tree
Showing 13 changed files with 310 additions and 93 deletions.
2 changes: 1 addition & 1 deletion DEPENDENCIES
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
vendorpull https://github.com/sourcemeta/vendorpull dea311b5bfb53b6926a4140267959ae334d3ecf4
noa https://github.com/sourcemeta/noa 7e26abce7a4e31e86a16ef2851702a56773ca527
jsontoolkit https://github.com/sourcemeta/jsontoolkit 8e4d59fb0d75351175337bdcff7fe6caf4fe7096
jsontoolkit https://github.com/sourcemeta/jsontoolkit a3765c8038ba4271e55318a677f6366bdaa7b805
hydra https://github.com/sourcemeta/hydra 3c53d3fdef79e9ba603d48470a508cc45472a0dc
3 changes: 2 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ add_executable(jsonschema_cli
command_test.cc
command_lint.cc
command_metaschema.cc
command_validate.cc)
command_validate.cc
command_identify.cc)

noa_add_default_options(PRIVATE jsonschema_cli)
set_target_properties(jsonschema_cli PROPERTIES OUTPUT_NAME jsonschema)
Expand Down
1 change: 1 addition & 0 deletions src/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ auto test(const std::span<const std::string> &arguments) -> int;
auto lint(const std::span<const std::string> &arguments) -> int;
auto validate(const std::span<const std::string> &arguments) -> int;
auto metaschema(const std::span<const std::string> &arguments) -> int;
auto identify(const std::span<const std::string> &arguments) -> int;
} // namespace intelligence::jsonschema::cli

#endif
51 changes: 51 additions & 0 deletions src/command_identify.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <sourcemeta/jsontoolkit/json.h>
#include <sourcemeta/jsontoolkit/jsonschema.h>

#include <cstdlib> // EXIT_SUCCESS, EXIT_FAILURE
#include <iostream> // std::cout

#include "command.h"
#include "utils.h"

auto intelligence::jsonschema::cli::identify(
const std::span<const std::string> &arguments) -> int {
const auto options{parse_options(arguments, {})};
if (options.at("").size() < 1) {
std::cerr
<< "error: This command expects a path to a schema. For example:\n\n"
<< " jsonschema identify path/to/schema.json\n";
return EXIT_FAILURE;
}

const sourcemeta::jsontoolkit::JSON schema{
sourcemeta::jsontoolkit::from_file(options.at("").front())};

// Just to print a nice warning
try {
const auto base_dialect{
sourcemeta::jsontoolkit::base_dialect(
schema, sourcemeta::jsontoolkit::official_resolver)
.get()};
if (!base_dialect.has_value()) {
std::cerr << "warning: Cannot determine the base dialect of the schema, "
"but will attempt to guess\n";
}
} catch (const sourcemeta::jsontoolkit::SchemaResolutionError &) {
std::cerr << "warning: Cannot determine the base dialect of the schema, "
"but will attempt to guess\n";
}

const auto identifier{
sourcemeta::jsontoolkit::id(
schema, sourcemeta::jsontoolkit::official_resolver,
sourcemeta::jsontoolkit::IdentificationStrategy::Loose)
.get()};

if (!identifier.has_value()) {
log_verbose(options) << "error: Could not determine schema identifier\n";
return EXIT_FAILURE;
}

std::cout << identifier.value() << "\n";
return EXIT_SUCCESS;
}
7 changes: 7 additions & 0 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ Global Options:
Pre-process a JSON Schema into JSON Toolkit's low-level JSON-based
compiled form for faster evaluation.
identify <schema.json> [--relative-from/-f <uri>]
Print the URI of the given schema to standard output, optionally
relative to a given base URI.
For more documentation, visit https://github.com/Intelligence-AI/jsonschema
)EOF"};

Expand All @@ -85,6 +90,8 @@ auto jsonschema_main(const std::string &program, const std::string &command,
return intelligence::jsonschema::cli::metaschema(arguments);
} else if (command == "test") {
return intelligence::jsonschema::cli::test(arguments);
} else if (command == "identify") {
return intelligence::jsonschema::cli::identify(arguments);
} else {
std::cout << "JSON Schema CLI - v"
<< intelligence::jsonschema::cli::PROJECT_VERSION << "\n";
Expand Down
21 changes: 1 addition & 20 deletions test/compile/pass.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,7 @@ cat << 'EOF' > "$TMP/expected.json"
"type": "instance",
"location": ""
},
"condition": [
{
"category": "assertion",
"type": "type-strict",
"value": {
"category": "value",
"type": "type",
"value": "object"
},
"absoluteKeywordLocation": "#/properties",
"relativeSchemaLocation": "",
"relativeInstanceLocation": "",
"target": {
"category": "target",
"type": "instance",
"location": ""
},
"condition": []
}
],
"condition": [],
"children": [
{
"category": "internal",
Expand Down
5 changes: 4 additions & 1 deletion vendor/jsontoolkit/src/jsonschema/compile.cc

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/compile_evaluate.cc

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.

46 changes: 43 additions & 3 deletions vendor/jsontoolkit/src/jsonschema/jsonschema.cc

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

14 changes: 14 additions & 0 deletions vendor/jsontoolkit/src/uri/include/sourcemeta/jsontoolkit/uri.h

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

Loading

0 comments on commit 3405644

Please sign in to comment.