-
Notifications
You must be signed in to change notification settings - Fork 9
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 1 commit
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 |
---|---|---|
@@ -1,71 +1,98 @@ | ||
#include <sourcemeta/jsontoolkit/json.h> | ||
#include <sourcemeta/jsontoolkit/jsonschema.h> | ||
|
||
#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, {})}; | ||
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())}; | ||
|
||
sourcemeta::jsontoolkit::ReferenceFrame frame; | ||
sourcemeta::jsontoolkit::ReferenceMap references; | ||
sourcemeta::jsontoolkit::frame(schema, frame, references, | ||
sourcemeta::jsontoolkit::default_schema_walker, | ||
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 << " "; | ||
} | ||
// Function to pad each line of a string except the first line | ||
std::string padLines(const std::string &input, int padding) { | ||
std::istringstream inputStream(input); | ||
std::ostringstream outputStream; | ||
std::string line; | ||
std::string pad(padding, ' '); | ||
|
||
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 << " "; | ||
// Handle the first line | ||
if (std::getline(inputStream, line)) { | ||
outputStream << line << "\n"; | ||
} | ||
|
||
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"; | ||
while (std::getline(inputStream, line)) { | ||
outputStream << pad << line << "\n"; | ||
} | ||
std::cout << "\n"; | ||
std::cout << " Destination : " << entry.destination << "\n"; | ||
|
||
if (entry.base.has_value()) { | ||
std::cout << " - (w/o fragment) : " << entry.base.value() << "\n"; | ||
return outputStream.str(); | ||
} | ||
|
||
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()) | ||
}; | ||
|
||
sourcemeta::jsontoolkit::ReferenceFrame frame; | ||
sourcemeta::jsontoolkit::ReferenceMap references; | ||
sourcemeta::jsontoolkit::frame(schema, frame, references, | ||
sourcemeta::jsontoolkit::default_schema_walker, | ||
resolver(options)) | ||
.wait(); | ||
|
||
bool outputJson = options.contains("json") || options.contains("-j"); | ||
|
||
for (const auto &[key, entry] : frame) { | ||
std::cout << "(LOCATION) URI: "; | ||
std::cout << key.second << "\n"; | ||
std::cout << " Schema : " << 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. i.e. all of these things you should be transforming to JSON. In the end you want the command to print only one big JSON document with all the information at once 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. Ohh, I got it. I thought it might be this way, just in sometime... |
||
<< "\n"; | ||
std::cout << " Pointer :"; | ||
if (!entry.pointer.empty()) { | ||
std::cout << " "; | ||
} | ||
|
||
sourcemeta::jsontoolkit::stringify(entry.pointer, std::cout); | ||
if (outputJson) { | ||
std::cout << "\n"; | ||
std::cout << " JSON : "; | ||
std::ostringstream jsonStream; | ||
sourcemeta::jsontoolkit::prettify(sourcemeta::jsontoolkit::get(schema, entry.pointer), jsonStream); | ||
std::cout << padLines(jsonStream.str(), 23); | ||
} | ||
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 : "; | ||
if (pointer.first == sourcemeta::jsontoolkit::ReferenceType::Dynamic) { | ||
std::cout << "Dynamic"; | ||
} else { | ||
std::cout << "Static"; | ||
} | ||
std::cout << "\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; | ||
return EXIT_SUCCESS; | ||
} |
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?