Skip to content
Open
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
44 changes: 26 additions & 18 deletions src/command_fmt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,26 @@ auto sourcemeta::jsonschema::fmt(const sourcemeta::core::Options &options)
std::vector<std::string> failed_files;
const auto indentation{parse_indentation(options)};
for (const auto &entry : for_each_json(options)) {
if (entry.from_stdin) {
throw std::runtime_error{
"This command does not support reading from standard input"};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not?

}

if (entry.yaml) {
throw YAMLInputError{"This command does not support YAML input files yet",
entry.first};
entry.resolution_base};
}

if (options.contains("check")) {
LOG_VERBOSE(options) << "Checking: " << entry.first.string() << "\n";
LOG_VERBOSE(options) << "Checking: " << entry.first << "\n";
} else {
LOG_VERBOSE(options) << "Formatting: " << entry.first.string() << "\n";
LOG_VERBOSE(options) << "Formatting: " << entry.first << "\n";
}

try {
const auto configuration_path{find_configuration(entry.first)};
const auto &configuration{
read_configuration(options, configuration_path, entry.first)};
const auto configuration_path{find_configuration(entry.resolution_base)};
const auto &configuration{read_configuration(options, configuration_path,
entry.resolution_base)};
const auto dialect{default_dialect(options, configuration)};
const auto &custom_resolver{
resolver(options, options.contains("http"), dialect, configuration)};
Expand All @@ -51,43 +56,46 @@ auto sourcemeta::jsonschema::fmt(const sourcemeta::core::Options &options)
}
expected << "\n";

std::ifstream current_stream{entry.first};
std::ifstream current_stream{entry.resolution_base};
std::ostringstream current;
current << current_stream.rdbuf();

if (options.contains("check")) {
if (current.str() == expected.str()) {
LOG_VERBOSE(options) << "ok: " << entry.first.string() << "\n";
LOG_VERBOSE(options) << "ok: " << entry.first << "\n";
} else if (output_json) {
failed_files.push_back(entry.first.string());
failed_files.push_back(entry.first);
result = false;
} else {
std::cerr << "fail: " << entry.first.string() << "\n";
std::cerr << "fail: " << entry.first << "\n";
result = false;
}
} else {
if (current.str() != expected.str()) {
std::ofstream output{entry.first};
std::ofstream output{entry.resolution_base};
output << expected.str();
}
}
} catch (const sourcemeta::core::SchemaKeywordError &error) {
throw FileError<sourcemeta::core::SchemaKeywordError>(entry.first, error);
throw FileError<sourcemeta::core::SchemaKeywordError>(
entry.resolution_base, error);
} catch (const sourcemeta::core::SchemaFrameError &error) {
throw FileError<sourcemeta::core::SchemaFrameError>(entry.first, error);
throw FileError<sourcemeta::core::SchemaFrameError>(entry.resolution_base,
error);
} catch (const sourcemeta::core::SchemaRelativeMetaschemaResolutionError
&error) {
throw FileError<
sourcemeta::core::SchemaRelativeMetaschemaResolutionError>(
entry.first, error);
entry.resolution_base, error);
} catch (const sourcemeta::core::SchemaResolutionError &error) {
throw FileError<sourcemeta::core::SchemaResolutionError>(entry.first,
error);
throw FileError<sourcemeta::core::SchemaResolutionError>(
entry.resolution_base, error);
} catch (const sourcemeta::core::SchemaUnknownBaseDialectError &) {
throw FileError<sourcemeta::core::SchemaUnknownBaseDialectError>(
entry.first);
entry.resolution_base);
} catch (const sourcemeta::core::SchemaError &error) {
throw FileError<sourcemeta::core::SchemaError>(entry.first, error.what());
throw FileError<sourcemeta::core::SchemaError>(entry.resolution_base,
error.what());
}
}

