Skip to content

Commit 02c2264

Browse files
committed
[WIP] Introduce a new --default-dialect option for most commands
See: #218 Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
1 parent a9ac9ed commit 02c2264

8 files changed

+43
-12
lines changed

src/command_bundle.cc

+5-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ auto sourcemeta::jsonschema::cli::bundle(
1111
const std::span<const std::string> &arguments) -> int {
1212
const auto options{
1313
parse_options(arguments, {"h", "http", "w", "without-id"})};
14+
const auto dialect{default_dialect(options)};
1415

1516
if (options.at("").size() < 1) {
1617
std::cerr
@@ -23,13 +24,15 @@ auto sourcemeta::jsonschema::cli::bundle(
2324

2425
sourcemeta::core::bundle(
2526
schema, sourcemeta::core::schema_official_walker,
26-
resolver(options, options.contains("h") || options.contains("http")));
27+
resolver(options, options.contains("h") || options.contains("http")),
28+
dialect);
2729

2830
if (options.contains("w") || options.contains("without-id")) {
2931
log_verbose(options) << "Removing schema identifiers\n";
3032
sourcemeta::core::unidentify(
3133
schema, sourcemeta::core::schema_official_walker,
32-
resolver(options, options.contains("h") || options.contains("http")));
34+
resolver(options, options.contains("h") || options.contains("http")),
35+
dialect);
3336
}
3437

3538
sourcemeta::core::prettify(schema, std::cout,

src/command_inspect.cc

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ auto sourcemeta::jsonschema::cli::inspect(
2222

2323
sourcemeta::core::SchemaFrame frame{
2424
sourcemeta::core::SchemaFrame::Mode::Instances};
25+
26+
const auto dialect{default_dialect(options)};
2527
frame.analyse(schema, sourcemeta::core::schema_official_walker,
26-
resolver(options));
28+
resolver(options), dialect);
2729

2830
if (options.contains("json") || options.contains("j")) {
2931
sourcemeta::core::prettify(frame.to_json(), std::cout);

src/command_lint.cc

+4-2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ auto sourcemeta::jsonschema::cli::lint(
6161

6262
bool result{true};
6363
auto errors_array = sourcemeta::core::JSON::make_array();
64+
const auto dialect{default_dialect(options)};
6465

6566
if (options.contains("f") || options.contains("fix")) {
6667
for (const auto &entry :
@@ -75,7 +76,7 @@ auto sourcemeta::jsonschema::cli::lint(
7576

7677
auto copy = entry.second;
7778
bundle.apply(copy, sourcemeta::core::schema_official_walker,
78-
resolver(options));
79+
resolver(options), dialect);
7980
std::ofstream output{entry.first};
8081
if (options.contains("k") || options.contains("keep-ordering")) {
8182
sourcemeta::core::prettify(copy, output);
@@ -114,7 +115,8 @@ auto sourcemeta::jsonschema::cli::lint(
114115
sourcemeta::core::stringify(pointer, std::cout);
115116
std::cout << "\"\n";
116117
}
117-
});
118+
},
119+
dialect);
118120

119121
if (!subresult) {
120122
result = false;

src/command_metaschema.cc

+6-4
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,18 @@ auto sourcemeta::jsonschema::cli::metaschema(
3737
return EXIT_FAILURE;
3838
}
3939

40-
const auto dialect{sourcemeta::core::dialect(entry.second)};
40+
const auto default_dialect_option{default_dialect(options)};
41+
const auto dialect{
42+
sourcemeta::core::dialect(entry.second, default_dialect_option)};
4143
assert(dialect.has_value());
4244

43-
const auto metaschema{
44-
sourcemeta::core::metaschema(entry.second, custom_resolver)};
45+
const auto metaschema{sourcemeta::core::metaschema(
46+
entry.second, custom_resolver, default_dialect_option)};
4547
if (!cache.contains(dialect.value())) {
4648
const auto metaschema_template{sourcemeta::blaze::compile(
4749
metaschema, sourcemeta::core::schema_official_walker, custom_resolver,
4850
sourcemeta::blaze::default_schema_compiler,
49-
sourcemeta::blaze::Mode::Exhaustive)};
51+
sourcemeta::blaze::Mode::Exhaustive, default_dialect_option)};
5052
cache.insert({dialect.value(), metaschema_template});
5153
}
5254

src/command_test.cc

+3-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ auto sourcemeta::jsonschema::cli::test(
4848
resolver(options, options.contains("h") || options.contains("http"))};
4949
const auto verbose{options.contains("verbose") || options.contains("v")};
5050
sourcemeta::blaze::Evaluator evaluator;
51+
const auto dialect{default_dialect(options)};
5152

5253
for (const auto &entry : for_each_json(options.at(""), parse_ignore(options),
5354
parse_extensions(options))) {
@@ -117,7 +118,8 @@ auto sourcemeta::jsonschema::cli::test(
117118
try {
118119
schema_template = sourcemeta::blaze::compile(
119120
schema, sourcemeta::core::schema_official_walker, test_resolver,
120-
sourcemeta::blaze::default_schema_compiler);
121+
sourcemeta::blaze::default_schema_compiler,
122+
sourcemeta::blaze::Mode::FastValidation, dialect);
121123
} catch (const sourcemeta::core::SchemaReferenceError &error) {
122124
if (error.location() == sourcemeta::core::Pointer{"$ref"} &&
123125
error.id() == schema_uri.recompose()) {

src/command_validate.cc

+3-2
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,15 @@ auto sourcemeta::jsonschema::cli::validate(
5454
}
5555

5656
const auto fast_mode{options.contains("f") || options.contains("fast")};
57-
5857
const auto benchmark{options.contains("b") || options.contains("benchmark")};
5958
const auto trace{options.contains("t") || options.contains("trace")};
59+
const auto dialect{default_dialect(options)};
6060
const auto schema_template{sourcemeta::blaze::compile(
6161
schema, sourcemeta::core::schema_official_walker, custom_resolver,
6262
sourcemeta::blaze::default_schema_compiler,
6363
fast_mode ? sourcemeta::blaze::Mode::FastValidation
64-
: sourcemeta::blaze::Mode::Exhaustive)};
64+
: sourcemeta::blaze::Mode::Exhaustive,
65+
dialect)};
6566
sourcemeta::blaze::Evaluator evaluator;
6667

6768
bool result{true};

src/utils.cc

+15
Original file line numberDiff line numberDiff line change
@@ -375,4 +375,19 @@ auto safe_weakly_canonical(const std::filesystem::path &input)
375375
: std::filesystem::weakly_canonical(input);
376376
}
377377

378+
auto default_dialect(
379+
const std::map<std::string, std::vector<std::string>> &options)
380+
-> std::optional<std::string> {
381+
382+
if (options.contains("default-dialect")) {
383+
return options.at("default-dialect").front();
384+
}
385+
386+
if (options.contains("l")) {
387+
return options.at("l").front();
388+
}
389+
390+
return std::nullopt;
391+
}
392+
378393
} // namespace sourcemeta::jsonschema::cli

src/utils.h

+4
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ auto parse_ignore(
5353
auto safe_weakly_canonical(const std::filesystem::path &input)
5454
-> std::filesystem::path;
5555

56+
auto default_dialect(
57+
const std::map<std::string, std::vector<std::string>> &options)
58+
-> std::optional<std::string>;
59+
5660
} // namespace sourcemeta::jsonschema::cli
5761

5862
#endif

0 commit comments

Comments
 (0)