Skip to content

Commit

Permalink
Centralise logic to read JSON files (#210)
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <[email protected]>
  • Loading branch information
jviotti authored Jan 15, 2025
1 parent b880248 commit 6f498fc
Show file tree
Hide file tree
Showing 11 changed files with 23 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/command_bundle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ auto sourcemeta::jsonschema::cli::bundle(
return EXIT_FAILURE;
}

auto schema{sourcemeta::jsontoolkit::from_file(options.at("").front())};
auto schema{sourcemeta::jsonschema::cli::read_file(options.at("").front())};

sourcemeta::jsontoolkit::bundle(
schema, sourcemeta::jsontoolkit::default_schema_walker,
Expand Down
2 changes: 1 addition & 1 deletion src/command_encode.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ auto sourcemeta::jsonschema::cli::encode(
<< "%\n";
} else {
const auto entry{
sourcemeta::jsontoolkit::from_file(options.at("").front())};
sourcemeta::jsonschema::cli::read_file(options.at("").front())};
std::ofstream output_stream(
std::filesystem::weakly_canonical(options.at("").at(1)),
std::ios::binary);
Expand Down
2 changes: 1 addition & 1 deletion src/command_frame.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ auto sourcemeta::jsonschema::cli::frame(
}

const sourcemeta::jsontoolkit::JSON schema{
sourcemeta::jsontoolkit::from_file(options.at("").front())};
sourcemeta::jsonschema::cli::read_file(options.at("").front())};

sourcemeta::jsontoolkit::Frame frame;
frame.analyse(schema, sourcemeta::jsontoolkit::default_schema_walker,
Expand Down
4 changes: 2 additions & 2 deletions src/command_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ static auto get_data(const sourcemeta::jsontoolkit::JSON &test_case,
}

try {
return sourcemeta::jsontoolkit::from_file(data_path);
return sourcemeta::jsonschema::cli::read_file(data_path);
} catch (...) {
std::cout << "\n";
throw;
Expand All @@ -76,7 +76,7 @@ auto sourcemeta::jsonschema::cli::test(
for (const auto &entry : for_each_json(options.at(""), parse_ignore(options),
parse_extensions(options))) {
const sourcemeta::jsontoolkit::JSON test{
sourcemeta::jsontoolkit::from_file(entry.first)};
sourcemeta::jsonschema::cli::read_file(entry.first)};
std::cout << entry.first.string() << ":";

if (!test.is_object()) {
Expand Down
5 changes: 3 additions & 2 deletions src/command_validate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ auto sourcemeta::jsonschema::cli::validate(
const auto custom_resolver{
resolver(options, options.contains("h") || options.contains("http"))};

const auto schema{sourcemeta::jsontoolkit::from_file(schema_path)};
const auto schema{sourcemeta::jsonschema::cli::read_file(schema_path)};

if (!sourcemeta::jsontoolkit::is_schema(schema)) {
std::cerr << "error: The schema file you provided does not represent a "
Expand Down Expand Up @@ -131,7 +131,8 @@ auto sourcemeta::jsonschema::cli::validate(
log_verbose(options) << "warning: The JSONL file is empty\n";
}
} else {
const auto instance{sourcemeta::jsontoolkit::from_file(instance_path)};
const auto instance{
sourcemeta::jsonschema::cli::read_file(instance_path)};
std::ostringstream error;
sourcemeta::blaze::ErrorOutput output{instance};
sourcemeta::blaze::TraceOutput trace_output;
Expand Down
10 changes: 8 additions & 2 deletions src/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ auto handle_json_entry(

// TODO: Print a verbose message for what is getting parsed
result.emplace_back(canonical,
sourcemeta::jsontoolkit::from_file(canonical));
sourcemeta::jsonschema::cli::read_file(canonical));
}
}
} else {
Expand All @@ -85,7 +85,7 @@ auto handle_json_entry(

// TODO: Print a verbose message for what is getting parsed
result.emplace_back(canonical,
sourcemeta::jsontoolkit::from_file(canonical));
sourcemeta::jsonschema::cli::read_file(canonical));
}
}
}
Expand All @@ -104,6 +104,12 @@ auto normalize_extension(const std::string &extension) -> std::string {

namespace sourcemeta::jsonschema::cli {

auto read_file(const std::filesystem::path &path)
-> sourcemeta::jsontoolkit::JSON {
// TODO: Extend to support YAML
return sourcemeta::jsontoolkit::from_file(path);
}

auto for_each_json(const std::vector<std::string> &arguments,
const std::set<std::filesystem::path> &blacklist,
const std::set<std::string> &extensions)
Expand Down
3 changes: 3 additions & 0 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

namespace sourcemeta::jsonschema::cli {

auto read_file(const std::filesystem::path &path)
-> sourcemeta::jsontoolkit::JSON;

auto parse_options(const std::span<const std::string> &arguments,
const std::set<std::string> &flags)
-> std::map<std::string, std::vector<std::string>>;
Expand Down
3 changes: 1 addition & 2 deletions test/ci/fail_bundle_http_non_200.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ EOF
test "$CODE" = "1" || exit 1

cat << EOF > "$TMP/expected.txt"
error: 400 Bad Request
at https://example.com
error: Failed to parse the JSON document at line 1 and column 1
EOF

diff "$TMP/stderr.txt" "$TMP/expected.txt"
3 changes: 1 addition & 2 deletions test/ci/fail_bundle_http_non_200_verbose.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ test "$CODE" = "1" || exit 1

cat << EOF > "$TMP/expected.txt"
Resolving over HTTP: https://example.com
error: 400 Bad Request
at https://example.com
error: Failed to parse the JSON document at line 1 and column 1
EOF

diff "$TMP/stderr.txt" "$TMP/expected.txt"
3 changes: 1 addition & 2 deletions test/ci/fail_validate_http_non_200.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ EOF
test "$CODE" = "1" || exit 1

cat << EOF > "$TMP/expected.txt"
error: 400 Bad Request
at https://example.com
error: Failed to parse the JSON document at line 1 and column 1
EOF

diff "$TMP/stderr.txt" "$TMP/expected.txt"
3 changes: 1 addition & 2 deletions test/ci/fail_validate_http_non_200_verbose.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ test "$CODE" = "1" || exit 1

cat << EOF > "$TMP/expected.txt"
Resolving over HTTP: https://example.com
error: 400 Bad Request
at https://example.com
error: Failed to parse the JSON document at line 1 and column 1
EOF

diff "$TMP/stderr.txt" "$TMP/expected.txt"

0 comments on commit 6f498fc

Please sign in to comment.