Expand Down
64 changes: 32 additions & 32 deletions src/command_lint.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ static auto get_lint_callback(sourcemeta::core::JSON &errors_array,
if (output_json) {
auto error_obj = sourcemeta::core::JSON::make_object();

error_obj.assign("path", sourcemeta::core::JSON{entry.first.string()});
error_obj.assign("path", sourcemeta::core::JSON{entry.first});
error_obj.assign("id", sourcemeta::core::JSON{name});
error_obj.assign("message", sourcemeta::core::JSON{message});
error_obj.assign("description",
Expand All @@ -106,7 +106,7 @@ static auto get_lint_callback(sourcemeta::core::JSON &errors_array,

errors_array.push_back(error_obj);
} else {
std::cout << std::filesystem::relative(entry.first).string();
std::cout << std::filesystem::relative(entry.resolution_base).string();
if (position.has_value()) {
std::cout << ":";
std::cout << std::get<0>(position.value());
Expand Down Expand Up @@ -215,18 +215,18 @@ auto sourcemeta::jsonschema::lint(const sourcemeta::core::Options &options)

if (options.contains("fix")) {
for (const auto &entry : for_each_json(options)) {
const auto configuration_path{find_configuration(entry.first)};
const auto &configuration{
read_configuration(options, configuration_path, entry.first)};
const auto configuration_path{find_configuration(entry.resolution_base)};
const auto &configuration{read_configuration(options, configuration_path,
entry.resolution_base)};
const auto dialect{default_dialect(options, configuration)};

const auto &custom_resolver{
resolver(options, options.contains("http"), dialect, configuration)};
LOG_VERBOSE(options) << "Linting: " << entry.first.string() << "\n";
LOG_VERBOSE(options) << "Linting: " << entry.first << "\n";
if (entry.yaml) {
throw YAMLInputError{
"The --fix option is not supported for YAML input files",
entry.first};
entry.resolution_base};
}

auto copy = entry.second;
Expand All @@ -239,7 +239,7 @@ auto sourcemeta::jsonschema::lint(const sourcemeta::core::Options &options)
copy, sourcemeta::core::schema_walker, custom_resolver,
get_lint_callback(errors_array, entry, output_json, true,
printed_progress),
dialect, sourcemeta::jsonschema::default_id(entry.first),
dialect, sourcemeta::jsonschema::default_id(entry),
EXCLUDE_KEYWORD);
if (printed_progress) {
std::cerr << "\n";
Expand All @@ -257,7 +257,7 @@ auto sourcemeta::jsonschema::lint(const sourcemeta::core::Options &options)
std::cerr << "\n";
}

throw LintAutoFixError{error.what(), entry.first,
throw LintAutoFixError{error.what(), entry.resolution_base,
error.location()};
} catch (
const sourcemeta::core::SchemaBrokenReferenceError &error) {
Expand All @@ -268,7 +268,7 @@ auto sourcemeta::jsonschema::lint(const sourcemeta::core::Options &options)
throw LintAutoFixError{
"Could not autofix the schema without breaking its internal "
"references",
entry.first, error.location()};
entry.resolution_base, error.location()};
} catch (
const sourcemeta::blaze::CompilerReferenceTargetNotSchemaError
&error) {
Expand All @@ -278,35 +278,35 @@ auto sourcemeta::jsonschema::lint(const sourcemeta::core::Options &options)

throw FileError<
sourcemeta::blaze::CompilerReferenceTargetNotSchemaError>(
entry.first, error);
entry.resolution_base, error);
} catch (const sourcemeta::core::SchemaKeywordError &error) {
if (printed_progress) {
std::cerr << "\n";
}

throw FileError<sourcemeta::core::SchemaKeywordError>(entry.first,
error);
throw FileError<sourcemeta::core::SchemaKeywordError>(
entry.resolution_base, error);
} catch (const sourcemeta::core::SchemaFrameError &error) {
if (printed_progress) {
std::cerr << "\n";
}

throw FileError<sourcemeta::core::SchemaFrameError>(entry.first,
error);
throw FileError<sourcemeta::core::SchemaFrameError>(
entry.resolution_base, error);
} catch (const sourcemeta::core::SchemaUnknownBaseDialectError &) {
if (printed_progress) {
std::cerr << "\n";
}

throw FileError<sourcemeta::core::SchemaUnknownBaseDialectError>(
entry.first);
entry.resolution_base);
} catch (const sourcemeta::core::SchemaResolutionError &error) {
if (printed_progress) {
std::cerr << "\n";
}

throw FileError<sourcemeta::core::SchemaResolutionError>(
entry.first, error);
entry.resolution_base, error);
} catch (...) {
if (printed_progress) {
std::cerr << "\n";
Expand All @@ -331,16 +331,16 @@ auto sourcemeta::jsonschema::lint(const sourcemeta::core::Options &options)
sourcemeta::core::prettify(copy, expected, indentation);
expected << "\n";

std::ifstream current_stream{entry.first};
std::ifstream current_stream{entry.resolution_base};
std::ostringstream current;
current << current_stream.rdbuf();

if (current.str() != expected.str()) {
std::ofstream output{entry.first};
std::ofstream output{entry.resolution_base};
output << expected.str();
}
} else if (copy != entry.second) {
std::ofstream output{entry.first};
std::ofstream output{entry.resolution_base};
sourcemeta::core::prettify(copy, output, indentation);
output << "\n";
}
Expand All @@ -351,13 +351,13 @@ auto sourcemeta::jsonschema::lint(const sourcemeta::core::Options &options)
}
} else {
for (const auto &entry : for_each_json(options)) {
const auto configuration_path{find_configuration(entry.first)};
const auto &configuration{
read_configuration(options, configuration_path, entry.first)};
const auto configuration_path{find_configuration(entry.resolution_base)};
const auto &configuration{read_configuration(options, configuration_path,
entry.resolution_base)};
const auto dialect{default_dialect(options, configuration)};
const auto &custom_resolver{
resolver(options, options.contains("http"), dialect, configuration)};
LOG_VERBOSE(options) << "Linting: " << entry.first.string() << "\n";
LOG_VERBOSE(options) << "Linting: " << entry.first << "\n";

bool printed_progress{false};
const auto wrapper_result =
Expand All @@ -368,7 +368,7 @@ auto sourcemeta::jsonschema::lint(const sourcemeta::core::Options &options)
custom_resolver,
get_lint_callback(errors_array, entry, output_json, false,
printed_progress),
dialect, sourcemeta::jsonschema::default_id(entry.first),
dialect, sourcemeta::jsonschema::default_id(entry),
EXCLUDE_KEYWORD);
scores.emplace_back(subresult.second);
if (subresult.first) {
Expand All @@ -382,19 +382,19 @@ auto sourcemeta::jsonschema::lint(const sourcemeta::core::Options &options)
&error) {
throw FileError<
sourcemeta::blaze::CompilerReferenceTargetNotSchemaError>(
entry.first, error);
entry.resolution_base, error);
} catch (const sourcemeta::core::SchemaKeywordError &error) {
throw FileError<sourcemeta::core::SchemaKeywordError>(entry.first,
error);
throw FileError<sourcemeta::core::SchemaKeywordError>(
entry.resolution_base, error);
} catch (const sourcemeta::core::SchemaFrameError &error) {
throw FileError<sourcemeta::core::SchemaFrameError>(entry.first,
error);
throw FileError<sourcemeta::core::SchemaFrameError>(
entry.resolution_base, error);
} catch (const sourcemeta::core::SchemaUnknownBaseDialectError &) {
throw FileError<sourcemeta::core::SchemaUnknownBaseDialectError>(
entry.first);
entry.resolution_base);
} catch (const sourcemeta::core::SchemaResolutionError &error) {
throw FileError<sourcemeta::core::SchemaResolutionError>(
entry.first, error);
entry.resolution_base, error);
}
});

