Skip to content

Commit

Permalink
Format using nmake build
Browse files Browse the repository at this point in the history
  • Loading branch information
suprith-hub committed Jul 18, 2024
1 parent c68f7a8 commit e442a17
Showing 1 changed file with 49 additions and 27 deletions.
76 changes: 49 additions & 27 deletions src/command_frame.cc
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#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 <cstdlib> // EXIT_SUCCESS, EXIT_FAILURE
#include <iostream> // std::cout
#include <sstream> // std::ostringstream

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

static auto enum_to_string(const sourcemeta::jsontoolkit::ReferenceEntryType type) -> std::string {
static auto enum_to_string(
const sourcemeta::jsontoolkit::ReferenceEntryType type) -> std::string {
switch (type) {
case sourcemeta::jsontoolkit::ReferenceEntryType::Resource:
return "resource";
Expand All @@ -21,11 +22,13 @@ static auto enum_to_string(const sourcemeta::jsontoolkit::ReferenceEntryType typ
}
}

auto intelligence::jsonschema::cli::frame(const std::span<const std::string> &arguments) -> int {
auto intelligence::jsonschema::cli::frame(
const std::span<const std::string> &arguments) -> int {
const auto options{parse_options(arguments, {})};
if (options.at("").size() < 1) {
std::cerr << "error: This command expects a path to a schema. For example:\n\n"
<< " jsonschema frame path/to/schema.json\n";
std::cerr
<< "error: This command expects a path to a schema. For example:\n\n"
<< " jsonschema frame path/to/schema.json\n";
return EXIT_FAILURE;
}

Expand All @@ -35,7 +38,8 @@ auto intelligence::jsonschema::cli::frame(const std::span<const std::string> &ar
sourcemeta::jsontoolkit::ReferenceFrame frame;
sourcemeta::jsontoolkit::ReferenceMap references;
sourcemeta::jsontoolkit::frame(schema, frame, references,
sourcemeta::jsontoolkit::default_schema_walker, resolver(options))
sourcemeta::jsontoolkit::default_schema_walker,
resolver(options))
.wait();

const auto output_json = options.contains("json") || options.contains("j");
Expand All @@ -47,53 +51,68 @@ auto intelligence::jsonschema::cli::frame(const std::span<const std::string> &ar
for (const auto &[key, entry] : frame) {
auto frame_entry = sourcemeta::jsontoolkit::JSON::make_object();
if (entry.root.has_value()) {
frame_entry.assign("root", sourcemeta::jsontoolkit::JSON{entry.root.value()});
frame_entry.assign("root",
sourcemeta::jsontoolkit::JSON{entry.root.value()});
} else {
frame_entry.assign("root", sourcemeta::jsontoolkit::JSON{nullptr});
}
std::ostringstream pointer_stream;
sourcemeta::jsontoolkit::stringify(entry.pointer, pointer_stream);
frame_entry.assign("pointer", sourcemeta::jsontoolkit::JSON{pointer_stream.str()});
frame_entry.assign("pointer",
sourcemeta::jsontoolkit::JSON{pointer_stream.str()});
frame_entry.assign("base", sourcemeta::jsontoolkit::JSON{entry.base});
frame_entry.assign("type", sourcemeta::jsontoolkit::JSON{enum_to_string(entry.type)});
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});
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});
output_json_object.assign("frames",
sourcemeta::jsontoolkit::JSON{frame_json});

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});
"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()) {
ref_entry.assign("base", sourcemeta::jsontoolkit::JSON{entry.base.value()});
ref_entry.assign("base",
sourcemeta::jsontoolkit::JSON{entry.base.value()});
} else {
ref_entry.assign("base", sourcemeta::jsontoolkit::JSON{nullptr});
}
if (entry.fragment.has_value()) {
ref_entry.assign("fragment", sourcemeta::jsontoolkit::JSON{entry.fragment.value()});
ref_entry.assign("fragment",
sourcemeta::jsontoolkit::JSON{entry.fragment.value()});
} else {
ref_entry.assign("fragment", sourcemeta::jsontoolkit::JSON{nullptr});
}
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});
references_json.assign(ref_entry_stream.str(),
sourcemeta::jsontoolkit::JSON{ref_entry});
}
output_json_object.assign("references", sourcemeta::jsontoolkit::JSON{references_json});
output_json_object.assign("references",
sourcemeta::jsontoolkit::JSON{references_json});

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 << " Schema : "
<< entry.root.value_or("<ANONYMOUS>") << "\n";
std::cout << " Pointer :";
if (!entry.pointer.empty()) {
std::cout << " ";
Expand All @@ -115,8 +134,10 @@ auto intelligence::jsonschema::cli::frame(const std::span<const std::string> &ar
sourcemeta::jsontoolkit::stringify(pointer.second, std::cout);
std::cout << "\n";
std::cout << " Type : "
<< (pointer.first == sourcemeta::jsontoolkit::ReferenceType::Dynamic ? "Dynamic"
: "Static")
<< (pointer.first ==
sourcemeta::jsontoolkit::ReferenceType::Dynamic
? "Dynamic"
: "Static")
<< "\n";
std::cout << " Destination : " << entry.destination << "\n";

Expand All @@ -125,7 +146,8 @@ auto intelligence::jsonschema::cli::frame(const std::span<const std::string> &ar
}

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

0 comments on commit e442a17

Please sign in to comment.