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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ Brewfile.lock.json
.DS_Store
*.snap
/node_modules
/.cache
6 changes: 3 additions & 3 deletions DEPENDENCIES
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
vendorpull https://github.com/sourcemeta/vendorpull 1dcbac42809cf87cb5b045106b863e17ad84ba02
core https://github.com/sourcemeta/core 94ff49bd58ca63c5e5a7fb2435b3bc72872517c4
jsonbinpack https://github.com/sourcemeta/jsonbinpack 8fae212dc7ec02af4bb0cd4e7fccd42a2471f1c1
blaze https://github.com/sourcemeta/blaze 8dba65f8aebfe1ac976168b76e01c20dd406c517
core https://github.com/sourcemeta/core 376f397db98e72be48d8401e2fbf2ee96f033966
jsonbinpack https://github.com/sourcemeta/jsonbinpack 0c2340990bf31c630155991a93306990d9d94fd4
blaze https://github.com/sourcemeta/blaze 93342104a85814bc0fd11792d305c4e83de259c0
hydra https://github.com/sourcemeta/hydra c86d2165a2f27f838837af1a5af24b1055a35317
ctrf https://github.com/ctrf-io/ctrf 93ea827d951390190171d37443bff169cf47c808
43 changes: 24 additions & 19 deletions src/command_inspect.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ auto print_frame(std::ostream &stream,
}

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

