Skip to content

Commit 7cb6852

Browse files
committed
Fixes
Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
1 parent a1365ef commit 7cb6852

File tree

9 files changed

+76
-72
lines changed

9 files changed

+76
-72
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ Brewfile.lock.json
1313
.DS_Store
1414
*.snap
1515
/node_modules
16+
/.cache

src/command_inspect.cc

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ auto print_frame(std::ostream &stream,
5050
}
5151

5252
stream << " Root : "
53-
<< location.second.root.value_or("<ANONYMOUS>") << "\n";
53+
<< (frame.root().empty() ? "<ANONYMOUS>" : frame.root()) << "\n";
5454

5555
if (location.second.pointer.empty()) {
5656
stream << " Pointer :\n";
@@ -70,17 +70,19 @@ auto print_frame(std::ostream &stream,
7070

7171
stream << " Base : " << location.second.base << "\n";
7272

73-
if (location.second.relative_pointer.empty()) {
73+
const auto relative_pointer{
74+
location.second.pointer.slice(location.second.relative_pointer)};
75+
if (relative_pointer.empty()) {
7476
stream << " Relative Pointer :\n";
7577
} else {
7678
stream << " Relative Pointer : ";
77-
sourcemeta::core::stringify(location.second.relative_pointer, stream);
79+
sourcemeta::core::stringify(relative_pointer, stream);
7880
stream << "\n";
7981
}
8082

8183
stream << " Dialect : " << location.second.dialect << "\n";
82-
stream << " Base Dialect : " << location.second.base_dialect
83-
<< "\n";
84+
stream << " Base Dialect : "
85+
<< sourcemeta::core::to_string(location.second.base_dialect) << "\n";
8486

8587
if (location.second.parent.has_value()) {
8688
if (location.second.parent.value().empty()) {
@@ -125,7 +127,8 @@ auto print_frame(std::ostream &stream,
125127
stream << " Destination : " << reference.second.destination
126128
<< "\n";
127129
stream << " - (w/o fragment) : "
128-
<< reference.second.base.value_or("<NONE>") << "\n";
130+
<< (reference.second.base.empty() ? "<NONE>" : reference.second.base)
131+
<< "\n";
129132
stream << " - (fragment) : "
130133
<< reference.second.fragment.value_or("<NONE>") << "\n";
131134
}
@@ -157,17 +160,17 @@ auto sourcemeta::jsonschema::inspect(const sourcemeta::core::Options &options)
157160
const auto identifier{
158161
sourcemeta::core::identify(schema, custom_resolver, dialect)};
159162

160-
frame.analyse(
161-
schema, sourcemeta::core::schema_walker, custom_resolver, dialect,
162-
163-
// Only use the file-based URI if the schema has no identifier,
164-
// as otherwise we make the output unnecessarily hard when it
165-
// comes to debugging schemas
166-
identifier.has_value()
167-
? std::optional<sourcemeta::core::JSON::String>(std::nullopt)
168-
: sourcemeta::core::URI::from_path(
169-
sourcemeta::core::weakly_canonical(schema_path))
170-
.recompose());
163+
frame.analyse(schema, sourcemeta::core::schema_walker, custom_resolver,
164+
dialect,
165+
166+
// Only use the file-based URI if the schema has no
167+
// identifier, as otherwise we make the output unnecessarily
168+
// hard when it comes to debugging schemas
169+
!identifier.empty()
170+
? ""
171+
: sourcemeta::core::URI::from_path(
172+
sourcemeta::core::weakly_canonical(schema_path))
173+
.recompose());
171174
} catch (
172175
const sourcemeta::core::SchemaRelativeMetaschemaResolutionError &error) {
173176
throw FileError<sourcemeta::core::SchemaRelativeMetaschemaResolutionError>(

src/command_lint.cc

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -155,24 +155,21 @@ auto sourcemeta::jsonschema::lint(const sourcemeta::core::Options &options)
155155
}
156156

157157
if (options.contains("list")) {
158-
std::vector<std::pair<std::reference_wrapper<const std::string>,
159-
std::reference_wrapper<const std::string>>>
160-
rules;
158+
std::vector<std::pair<std::string_view, std::string_view>> rules;
161159
for (const auto &entry : bundle) {
162160
rules.emplace_back(entry->name(), entry->message());
163161
}
164162

165-
std::sort(rules.begin(), rules.end(),
166-
[](const auto &left, const auto &right) {
167-
return left.first.get() < right.first.get() ||
168-
(left.first.get() == right.first.get() &&
169-
left.second.get() < right.second.get());
170-
});
163+
std::sort(
164+
rules.begin(), rules.end(), [](const auto &left, const auto &right) {
165+
return left.first < right.first ||
166+
(left.first == right.first && left.second < right.second);
167+
});
171168

172169
std::size_t count{0};
173170
for (const auto &entry : rules) {
174-
std::cout << entry.first.get() << "\n";
175-
std::cout << " " << entry.second.get() << "\n\n";
171+
std::cout << entry.first << "\n";
172+
std::cout << " " << entry.second << "\n\n";
176173
count += 1;
177174
}
178175

src/command_metaschema.cc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ auto sourcemeta::jsonschema::metaschema(
4747
try {
4848
const auto dialect{
4949
sourcemeta::core::dialect(entry.second, default_dialect_option)};
50-
if (!dialect) {
50+
if (dialect.empty()) {
5151
throw FileError<sourcemeta::core::SchemaUnknownBaseDialectError>(
5252
entry.first);
5353
}
@@ -62,27 +62,27 @@ auto sourcemeta::jsonschema::metaschema(
6262
frame.analyse(bundled, sourcemeta::core::schema_walker, custom_resolver,
6363
default_dialect_option);
6464

65-
if (!cache.contains(dialect.value())) {
65+
if (!cache.contains(std::string{dialect})) {
6666
const auto metaschema_template{sourcemeta::blaze::compile(
6767
bundled, sourcemeta::core::schema_walker, custom_resolver,
6868
sourcemeta::blaze::default_schema_compiler, frame,
6969
sourcemeta::blaze::Mode::Exhaustive, default_dialect_option)};
70-
cache.insert({dialect.value(), metaschema_template});
70+
cache.insert({std::string{dialect}, metaschema_template});
7171
}
7272

7373
if (trace) {
7474
sourcemeta::blaze::TraceOutput output{
7575
sourcemeta::core::schema_walker, custom_resolver,
7676
sourcemeta::core::empty_weak_pointer, frame};
77-
result = evaluator.validate(cache.at(dialect.value()), entry.second,
78-
std::ref(output));
77+
result = evaluator.validate(cache.at(std::string{dialect}),
78+
entry.second, std::ref(output));
7979
print(output, entry.positions, std::cout);
8080
} else if (json_output) {
8181
// Otherwise its impossible to correlate the output
8282
// when validating i.e. a directory of schemas
8383
std::cerr << entry.first.string() << "\n";
8484
const auto output{sourcemeta::blaze::standard(
85-
evaluator, cache.at(dialect.value()), entry.second,
85+
evaluator, cache.at(std::string{dialect}), entry.second,
8686
sourcemeta::blaze::StandardOutput::Basic, entry.positions)};
8787
assert(output.is_object());
8888
assert(output.defines("valid"));
@@ -95,12 +95,12 @@ auto sourcemeta::jsonschema::metaschema(
9595
std::cout << "\n";
9696
} else {
9797
sourcemeta::blaze::SimpleOutput output{entry.second};
98-
if (evaluator.validate(cache.at(dialect.value()), entry.second,
98+
if (evaluator.validate(cache.at(std::string{dialect}), entry.second,
9999
std::ref(output))) {
100100
LOG_VERBOSE(options)
101101
<< "ok: "
102102
<< sourcemeta::core::weakly_canonical(entry.first).string()
103-
<< "\n matches " << dialect.value() << "\n";
103+
<< "\n matches " << dialect << "\n";
104104
} else {
105105
std::cerr << "fail: "
106106
<< sourcemeta::core::weakly_canonical(entry.first).string()

src/command_test.cc

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33

44
#include <sourcemeta/core/json.h>
55

6-
#include <chrono> // std::chrono
7-
#include <cstdlib> // EXIT_FAILURE
8-
#include <iostream> // std::cerr, std::cout
9-
#include <sstream> // std::ostringstream
10-
#include <string> // std::string
11-
#include <thread> // std::this_thread
12-
#include <vector> // std::vector
6+
#include <chrono> // std::chrono
7+
#include <cstdlib> // EXIT_FAILURE
8+
#include <iostream> // std::cerr, std::cout
9+
#include <sstream> // std::ostringstream
10+
#include <string> // std::string
11+
#include <string_view> // std::string_view
12+
#include <thread> // std::this_thread
1313

1414
#include "command.h"
1515
#include "configuration.h"
@@ -24,8 +24,8 @@ namespace {
2424

2525
auto parse_test_suite(const sourcemeta::jsonschema::InputJSON &entry,
2626
const sourcemeta::core::SchemaResolver &schema_resolver,
27-
const std::optional<std::string> &dialect,
28-
const bool json_output) -> sourcemeta::blaze::TestSuite {
27+
const std::string_view dialect, const bool json_output)
28+
-> sourcemeta::blaze::TestSuite {
2929
try {
3030
return sourcemeta::blaze::TestSuite::parse(
3131
entry.second, entry.positions, entry.first.parent_path(),

src/command_validate.cc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
#include <sourcemeta/blaze/evaluator.h>
99
#include <sourcemeta/blaze/output.h>
1010

11-
#include <chrono> // std::chrono
12-
#include <cmath> // std::sqrt
13-
#include <iostream> // std::cerr
14-
#include <string> // std::string
11+
#include <chrono> // std::chrono
12+
#include <cmath> // std::sqrt
13+
#include <iostream> // std::cerr
14+
#include <string> // std::string
15+
#include <string_view> // std::string_view
1516

1617
#include "command.h"
1718
#include "configuration.h"
@@ -36,8 +37,8 @@ auto get_precompiled_schema_template_path(
3637
auto get_schema_template(const sourcemeta::core::JSON &bundled,
3738
const sourcemeta::core::SchemaResolver &resolver,
3839
const sourcemeta::core::SchemaFrame &frame,
39-
const std::optional<std::string> &default_dialect,
40-
const std::optional<std::string> &default_id,
40+
const std::string_view default_dialect,
41+
const std::string_view default_id,
4142
const bool fast_mode,
4243
const sourcemeta::core::Options &options)
4344
-> sourcemeta::blaze::Template {

src/error.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,10 +298,11 @@ inline auto print_exception(const bool is_json, const Exception &exception)
298298
}
299299

300300
if constexpr (requires(const Exception &current) {
301-
{ current.option() } -> std::convertible_to<std::string>;
301+
{ current.option() } -> std::convertible_to<std::string_view>;
302302
}) {
303303
if (is_json) {
304-
error_json.assign("option", sourcemeta::core::JSON{exception.option()});
304+
error_json.assign(
305+
"option", sourcemeta::core::JSON{std::string{exception.option()}});
305306
} else {
306307
std::cerr << " at option " << exception.option() << "\n";
307308
}

src/resolver.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class CustomResolver {
8080
CustomResolver(
8181
const sourcemeta::core::Options &options,
8282
const std::optional<sourcemeta::core::SchemaConfig> &configuration,
83-
const bool remote, const std::optional<std::string> &default_dialect)
83+
const bool remote, const std::string_view default_dialect)
8484
: options_{options}, configuration_{configuration}, remote_{remote} {
8585
if (options.contains("resolve")) {
8686
for (const auto &entry : for_each_json(options.at("resolve"), options)) {
@@ -121,8 +121,8 @@ class CustomResolver {
121121
}
122122

123123
auto add(const sourcemeta::core::JSON &schema,
124-
const std::optional<std::string> &default_dialect = std::nullopt,
125-
const std::optional<std::string> &default_id = std::nullopt,
124+
const std::string_view default_dialect = "",
125+
const std::string_view default_id = "",
126126
const std::function<void(const sourcemeta::core::JSON::String &)>
127127
&callback = nullptr) -> bool {
128128
assert(sourcemeta::core::is_schema(schema));
@@ -223,12 +223,12 @@ class CustomResolver {
223223

224224
inline auto
225225
resolver(const sourcemeta::core::Options &options, const bool remote,
226-
const std::optional<std::string> &default_dialect,
226+
const std::string_view default_dialect,
227227
const std::optional<sourcemeta::core::SchemaConfig> &configuration)
228228
-> const CustomResolver & {
229-
using CacheKey = std::pair<bool, std::optional<std::string>>;
229+
using CacheKey = std::pair<bool, std::string>;
230230
static std::map<CacheKey, CustomResolver> resolver_cache;
231-
const CacheKey cache_key{remote, default_dialect};
231+
const CacheKey cache_key{remote, std::string{default_dialect}};
232232

233233
// Check if resolver is already cached
234234
auto iterator{resolver_cache.find(cache_key)};

src/utils.h

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,29 @@
88

99
#include <sourcemeta/blaze/output.h>
1010

11-
#include <cassert> // assert
12-
#include <iterator> // std::next
13-
#include <optional> // std::optional
14-
#include <ostream> // std::ostream
15-
#include <sstream> // std::ostringstream
16-
#include <string> // std::string, std::stoull
17-
#include <tuple> // std::get
18-
#include <variant> // std::visit
11+
#include <cassert> // assert
12+
#include <iterator> // std::next
13+
#include <optional> // std::optional
14+
#include <ostream> // std::ostream
15+
#include <string> // std::string, std::stoull
16+
#include <string_view> // std::string_view
17+
#include <tuple> // std::get
18+
#include <variant> // std::visit
1919

2020
namespace sourcemeta::jsonschema {
2121

2222
inline auto default_dialect(
2323
const sourcemeta::core::Options &options,
2424
const std::optional<sourcemeta::core::SchemaConfig> &configuration)
25-
-> std::optional<std::string> {
25+
-> std::string_view {
2626
if (options.contains("default-dialect")) {
27-
return std::string{options.at("default-dialect").front()};
28-
} else if (configuration.has_value()) {
29-
return configuration.value().default_dialect;
27+
return options.at("default-dialect").front();
28+
} else if (configuration.has_value() &&
29+
configuration.value().default_dialect.has_value()) {
30+
return configuration.value().default_dialect.value();
3031
}
3132

32-
return std::nullopt;
33+
return "";
3334
}
3435

3536
inline auto parse_indentation(const sourcemeta::core::Options &options)

0 commit comments

Comments
 (0)