diff --git a/README.markdown b/README.markdown index 62905fde..a4050c6c 100644 --- a/README.markdown +++ b/README.markdown @@ -147,6 +147,7 @@ documentation: - [`jsonschema lint`](./docs/lint.markdown) - [`jsonschema bundle`](./docs/bundle.markdown) (for inlining remote references in a schema) - [`jsonschema frame`](./docs/frame.markdown) (for debugging references) +- [`jsonschema compile`](./docs/compile.markdown) (for internal debugging) Coming Soon ----------- diff --git a/docs/compile.markdown b/docs/compile.markdown new file mode 100644 index 00000000..e53cf5ac --- /dev/null +++ b/docs/compile.markdown @@ -0,0 +1,46 @@ +Compile +======= + +> [!WARNING] +> Only JSON Schema Draft 4, Draft 6, and Draft 7 are supported at this point in +> time. We have plans to support *every* dialect of JSON Schema from Draft 0 to +> Draft 2020-12 soon. + +```sh +jsonschema compile +``` + +Evaluating a JSON Schema is a complex process with a non-trivial associated +performance overhead. For faster evaluation, we internally pre-process a given +JSON Schema into a compiled low-level form. This command allows you to obtain +the schema compilation result as a separate step. + +**At least for now, this command is mainly for internal debugging purposes. As +an end-user, you are better served by the [`validate`](./validate.markdown) +command, which internally compiles schemas.** + +Examples +-------- + +For example, consider the following simple schema: + +```json +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "properties": { + "foo": { "type": "string" } + } +} +``` + +The compilation process will result in something like this: + +``` +TODO +``` + +### Compile a JSON Schema + +```sh +jsonschema compile path/to/my/schema.json +``` diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0ef07f9f..aca9dcb8 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -4,6 +4,7 @@ add_executable(jsonschema_cli command_fmt.cc command_frame.cc command_bundle.cc + command_compile.cc command_test.cc command_lint.cc command_metaschema.cc diff --git a/src/command.h b/src/command.h index 75877e7e..0289e717 100644 --- a/src/command.h +++ b/src/command.h @@ -8,6 +8,7 @@ namespace intelligence::jsonschema::cli { auto fmt(const std::span &arguments) -> int; auto frame(const std::span &arguments) -> int; auto bundle(const std::span &arguments) -> int; +auto compile(const std::span &arguments) -> int; auto test(const std::span &arguments) -> int; auto lint(const std::span &arguments) -> int; auto validate(const std::span &arguments) -> int; diff --git a/src/command_compile.cc b/src/command_compile.cc new file mode 100644 index 00000000..6b931fe1 --- /dev/null +++ b/src/command_compile.cc @@ -0,0 +1,33 @@ +#include +#include + +#include // EXIT_SUCCESS +#include // std::cout, std::endl + +#include "command.h" +#include "utils.h" + +auto intelligence::jsonschema::cli::compile( + const std::span &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 compile path/to/schema.json\n"; + return EXIT_FAILURE; + } + + const auto schema{sourcemeta::jsontoolkit::from_file(options.at("").front())}; + + const auto compiled_schema{sourcemeta::jsontoolkit::compile( + schema, sourcemeta::jsontoolkit::default_schema_walker, + resolver(options, options.contains("h") || options.contains("http")), + sourcemeta::jsontoolkit::default_schema_compiler)}; + + const sourcemeta::jsontoolkit::JSON result{ + sourcemeta::jsontoolkit::to_json(compiled_schema)}; + sourcemeta::jsontoolkit::prettify(result, std::cout); + std::cout << std::endl; + return EXIT_SUCCESS; +} diff --git a/src/main.cc b/src/main.cc index 91cdbf21..3d5edcb5 100644 --- a/src/main.cc +++ b/src/main.cc @@ -58,6 +58,11 @@ Global Options: Frame a schema in-place, displaying schema locations and references in a human-readable manner. + compile + + Pre-process a JSON Schema into JSON Toolkit's low-level JSON-based + compiled form for faster evaluation. + For more documentation, visit https://github.com/Intelligence-AI/jsonschema )EOF"}; @@ -69,6 +74,8 @@ auto jsonschema_main(const std::string &program, const std::string &command, return intelligence::jsonschema::cli::frame(arguments); } else if (command == "bundle") { return intelligence::jsonschema::cli::bundle(arguments); + } else if (command == "compile") { + return intelligence::jsonschema::cli::compile(arguments); } else if (command == "lint") { return intelligence::jsonschema::cli::lint(arguments); } else if (command == "validate") {