-
-
Notifications
You must be signed in to change notification settings - Fork 18
Support basic JSON-LD annotations over instances #900
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| #include <sourcemeta/blaze/foundation.h> | ||
| #include <sourcemeta/core/io.h> | ||
| #include <sourcemeta/core/json.h> | ||
| #include <sourcemeta/core/jsonpointer.h> | ||
|
|
||
| #include <sourcemeta/blaze/compiler.h> | ||
| #include <sourcemeta/blaze/evaluator.h> | ||
| #include <sourcemeta/blaze/output.h> | ||
|
|
||
| #include <cstdlib> // EXIT_SUCCESS, EXIT_FAILURE | ||
| #include <exception> // std::exception | ||
| #include <iostream> // std::cerr, std::cout | ||
| #include <unordered_set> // std::unordered_set | ||
| #include <variant> // std::get, std::holds_alternative | ||
|
|
||
| // A convenience script to evaluate an instance against a JSON Schema whose | ||
| // atoms carry JSON-LD annotations, and print the resulting expanded JSON-LD, | ||
| // the schema validation errors, or the JSON-LD resolution error. | ||
|
|
||
| static auto run(const char *schema_path, const char *instance_path) -> int; | ||
|
|
||
| auto main(int argc, char *argv[]) -> int { | ||
| if (argc != 3) { | ||
| std::cerr << "Usage: " << argv[0] << " <schema.json> <instance.json>\n"; | ||
| return EXIT_FAILURE; | ||
| } | ||
|
|
||
| try { | ||
| return run(argv[1], argv[2]); | ||
| } catch (const std::exception &error) { | ||
| std::cerr << "Error: " << error.what() << "\n"; | ||
| return EXIT_FAILURE; | ||
| } | ||
| } | ||
|
|
||
| static auto run(const char *schema_path, const char *instance_path) -> int { | ||
| const auto schema{sourcemeta::core::read_json(schema_path)}; | ||
| const auto instance{sourcemeta::core::read_json(instance_path)}; | ||
|
|
||
| sourcemeta::blaze::Tweaks tweaks; | ||
| tweaks.annotations = std::unordered_set<sourcemeta::core::JSON::StringView>{ | ||
| "x-jsonld-id", "x-jsonld-type"}; | ||
| const auto schema_template{sourcemeta::blaze::compile( | ||
| schema, sourcemeta::blaze::schema_walker, | ||
| sourcemeta::blaze::schema_resolver, | ||
| sourcemeta::blaze::default_schema_compiler, | ||
| sourcemeta::blaze::Mode::FastValidation, "", "", "", tweaks)}; | ||
|
|
||
| sourcemeta::blaze::Evaluator evaluator; | ||
| const auto outcome{ | ||
| sourcemeta::blaze::jsonld(evaluator, schema_template, instance)}; | ||
|
|
||
| if (std::holds_alternative<sourcemeta::core::JSON>(outcome)) { | ||
| sourcemeta::core::prettify(std::get<sourcemeta::core::JSON>(outcome), | ||
| std::cout); | ||
| std::cout << "\n"; | ||
| return EXIT_SUCCESS; | ||
| } | ||
|
|
||
| if (std::holds_alternative<sourcemeta::blaze::JSONLDInvalid>(outcome)) { | ||
| std::cerr << "The instance does not conform to the schema:\n"; | ||
| for (const auto &entry : | ||
| std::get<sourcemeta::blaze::JSONLDInvalid>(outcome)) { | ||
| std::cerr << " - " << entry.message << "\n at instance location \"" | ||
| << sourcemeta::core::to_string(entry.instance_location) | ||
| << "\"\n at evaluate path \"" | ||
| << sourcemeta::core::to_string(entry.evaluate_path) << "\"\n"; | ||
| } | ||
| return EXIT_FAILURE; | ||
| } | ||
|
|
||
| const auto &error{ | ||
| std::get<sourcemeta::blaze::JSONLDResolutionError>(outcome)}; | ||
| std::cerr << "The instance conforms but could not be resolved into JSON-LD:\n" | ||
| << " - " << error.message << "\n at instance location \"" | ||
| << sourcemeta::core::to_string(error.instance_location) << "\"\n"; | ||
| return EXIT_FAILURE; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| #ifndef SOURCEMETA_BLAZE_OUTPUT_JSONLD_H_ | ||
| #define SOURCEMETA_BLAZE_OUTPUT_JSONLD_H_ | ||
|
|
||
| #ifndef SOURCEMETA_BLAZE_OUTPUT_EXPORT | ||
| #include <sourcemeta/blaze/output_export.h> | ||
| #endif | ||
|
|
||
| #include <sourcemeta/core/json.h> | ||
| #include <sourcemeta/core/jsonpointer.h> | ||
|
|
||
| #include <sourcemeta/blaze/evaluator.h> | ||
| #include <sourcemeta/blaze/output_simple.h> | ||
|
|
||
| #include <cstdint> // std::uint8_t | ||
| #include <string> // std::string | ||
| #include <variant> // std::variant | ||
| #include <vector> // std::vector | ||
|
|
||
| namespace sourcemeta::blaze { | ||
|
|
||
| /// @ingroup output | ||
| /// The descriptor facet that a JSON-LD resolution error is about | ||
| enum class JSONLDFacet : std::uint8_t { Type, Predicate }; | ||
|
|
||
| /// @ingroup output | ||
| /// The instance conforms but one of its JSON-LD annotations cannot be resolved | ||
| /// into a consistent mapping. Reported as a single failure at the instance | ||
| /// location where resolution stopped, distinct from a schema validation error. | ||
| struct JSONLDResolutionError { | ||
| sourcemeta::core::Pointer instance_location; | ||
| JSONLDFacet facet; | ||
| std::string message; | ||
| }; | ||
|
|
||
| /// @ingroup output | ||
| /// The instance does not conform to the schema, so no JSON-LD is produced. | ||
| /// These are the schema validation errors. | ||
| using JSONLDInvalid = std::vector<SimpleOutput::Entry>; | ||
|
|
||
| /// @ingroup output | ||
| /// The tri-state outcome of a JSON-LD evaluation: the expanded document (as | ||
| /// expanded JSON-LD), schema validation errors, or a JSON-LD resolution error. | ||
| using JSONLDOutcome = | ||
| std::variant<sourcemeta::core::JSON, JSONLDInvalid, JSONLDResolutionError>; | ||
|
|
||
| /// @ingroup output | ||
| /// | ||
| /// Evaluate an instance against a JSON Schema whose atoms carry JSON-LD | ||
| /// annotations and, on success, return the instance promoted to expanded | ||
| /// JSON-LD. The schema must have been compiled with annotation collection | ||
| /// enabled for its JSON-LD keywords, as shown below. For example: | ||
| /// | ||
| /// ```cpp | ||
| /// #include <sourcemeta/blaze/compiler.h> | ||
| /// #include <sourcemeta/blaze/evaluator.h> | ||
| /// #include <sourcemeta/blaze/output.h> | ||
| /// | ||
| /// #include <sourcemeta/core/json.h> | ||
| /// #include <sourcemeta/blaze/foundation.h> | ||
| /// | ||
| /// #include <unordered_set> | ||
| /// #include <variant> | ||
| /// | ||
| /// const sourcemeta::core::JSON schema = | ||
| /// sourcemeta::core::parse_json(R"JSON({ | ||
| /// "$schema": "https://json-schema.org/draft/2020-12/schema", | ||
| /// "type": "object", | ||
| /// "x-jsonld-type": "https://schema.org/Person", | ||
| /// "properties": { | ||
| /// "name": { "type": "string", "x-jsonld-id": "https://schema.org/name" } | ||
| /// } | ||
| /// })JSON"); | ||
| /// | ||
| /// sourcemeta::blaze::Tweaks tweaks; | ||
| /// tweaks.annotations = std::unordered_set<sourcemeta::core::JSON::StringView>{ | ||
| /// "x-jsonld-id", "x-jsonld-type"}; | ||
| /// | ||
| /// const auto schema_template{sourcemeta::blaze::compile( | ||
| /// schema, sourcemeta::blaze::schema_walker, | ||
| /// sourcemeta::blaze::schema_resolver, | ||
| /// sourcemeta::blaze::default_schema_compiler, | ||
| /// sourcemeta::blaze::Mode::FastValidation, "", "", "", tweaks)}; | ||
| /// | ||
| /// const sourcemeta::core::JSON instance{ | ||
| /// sourcemeta::core::parse_json(R"JSON({ "name": "Ada" })JSON")}; | ||
| /// | ||
| /// sourcemeta::blaze::Evaluator evaluator; | ||
| /// const auto outcome{ | ||
| /// sourcemeta::blaze::jsonld(evaluator, schema_template, instance)}; | ||
| /// ``` | ||
| auto SOURCEMETA_BLAZE_OUTPUT_EXPORT | ||
| jsonld(Evaluator &evaluator, const Template &schema, | ||
| const sourcemeta::core::JSON &instance) -> JSONLDOutcome; | ||
|
|
||
| } // namespace sourcemeta::blaze | ||
|
|
||
| #endif |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
src/output/CMakeLists.txt:18:
sourcemeta_blaze_outputlinkssourcemeta::core::jsonldas PRIVATE, which can break downstream links in static builds when callingblaze::jsonld(e.g.contrib_jsonldonly linksblaze::output). Consider making this dependency PUBLIC (or explicitly linkingcore::jsonldfrom dependents) so consumers get the required transitive link interface.Severity: medium
🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.