-
Notifications
You must be signed in to change notification settings - Fork 7
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
Changes from 3 commits
2cbce79
d2bc412
ae2f7f5
8ac7f94
14bd5f3
0a65b35
636594a
13a5f36
1eea8bf
3650b6f
838558f
c68f7a8
e442a17
d2e175d
09282c0
6ab1671
47f5ef8
8e5dbd7
fbf706f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,13 +3,26 @@ | |
|
||
#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())}; | ||
|
@@ -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>")}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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()}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here you can make it just |
||
} | ||
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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,4 +70,4 @@ cat << 'EOF' > "$TMP/expected.txt" | |
- (fragment) : /$defs/string | ||
EOF | ||
|
||
diff "$TMP/result.txt" "$TMP/expected.txt" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks like an unintended change There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" |
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", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here too. Maybe just call it |
||
"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" |
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.
Why was this line deleted?