Expand Down
26 changes: 11 additions & 15 deletions src/command_metaschema.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ auto sourcemeta::jsonschema::metaschema(

for (const auto &entry : for_each_json(options)) {
if (!sourcemeta::core::is_schema(entry.second)) {
throw NotSchemaError{entry.first};
throw NotSchemaError{entry.resolution_base};
}

const auto configuration_path{find_configuration(entry.first)};
const auto configuration_path{find_configuration(entry.resolution_base)};
const auto &configuration{
read_configuration(options, configuration_path, entry.first)};
read_configuration(options, configuration_path, entry.resolution_base)};
const auto default_dialect_option{default_dialect(options, configuration)};

const auto &custom_resolver{resolver(options, options.contains("http"),
Expand All @@ -49,7 +49,7 @@ auto sourcemeta::jsonschema::metaschema(
sourcemeta::core::dialect(entry.second, default_dialect_option)};
if (dialect.empty()) {
throw FileError<sourcemeta::core::SchemaUnknownBaseDialectError>(
entry.first);
entry.resolution_base);
}

const auto metaschema{sourcemeta::core::metaschema(
Expand Down Expand Up @@ -80,7 +80,7 @@ auto sourcemeta::jsonschema::metaschema(
} else if (json_output) {
// Otherwise its impossible to correlate the output
// when validating i.e. a directory of schemas
std::cerr << entry.first.string() << "\n";
std::cerr << entry.first << "\n";
const auto output{sourcemeta::blaze::standard(
evaluator, cache.at(std::string{dialect}), entry.second,
sourcemeta::blaze::StandardOutput::Basic, entry.positions)};
Expand All @@ -98,29 +98,25 @@ auto sourcemeta::jsonschema::metaschema(
if (evaluator.validate(cache.at(std::string{dialect}), entry.second,
std::ref(output))) {
LOG_VERBOSE(options)
<< "ok: "
<< sourcemeta::core::weakly_canonical(entry.first).string()
<< "\n matches " << dialect << "\n";
<< "ok: " << entry.first << "\n matches " << dialect << "\n";
} else {
std::cerr << "fail: "
<< sourcemeta::core::weakly_canonical(entry.first).string()
<< "\n";
std::cerr << "fail: " << entry.first << "\n";
print(output, entry.positions, std::cerr);
result = false;
}
}
} catch (
const sourcemeta::blaze::CompilerReferenceTargetNotSchemaError &error) {
throw FileError<sourcemeta::blaze::CompilerReferenceTargetNotSchemaError>(
entry.first, error);
entry.resolution_base, error);
} catch (const sourcemeta::core::SchemaRelativeMetaschemaResolutionError
&error) {
throw FileError<
sourcemeta::core::SchemaRelativeMetaschemaResolutionError>(
entry.first, error);
entry.resolution_base, error);
} catch (const sourcemeta::core::SchemaResolutionError &error) {
throw FileError<sourcemeta::core::SchemaResolutionError>(entry.first,
error);
throw FileError<sourcemeta::core::SchemaResolutionError>(
entry.resolution_base, error);
}
}

Expand Down
Loading