Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion DEPENDENCIES
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
vendorpull https://github.com/sourcemeta/vendorpull 1dcbac42809cf87cb5b045106b863e17ad84ba02
core https://github.com/sourcemeta/core f503133930d61dabbd59cded7e432a0addb11625
core https://github.com/sourcemeta/core 64f15b119964b3d5a1219eb797953d4698e69fa2
jsonbinpack https://github.com/sourcemeta/jsonbinpack ad91fc9e1129b5ee71983fdb709e3340632f6fbd
blaze https://github.com/sourcemeta/blaze e680badab795e9cdf30752f7dcfe77be708e05d7
ctrf https://github.com/ctrf-io/ctrf 93ea827d951390190171d37443bff169cf47c808
39 changes: 17 additions & 22 deletions src/command_fmt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,7 @@ auto sourcemeta::jsonschema::fmt(const sourcemeta::core::Options &options)
const auto display_path{stdin_path()};

std::string raw_stdin;
const auto parsed{read_from_stdin(&raw_stdin)};
if (parsed.yaml) {
throw YAMLInputError{"This command does not support YAML input files yet",
display_path};
}

const auto parsed{read_from_stdin(&raw_stdin, InputFormatting::Preserve)};
const auto &document{parsed.document};
const auto dialect{default_dialect(options, configuration)};
const auto is_test_document =
Expand All @@ -179,14 +174,15 @@ auto sourcemeta::jsonschema::fmt(const sourcemeta::core::Options &options)
if (options.contains("check")) {
std::ostringstream expected;
if (options.contains("keep-ordering")) {
sourcemeta::core::prettify(document, expected, indentation);
sourcemeta::jsonschema::write_schema(document, expected, indentation,
parsed.roundtrip);
} else {
auto copy = document;
sourcemeta::jsonschema::format_schema(copy, custom_resolver,
effective_dialect);
sourcemeta::core::prettify(copy, expected, indentation);
sourcemeta::jsonschema::write_schema(copy, expected, indentation,
parsed.roundtrip);
}
expected << "\n";

if (raw_stdin == expected.str()) {
const auto status =
Expand All @@ -200,14 +196,15 @@ auto sourcemeta::jsonschema::fmt(const sourcemeta::core::Options &options)
}
} else {
if (options.contains("keep-ordering")) {
sourcemeta::core::prettify(document, std::cout, indentation);
sourcemeta::jsonschema::write_schema(document, std::cout, indentation,
parsed.roundtrip);
} else {
auto copy = document;
sourcemeta::jsonschema::format_schema(copy, custom_resolver,
effective_dialect);
sourcemeta::core::prettify(copy, std::cout, indentation);
sourcemeta::jsonschema::write_schema(copy, std::cout, indentation,
parsed.roundtrip);
}
std::cout << "\n";
}
} catch (const sourcemeta::core::SchemaKeywordError &error) {
throw sourcemeta::core::FileError<sourcemeta::core::SchemaKeywordError>(
Expand Down Expand Up @@ -244,11 +241,6 @@ auto sourcemeta::jsonschema::fmt(const sourcemeta::core::Options &options)
};

const auto handle_file_entry = [&](const InputJSON &entry) {
if (entry.yaml) {
throw YAMLInputError{"This command does not support YAML input files yet",
entry.resolution_base};
}

if (entry.multidocument) {
throw MultiDocumentInputError{
"This command does not support input with multiple documents",
Expand Down Expand Up @@ -283,14 +275,15 @@ auto sourcemeta::jsonschema::fmt(const sourcemeta::core::Options &options)

std::ostringstream expected;
if (options.contains("keep-ordering")) {
sourcemeta::core::prettify(entry.second, expected, indentation);
sourcemeta::jsonschema::write_schema(entry.second, expected,
indentation, entry.roundtrip);
} else {
auto copy = entry.second;
sourcemeta::jsonschema::format_schema(copy, custom_resolver,
effective_dialect);
sourcemeta::core::prettify(copy, expected, indentation);
sourcemeta::jsonschema::write_schema(copy, expected, indentation,
entry.roundtrip);
}
expected << "\n";

const auto current{
sourcemeta::core::read_file_to_string(entry.resolution_base)};
Expand Down Expand Up @@ -356,7 +349,8 @@ auto sourcemeta::jsonschema::fmt(const sourcemeta::core::Options &options)
// When no positional arguments are given, default to for_each_json(options)
// which scans the current directory.
if (options.positional().empty()) {
for (const auto &entry : for_each_json(options)) {
for (const auto &entry :
for_each_json(options, InputFormatting::Preserve)) {
handle_file_entry(entry);
}
} else {
Expand All @@ -365,7 +359,8 @@ auto sourcemeta::jsonschema::fmt(const sourcemeta::core::Options &options)
if (arg == "-") {
handle_stdin();
} else {
for (const auto &entry : for_each_json({arg}, options)) {
for (const auto &entry :
for_each_json({arg}, options, InputFormatting::Preserve)) {
handle_file_entry(entry);
}
}
Expand Down
22 changes: 8 additions & 14 deletions src/command_lint.cc
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ auto sourcemeta::jsonschema::lint(const sourcemeta::core::Options &options)
const auto indentation{parse_indentation(options)};

if (options.contains("fix")) {
auto entries = for_each_json(options);
auto entries = for_each_json(options, InputFormatting::Preserve);
retag_openapi_stdin(entries);

for (const auto &entry : entries) {
Expand All @@ -485,12 +485,6 @@ auto sourcemeta::jsonschema::lint(const sourcemeta::core::Options &options)
const auto &custom_resolver{
resolver(options, options.contains("http"), dialect, configuration)};
LOG_VERBOSE(options) << "Linting: " << entry.first << "\n";
if (entry.yaml) {
throw YAMLInputError{
"The --fix option is not supported for YAML input files",
entry.resolution_base};
}

if (entry.multidocument) {
throw MultiDocumentInputError{
"The --fix option is not supported for input with multiple "
Expand Down Expand Up @@ -697,17 +691,17 @@ auto sourcemeta::jsonschema::lint(const sourcemeta::core::Options &options)
}
}

sourcemeta::core::prettify(copy, std::cout, indentation);
std::cout << "\n";
sourcemeta::jsonschema::write_schema(copy, std::cout, indentation,
entry.roundtrip);
} else if (format_output) {
if (!keep_ordering) {
sourcemeta::jsonschema::format_schema(copy, custom_resolver,
dialect);
}

std::ostringstream expected;
sourcemeta::core::prettify(copy, expected, indentation);
expected << "\n";
sourcemeta::jsonschema::write_schema(copy, expected, indentation,
entry.roundtrip);

const auto current{
sourcemeta::core::read_file_to_string(entry.resolution_base)};
Expand All @@ -719,9 +713,9 @@ auto sourcemeta::jsonschema::lint(const sourcemeta::core::Options &options)
} else if (copy != entry.second) {
sourcemeta::core::atomic_write_file(
entry.resolution_base,
[&copy, &indentation](std::ostream &stream) -> void {
sourcemeta::core::prettify(copy, stream, indentation);
stream << "\n";
[&copy, &indentation, &entry](std::ostream &stream) -> void {
sourcemeta::jsonschema::write_schema(copy, stream, indentation,
entry.roundtrip);
});
}
} else {
Expand Down
17 changes: 0 additions & 17 deletions src/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,19 +157,6 @@ class NotSchemaError : public std::runtime_error {
std::filesystem::path path_;
};

class YAMLInputError : public std::runtime_error {
public:
YAMLInputError(const std::string &message, std::filesystem::path path)
: std::runtime_error{message}, path_{std::move(path)} {}

[[nodiscard]] auto path() const noexcept -> const std::filesystem::path & {
return this->path_;
}

private:
std::filesystem::path path_;
};

class MultiDocumentInputError : public std::runtime_error {
public:
MultiDocumentInputError(const std::string &message,
Expand Down Expand Up @@ -1172,10 +1159,6 @@ inline auto try_catch(const sourcemeta::core::Options &options,
const auto is_json{options.contains("json")};
print_exception(is_json, error);
return EXIT_SCHEMA_INPUT_ERROR;
} catch (const YAMLInputError &error) {
const auto is_json{options.contains("json")};
print_exception(is_json, error);
return EXIT_NOT_SUPPORTED;
} catch (const MultiDocumentInputError &error) {
const auto is_json{options.contains("json")};
print_exception(is_json, error);
Expand Down
Loading
Loading