Skip to content
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

Add --json flag in frame to provide JSON output support #116

Merged
merged 19 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion docs/frame.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Framing
=======

```sh
jsonschema frame <schema.json> [--verbose/-v]
jsonschema frame <schema.json> [--json/-j] [--verbose/-v]
```

To evaluate a schema, an implementation will first scan it to determine the
Expand Down Expand Up @@ -58,3 +58,9 @@ reference:
```sh
jsonschema frame path/to/my/schema.json
```

### Frame a JSON Schema and output result as a JSON document

```sh
jsonschema frame path/to/my/schema.json --json
```
126 changes: 86 additions & 40 deletions src/command_frame.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,26 @@

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this line deleted?

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

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

auto intelligence::jsonschema::cli::frame(
const std::span<const std::string> &arguments) -> int {
const auto options{parse_options(arguments, {})};
static auto enum_to_string(const sourcemeta::jsontoolkit::ReferenceEntryType type) -> std::string {
switch (type) {
case sourcemeta::jsontoolkit::ReferenceEntryType::Resource:
return "Resource";
suprith-hub marked this conversation as resolved.
Show resolved Hide resolved
case sourcemeta::jsontoolkit::ReferenceEntryType::Anchor:
return "Anchor";
case sourcemeta::jsontoolkit::ReferenceEntryType::Pointer:
return "Pointer";
default:
return "Unknown";
}
}

auto intelligence::jsonschema::cli::frame(const std::span<const std::string> &arguments) -> int {
const auto options{parse_options(arguments, {"json", "j"})};
CLI_ENSURE(!options.at("").empty(), "You must pass a JSON Schema as input")
const sourcemeta::jsontoolkit::JSON schema{
sourcemeta::jsontoolkit::from_file(options.at("").front())};
Expand All @@ -21,51 +34,84 @@ auto intelligence::jsonschema::cli::frame(
resolver(options))
.wait();

for (const auto &[key, entry] : frame) {
std::cout << "(LOCATION) URI: ";
std::cout << key.second << "\n";
std::cout << " Schema : " << entry.root.value_or("<ANONYMOUS>")
<< "\n";
std::cout << " Pointer :";
if (!entry.pointer.empty()) {
std::cout << " ";
}
const auto output_json = options.contains("json") || options.contains("j");
if (output_json) {
auto output_json_object = sourcemeta::jsontoolkit::JSON::make_object();
auto frame_json = sourcemeta::jsontoolkit::JSON::make_object();
auto references_json = sourcemeta::jsontoolkit::JSON::make_object();

sourcemeta::jsontoolkit::stringify(entry.pointer, std::cout);
std::cout << "\n";
std::cout << " Base URI : " << entry.base << "\n";
std::cout << " Relative Pointer :";
if (!entry.relative_pointer.empty()) {
std::cout << " ";
for (const auto &[key, entry] : frame) {
auto frame_entry = sourcemeta::jsontoolkit::JSON::make_object();
frame_entry.assign("schema", sourcemeta::jsontoolkit::JSON{entry.root.value_or("<ANONYMOUS>")});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the root is not defined, can you make it null rather than setting it to a string with the value of <ANONYMOUS>?

std::ostringstream pointer_stream;
sourcemeta::jsontoolkit::stringify(entry.pointer, pointer_stream);
frame_entry.assign("pointer", sourcemeta::jsontoolkit::JSON{pointer_stream.str()});
frame_entry.assign("baseURI", sourcemeta::jsontoolkit::JSON{entry.base});
frame_entry.assign("type", sourcemeta::jsontoolkit::JSON{enum_to_string(entry.type)});
std::ostringstream reference_stream;
sourcemeta::jsontoolkit::stringify(entry.relative_pointer, reference_stream);
frame_entry.assign("relativePointer", sourcemeta::jsontoolkit::JSON{reference_stream.str()});
frame_entry.assign("dialect", sourcemeta::jsontoolkit::JSON{entry.dialect});
frame_json.assign(key.second, sourcemeta::jsontoolkit::JSON{frame_entry});
}
output_json_object.assign("frames", sourcemeta::jsontoolkit::JSON{frame_json});

sourcemeta::jsontoolkit::stringify(entry.relative_pointer, std::cout);
std::cout << "\n";
std::cout << " Dialect : " << entry.dialect << "\n";
}

for (const auto &[pointer, entry] : references) {
std::cout << "(REFERENCE) URI: ";
sourcemeta::jsontoolkit::stringify(pointer.second, std::cout);
std::cout << "\n";

std::cout << " Type : ";
if (pointer.first == sourcemeta::jsontoolkit::ReferenceType::Dynamic) {
std::cout << "Dynamic";
} else {
std::cout << "Static";
for (const auto &[pointer, entry] : references) {
auto ref_entry = sourcemeta::jsontoolkit::JSON::make_object();
ref_entry.assign("type",
sourcemeta::jsontoolkit::JSON{pointer.first == sourcemeta::jsontoolkit::ReferenceType::Dynamic ? "Dynamic" : "Static"});
ref_entry.assign("destination", sourcemeta::jsontoolkit::JSON{entry.destination});
if (entry.base.has_value()) {
suprith-hub marked this conversation as resolved.
Show resolved Hide resolved
ref_entry.assign("fragmentBaseURI", sourcemeta::jsontoolkit::JSON{entry.base.value()});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you can make it just base. I think in general keeping the JSON properties consistent with the names in the struct will make it easier to maintain

}
if (entry.fragment.has_value()) {
ref_entry.assign("fragment", sourcemeta::jsontoolkit::JSON{entry.fragment.value()});
}
std::ostringstream ref_entry_stream;
sourcemeta::jsontoolkit::stringify(pointer.second, ref_entry_stream);
references_json.assign(ref_entry_stream.str(), sourcemeta::jsontoolkit::JSON{ref_entry});
}
std::cout << "\n";
std::cout << " Destination : " << entry.destination << "\n";
output_json_object.assign("references", sourcemeta::jsontoolkit::JSON{references_json});

if (entry.base.has_value()) {
std::cout << " - (w/o fragment) : " << entry.base.value() << "\n";
std::ostringstream print_stream;
sourcemeta::jsontoolkit::prettify(output_json_object, print_stream);
std::cout << print_stream.str() << std::endl;
} else {
for (const auto &[key, entry] : frame) {
std::cout << "(LOCATION) URI: " << key.second << "\n";
std::cout << " Schema : " << entry.root.value_or("<ANONYMOUS>") << "\n";
std::cout << " Pointer :";
if (!entry.pointer.empty()) {
std::cout << " ";
}
sourcemeta::jsontoolkit::stringify(entry.pointer, std::cout);
std::cout << "\n";
std::cout << " Base URI : " << entry.base << "\n";
std::cout << " Relative Pointer :";
if (!entry.relative_pointer.empty()) {
std::cout << " ";
}
sourcemeta::jsontoolkit::stringify(entry.relative_pointer, std::cout);
std::cout << "\n";
std::cout << " Dialect : " << entry.dialect << "\n";
}

if (entry.fragment.has_value()) {
std::cout << " - (fragment) : " << entry.fragment.value() << "\n";
for (const auto &[pointer, entry] : references) {
std::cout << "(REFERENCE) URI: ";
sourcemeta::jsontoolkit::stringify(pointer.second, std::cout);
std::cout << "\n";
std::cout << " Type : " << (pointer.first == sourcemeta::jsontoolkit::ReferenceType::Dynamic ? "Dynamic" : "Static") << "\n";
std::cout << " Destination : " << entry.destination << "\n";

if (entry.base.has_value()) {
std::cout << " - (w/o fragment) : " << entry.base.value() << "\n";
}

if (entry.fragment.has_value()) {
std::cout << " - (fragment) : " << entry.fragment.value() << "\n";
}
}
}

return EXIT_SUCCESS;
}
}
2 changes: 1 addition & 1 deletion src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Global Options:
Perform JSON Schema Bundling on a schema to inline remote references,
printing the result to standard output.

frame <schema.json>
frame <schema.json> [--json/-j]

Frame a schema in-place, displaying schema locations and references
in a human-readable manner.
Expand Down
2 changes: 1 addition & 1 deletion test/frame.sh
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ cat << 'EOF' > "$TMP/expected.txt"
- (fragment) : /$defs/string
EOF

diff "$TMP/result.txt" "$TMP/expected.txt"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like an unintended change

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't have any difference between the original version, still I do not know why it says there is a diff

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the final new line character at the end of the file. We saw it in other PRs. It's likely a misconfiguration on your editor. Google how to make sure your editor includes an end-of-file line ending

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the symbol that GitHub shows on the diff:

Screenshot 2024-07-11 at 11 54 41 PM

Copy link
Contributor Author

@suprith-hub suprith-hub Jul 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shet! sorry I got it, I think myself I removed the EOF line, I will correct it

diff "$TMP/result.txt" "$TMP/expected.txt"
98 changes: 98 additions & 0 deletions test/frame_json_output.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#!/bin/sh

set -o errexit
set -o nounset

TMP="$(mktemp -d)"
clean() { rm -rf "$TMP"; }
trap clean EXIT

cat << 'EOF' > "$TMP/schema.json"
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com",
"$ref": "#/$defs/string",
"$defs": {
"string": { "type": "string" }
}
}
EOF

# Test with --json flag
"$1" frame "$TMP/schema.json" --json > "$TMP/result_json.txt"

# Test with -j flag
"$1" frame "$TMP/schema.json" -j > "$TMP/result.json"
suprith-hub marked this conversation as resolved.
Show resolved Hide resolved

cat << 'EOF' > "$TMP/expected_json.txt"
{
"frames": {
"https://example.com": {
"baseURI": "https://example.com",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here too. Maybe just call it base?

"dialect": "https://json-schema.org/draft/2020-12/schema",
"pointer": "",
"relativePointer": "",
"schema": "https://example.com",
suprith-hub marked this conversation as resolved.
Show resolved Hide resolved
"type": "Resource"
},
"https://example.com#/$defs": {
"baseURI": "https://example.com",
"dialect": "https://json-schema.org/draft/2020-12/schema",
"pointer": "/$defs",
"relativePointer": "/$defs",
"schema": "https://example.com",
"type": "Pointer"
},
"https://example.com#/$defs/string": {
"baseURI": "https://example.com",
"dialect": "https://json-schema.org/draft/2020-12/schema",
"pointer": "/$defs/string",
"relativePointer": "/$defs/string",
"schema": "https://example.com",
"type": "Pointer"
},
"https://example.com#/$defs/string/type": {
"baseURI": "https://example.com",
"dialect": "https://json-schema.org/draft/2020-12/schema",
"pointer": "/$defs/string/type",
"relativePointer": "/$defs/string/type",
"schema": "https://example.com",
"type": "Pointer"
},
"https://example.com#/$id": {
"baseURI": "https://example.com",
"dialect": "https://json-schema.org/draft/2020-12/schema",
"pointer": "/$id",
"relativePointer": "/$id",
"schema": "https://example.com",
"type": "Pointer"
},
"https://example.com#/$ref": {
"baseURI": "https://example.com",
"dialect": "https://json-schema.org/draft/2020-12/schema",
"pointer": "/$ref",
"relativePointer": "/$ref",
"schema": "https://example.com",
"type": "Pointer"
},
"https://example.com#/$schema": {
"baseURI": "https://example.com",
"dialect": "https://json-schema.org/draft/2020-12/schema",
"pointer": "/$schema",
"relativePointer": "/$schema",
"schema": "https://example.com",
"type": "Pointer"
}
},
"references": {
"/$ref": {
"destination": "https://example.com#/$defs/string",
"fragment": "/$defs/string",
"fragmentBaseURI": "https://example.com",
"type": "Static"
}
}
}
EOF

diff "$TMP/result_json.txt" "$TMP/expected_json.txt"
Loading