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 1 commit
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 with JSON output for all frames

```sh
jsonschema frame path/to/my/schema.json --json
```
133 changes: 80 additions & 53 deletions src/command_frame.cc
Original file line number Diff line number Diff line change
@@ -1,71 +1,98 @@
#include <sourcemeta/jsontoolkit/json.h>
#include <sourcemeta/jsontoolkit/jsonschema.h>

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, {})};
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>")
Copy link
Member

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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;
}
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