if (location.second.pointer.empty()) {
stream << " Pointer :\n";
Expand All @@ -60,7 +60,8 @@ auto print_frame(std::ostream &stream,
stream << "\n";
}

const auto position{positions.get(location.second.pointer)};
const auto position{
positions.get(sourcemeta::core::to_pointer(location.second.pointer))};
if (position.has_value()) {
stream << " File Position : " << std::get<0>(position.value())
<< ":" << std::get<1>(position.value()) << "\n";
Expand All @@ -70,17 +71,19 @@ auto print_frame(std::ostream &stream,

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

if (location.second.relative_pointer.empty()) {
const auto relative_pointer{
location.second.pointer.slice(location.second.relative_pointer)};
if (relative_pointer.empty()) {
stream << " Relative Pointer :\n";
} else {
stream << " Relative Pointer : ";
sourcemeta::core::stringify(location.second.relative_pointer, stream);
sourcemeta::core::stringify(relative_pointer, stream);
stream << "\n";
}

stream << " Dialect : " << location.second.dialect << "\n";
stream << " Base Dialect : " << location.second.base_dialect
<< "\n";
stream << " Base Dialect : "
<< sourcemeta::core::to_string(location.second.base_dialect) << "\n";

if (location.second.parent.has_value()) {
if (location.second.parent.value().empty()) {
Expand Down Expand Up @@ -114,7 +117,8 @@ auto print_frame(std::ostream &stream,
stream << " Type : Dynamic\n";
}

const auto position{positions.get(reference.first.second)};
const auto position{
positions.get(sourcemeta::core::to_pointer(reference.first.second))};
if (position.has_value()) {
stream << " File Position : " << std::get<0>(position.value())
<< ":" << std::get<1>(position.value()) << "\n";
Expand All @@ -125,7 +129,8 @@ auto print_frame(std::ostream &stream,
stream << " Destination : " << reference.second.destination
<< "\n";
stream << " - (w/o fragment) : "
<< reference.second.base.value_or("<NONE>") << "\n";
<< (reference.second.base.empty() ? "<NONE>" : reference.second.base)
<< "\n";
stream << " - (fragment) : "
<< reference.second.fragment.value_or("<NONE>") << "\n";
}
Expand Down Expand Up @@ -157,17 +162,17 @@ auto sourcemeta::jsonschema::inspect(const sourcemeta::core::Options &options)
const auto identifier{
sourcemeta::core::identify(schema, custom_resolver, dialect)};

frame.analyse(
schema, sourcemeta::core::schema_walker, custom_resolver, dialect,

// Only use the file-based URI if the schema has no identifier,
// as otherwise we make the output unnecessarily hard when it
// comes to debugging schemas
identifier.has_value()
? std::optional<sourcemeta::core::JSON::String>(std::nullopt)
: sourcemeta::core::URI::from_path(
sourcemeta::core::weakly_canonical(schema_path))
.recompose());
frame.analyse(schema, sourcemeta::core::schema_walker, custom_resolver,
dialect,

// Only use the file-based URI if the schema has no
// identifier, as otherwise we make the output unnecessarily
// hard when it comes to debugging schemas
!identifier.empty()
? ""
: sourcemeta::core::URI::from_path(
sourcemeta::core::weakly_canonical(schema_path))
.recompose());
} catch (
const sourcemeta::core::SchemaRelativeMetaschemaResolutionError &error) {
throw FileError<sourcemeta::core::SchemaRelativeMetaschemaResolutionError>(
Expand Down
19 changes: 8 additions & 11 deletions src/command_lint.cc
Original file line number Diff line number Diff line change
Expand Up @@ -155,24 +155,21 @@ auto sourcemeta::jsonschema::lint(const sourcemeta::core::Options &options)
}

if (options.contains("list")) {
std::vector<std::pair<std::reference_wrapper<const std::string>,
std::reference_wrapper<const std::string>>>
rules;
std::vector<std::pair<std::string_view, std::string_view>> rules;
for (const auto &entry : bundle) {
rules.emplace_back(entry->name(), entry->message());
}

std::sort(rules.begin(), rules.end(),
[](const auto &left, const auto &right) {
return left.first.get() < right.first.get() ||
(left.first.get() == right.first.get() &&
left.second.get() < right.second.get());
});
std::sort(
rules.begin(), rules.end(), [](const auto &left, const auto &right) {
return left.first < right.first ||
(left.first == right.first && left.second < right.second);
});

std::size_t count{0};
for (const auto &entry : rules) {
std::cout << entry.first.get() << "\n";
std::cout << " " << entry.second.get() << "\n\n";
std::cout << entry.first << "\n";
std::cout << " " << entry.second << "\n\n";
count += 1;
}

Expand Down
16 changes: 8 additions & 8 deletions src/command_metaschema.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ auto sourcemeta::jsonschema::metaschema(
try {
const auto dialect{
sourcemeta::core::dialect(entry.second, default_dialect_option)};
if (!dialect) {
if (dialect.empty()) {
throw FileError<sourcemeta::core::SchemaUnknownBaseDialectError>(
entry.first);
}
Expand All @@ -62,27 +62,27 @@ auto sourcemeta::jsonschema::metaschema(
frame.analyse(bundled, sourcemeta::core::schema_walker, custom_resolver,
default_dialect_option);

if (!cache.contains(dialect.value())) {
if (!cache.contains(std::string{dialect})) {
const auto metaschema_template{sourcemeta::blaze::compile(
bundled, sourcemeta::core::schema_walker, custom_resolver,
sourcemeta::blaze::default_schema_compiler, frame,
sourcemeta::blaze::Mode::Exhaustive, default_dialect_option)};
cache.insert({dialect.value(), metaschema_template});
cache.insert({std::string{dialect}, metaschema_template});
}

if (trace) {
sourcemeta::blaze::TraceOutput output{
sourcemeta::core::schema_walker, custom_resolver,
sourcemeta::core::empty_weak_pointer, frame};
result = evaluator.validate(cache.at(dialect.value()), entry.second,
std::ref(output));
result = evaluator.validate(cache.at(std::string{dialect}),
entry.second, std::ref(output));
print(output, entry.positions, std::cout);
} 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";
const auto output{sourcemeta::blaze::standard(
evaluator, cache.at(dialect.value()), entry.second,
evaluator, cache.at(std::string{dialect}), entry.second,
sourcemeta::blaze::StandardOutput::Basic, entry.positions)};
assert(output.is_object());
assert(output.defines("valid"));
Expand All @@ -95,12 +95,12 @@ auto sourcemeta::jsonschema::metaschema(
std::cout << "\n";
} else {
sourcemeta::blaze::SimpleOutput output{entry.second};
if (evaluator.validate(cache.at(dialect.value()), entry.second,
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.value() << "\n";
<< "\n matches " << dialect << "\n";
} else {
std::cerr << "fail: "
<< sourcemeta::core::weakly_canonical(entry.first).string()
Expand Down
18 changes: 9 additions & 9 deletions src/command_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

#include <sourcemeta/core/json.h>

#include <chrono> // std::chrono
#include <cstdlib> // EXIT_FAILURE
#include <iostream> // std::cerr, std::cout
#include <sstream> // std::ostringstream
#include <string> // std::string
#include <thread> // std::this_thread
#include <vector> // std::vector
#include <chrono> // std::chrono
#include <cstdlib> // EXIT_FAILURE
#include <iostream> // std::cerr, std::cout
#include <sstream> // std::ostringstream
#include <string> // std::string
#include <string_view> // std::string_view
#include <thread> // std::this_thread

#include "command.h"
#include "configuration.h"
Expand All @@ -24,8 +24,8 @@ namespace {

auto parse_test_suite(const sourcemeta::jsonschema::InputJSON &entry,
const sourcemeta::core::SchemaResolver &schema_resolver,
const std::optional<std::string> &dialect,
const bool json_output) -> sourcemeta::blaze::TestSuite {
const std::string_view dialect, const bool json_output)
-> sourcemeta::blaze::TestSuite {
try {
return sourcemeta::blaze::TestSuite::parse(
entry.second, entry.positions, entry.first.parent_path(),
Expand Down
13 changes: 7 additions & 6 deletions src/command_validate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
#include <sourcemeta/blaze/evaluator.h>
#include <sourcemeta/blaze/output.h>

#include <chrono> // std::chrono
#include <cmath> // std::sqrt
#include <iostream> // std::cerr
#include <string> // std::string
#include <chrono> // std::chrono
#include <cmath> // std::sqrt
#include <iostream> // std::cerr
#include <string> // std::string
#include <string_view> // std::string_view

#include "command.h"
#include "configuration.h"
Expand All @@ -36,8 +37,8 @@ auto get_precompiled_schema_template_path(
auto get_schema_template(const sourcemeta::core::JSON &bundled,
const sourcemeta::core::SchemaResolver &resolver,
const sourcemeta::core::SchemaFrame &frame,
const std::optional<std::string> &default_dialect,
const std::optional<std::string> &default_id,
const std::string_view default_dialect,
const std::string_view default_id,
const bool fast_mode,
const sourcemeta::core::Options &options)
-> sourcemeta::blaze::Template {
Expand Down
5 changes: 3 additions & 2 deletions src/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,11 @@ inline auto print_exception(const bool is_json, const Exception &exception)
}

if constexpr (requires(const Exception &current) {
{ current.option() } -> std::convertible_to<std::string>;
{ current.option() } -> std::convertible_to<std::string_view>;
}) {
if (is_json) {
error_json.assign("option", sourcemeta::core::JSON{exception.option()});
error_json.assign(
"option", sourcemeta::core::JSON{std::string{exception.option()}});
} else {
std::cerr << " at option " << exception.option() << "\n";
}
Expand Down
12 changes: 6 additions & 6 deletions src/resolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class CustomResolver {
CustomResolver(
const sourcemeta::core::Options &options,
const std::optional<sourcemeta::core::SchemaConfig> &configuration,
const bool remote, const std::optional<std::string> &default_dialect)
const bool remote, const std::string_view default_dialect)
: options_{options}, configuration_{configuration}, remote_{remote} {
if (options.contains("resolve")) {
for (const auto &entry : for_each_json(options.at("resolve"), options)) {
Expand Down Expand Up @@ -121,8 +121,8 @@ class CustomResolver {
}

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

inline auto
resolver(const sourcemeta::core::Options &options, const bool remote,
const std::optional<std::string> &default_dialect,
const std::string_view default_dialect,
const std::optional<sourcemeta::core::SchemaConfig> &configuration)
-> const CustomResolver & {
using CacheKey = std::pair<bool, std::optional<std::string>>;
using CacheKey = std::pair<bool, std::string>;
static std::map<CacheKey, CustomResolver> resolver_cache;
const CacheKey cache_key{remote, default_dialect};
const CacheKey cache_key{remote, std::string{default_dialect}};

// Check if resolver is already cached
auto iterator{resolver_cache.find(cache_key)};
Expand Down
27 changes: 14 additions & 13 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,29 @@

#include <sourcemeta/blaze/output.h>

#include <cassert> // assert
#include <iterator> // std::next
#include <optional> // std::optional
#include <ostream> // std::ostream
#include <sstream> // std::ostringstream
#include <string> // std::string, std::stoull
#include <tuple> // std::get
#include <variant> // std::visit
#include <cassert> // assert
#include <iterator> // std::next
#include <optional> // std::optional
#include <ostream> // std::ostream
#include <string> // std::string, std::stoull
#include <string_view> // std::string_view
#include <tuple> // std::get
#include <variant> // std::visit

namespace sourcemeta::jsonschema {

inline auto default_dialect(
const sourcemeta::core::Options &options,
const std::optional<sourcemeta::core::SchemaConfig> &configuration)
-> std::optional<std::string> {
-> std::string_view {
if (options.contains("default-dialect")) {
return std::string{options.at("default-dialect").front()};
} else if (configuration.has_value()) {
return configuration.value().default_dialect;
return options.at("default-dialect").front();
} else if (configuration.has_value() &&
configuration.value().default_dialect.has_value()) {
return configuration.value().default_dialect.value();
}

return std::nullopt;
return "";
}

inline auto parse_indentation(const sourcemeta::core::Options &options)
Expand Down
Loading
Loading