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 docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ contain the actual schema definitions that power your instance.
| `/ignore` | Array | No | None | An array of file paths (relative to the configuration file location) to exclude from the schema collection. See the [JSON Schema CLI configuration](https://github.com/sourcemeta/jsonschema/blob/main/docs/configuration.markdown) for more information |
| `/x-sourcemeta-one:evaluate` | Boolean | No | `true` | When set to `false`, disable the evaluation API for this schema collection. This is useful if you will never make use of the [evaluation API](api.md) and want to speed up the generation of the instance |
| `/x-sourcemeta-one:alert` | String | No | N/A | When set, provide a human-readable alert on both the API and the HTML explorer for every schema in the collection. This is useful to provide any important message to consumers. The web explorer renders this as Markdown |
| `/x-sourcemeta-one:priority` | Integer | No | `100` | A hint, from `0` (least important) to `100` (most important), that signals the relative importance of this collection compared to others in the same instance. Consumers may use this to rank or filter collections |
Comment thread
jviotti marked this conversation as resolved.

!!! warning

Expand Down
13 changes: 13 additions & 0 deletions src/configuration/include/sourcemeta/one/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#include <sourcemeta/one/configuration_error.h>

#include <algorithm> // std::clamp
#include <cstdint> // std::uint8_t
#include <filesystem> // std::filesystem::path
#include <optional> // std::optional
#include <string> // std::string
Expand Down Expand Up @@ -70,6 +72,17 @@ struct Configuration {
return value == nullptr || !value->is_boolean() || value->to_boolean();
}

[[nodiscard]] static auto priority(const Collection &collection)
-> std::uint8_t {
const auto *value{collection.extra.try_at("x-sourcemeta-one:priority")};
if (value == nullptr || !value->is_integer()) {
return 100;
}
return static_cast<std::uint8_t>(
std::clamp<sourcemeta::core::JSON::Integer>(value->to_integer(), 0,
100));
}

[[nodiscard]] auto
is_collection_base(const std::filesystem::path &relative_path) const -> bool {
const auto match{this->entries.find(relative_path)};
Expand Down
24 changes: 16 additions & 8 deletions src/configuration/read.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ auto dereference(const std::filesystem::path &base,
sourcemeta::core::JSON &input,
const sourcemeta::core::Pointer &location,
std::unordered_set<std::string> &visited,
std::unordered_set<std::string> &all_files, const bool is_root)
-> void {
std::unordered_set<std::string> &all_files, const bool is_root,
const std::filesystem::path &self_path) -> void {
assert(base.is_absolute());
if (!input.is_object()) {
return;
Expand All @@ -74,7 +74,7 @@ auto dereference(const std::filesystem::path &base,
auto extension{read_file(base, new_location, target_path)};
if (extension.is_object()) {
dereference(target_path, extension, new_location, visited, all_files,
true);
true, self_path);
accumulator.merge(std::move(extension).as_object());
}

Expand All @@ -86,7 +86,7 @@ auto dereference(const std::filesystem::path &base,
accumulator.merge(input.as_object());
input = std::move(accumulator);
assert(!input.defines("extends"));
dereference(base, input, location, visited, all_files, is_root);
dereference(base, input, location, visited, all_files, is_root, self_path);

// Read included files
} else if (!location.empty() && input.defines("include") &&
Expand All @@ -103,7 +103,8 @@ auto dereference(const std::filesystem::path &base,
}
all_files.emplace(target_path.native());
input.into(read_file(base, new_location, target_path));
dereference(target_path, input, new_location, visited, all_files, is_root);
dereference(target_path, input, new_location, visited, all_files, is_root,
self_path);
visited.erase(target_path.native());

// Revisit and relativize paths
Expand All @@ -117,6 +118,12 @@ auto dereference(const std::filesystem::path &base,
input.assign("x-sourcemeta-one:path",
sourcemeta::core::JSON{base.string()});
}
if (!input.defines("x-sourcemeta-one:priority") &&
sourcemeta::core::is_under_path(base, self_path)) {
input.assign("x-sourcemeta-one:priority",
sourcemeta::core::JSON{
static_cast<sourcemeta::core::JSON::Integer>(0)});
}

// Recurse on children, if any
} else if (input.defines("contents") && input.at("contents").is_object()) {
Expand All @@ -128,8 +135,8 @@ auto dereference(const std::filesystem::path &base,
[](const auto &entry) { return entry.first; });
for (const auto &key : keys) {
dereference(base, input.at("contents").at(key),
location.concat({"contents", key}), visited, all_files,
false);
location.concat({"contents", key}), visited, all_files, false,
self_path);
}
}
}
Expand Down Expand Up @@ -210,7 +217,8 @@ auto Configuration::read(const std::filesystem::path &configuration_path,
configuration_files.emplace(canonical_config);
std::unordered_set<std::string> visited;
visited.emplace(canonical_config);
dereference(configuration_path, data, {}, visited, configuration_files, true);
dereference(configuration_path, data, {}, visited, configuration_files, true,
self_path);

if (data.is_object() && data.defines("url") && data.defines("contents") &&
data.at("contents").is_object()) {
Expand Down
5 changes: 5 additions & 0 deletions src/configuration/schema/collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
"x-sourcemeta-one:path": {
"type": "string"
},
"x-sourcemeta-one:priority": {
"type": "integer",
"maximum": 100,
"minimum": 0
},
"baseUri": {
"$comment": "TODO: Use Format Assertion when supported on Blaze",
"type": "string",
Expand Down
1 change: 1 addition & 0 deletions test/cli/index/common/configuration-long.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ cat << EOF > "$TMP/expected.txt"
"schemas": {
"path": "$ONE_PREFIX/share/sourcemeta/one/self/v1/schemas",
"x-sourcemeta-one:path": "$ONE_PREFIX/share/sourcemeta/one/self/v1/one.json",
"x-sourcemeta-one:priority": 0,
"baseUri": "https://sourcemeta.com/"
}
}
Expand Down
1 change: 1 addition & 0 deletions test/cli/index/common/configuration-short.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ cat << EOF > "$TMP/expected.txt"
"schemas": {
"path": "$ONE_PREFIX/share/sourcemeta/one/self/v1/schemas",
"x-sourcemeta-one:path": "$ONE_PREFIX/share/sourcemeta/one/self/v1/one.json",
"x-sourcemeta-one:priority": 0,
"baseUri": "https://sourcemeta.com/"
}
}
Expand Down
18 changes: 17 additions & 1 deletion test/unit/configuration/configuration_read_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ TEST(Configuration_read, read_valid_001) {
"schemas": {
"path": "SELF_DIRECTORY/v1/schemas",
"x-sourcemeta-one:path": "SELF_DIRECTORY/v1/one.json",
"x-sourcemeta-one:priority": 0,
"baseUri": "http://localhost:8000"
}
}
Expand Down Expand Up @@ -107,6 +108,7 @@ TEST(Configuration_read, read_valid_002) {
"schemas": {
"path": "SELF_DIRECTORY/v1/schemas",
"x-sourcemeta-one:path": "SELF_DIRECTORY/v1/one.json",
"x-sourcemeta-one:priority": 0,
"baseUri": "http://localhost:8000"
}
}
Expand Down Expand Up @@ -160,6 +162,7 @@ TEST(Configuration_read, read_valid_003) {
"schemas": {
"path": "SELF_DIRECTORY/v1/schemas",
"x-sourcemeta-one:path": "SELF_DIRECTORY/v1/one.json",
"x-sourcemeta-one:priority": 0,
"baseUri": "http://localhost:8000"
}
}
Expand Down Expand Up @@ -212,6 +215,7 @@ TEST(Configuration_read, read_valid_004) {
"schemas": {
"path": "SELF_DIRECTORY/v1/schemas",
"x-sourcemeta-one:path": "SELF_DIRECTORY/v1/one.json",
"x-sourcemeta-one:priority": 0,
"baseUri": "http://localhost:8000"
}
}
Expand Down Expand Up @@ -267,6 +271,7 @@ TEST(Configuration_read, read_valid_005) {
"schemas": {
"path": "SELF_DIRECTORY/v1/schemas",
"x-sourcemeta-one:path": "SELF_DIRECTORY/v1/one.json",
"x-sourcemeta-one:priority": 0,
"baseUri": "http://localhost:8000"
}
}
Expand Down Expand Up @@ -322,6 +327,7 @@ TEST(Configuration_read, read_valid_006) {
"schemas": {
"path": "SELF_DIRECTORY/v1/schemas",
"x-sourcemeta-one:path": "SELF_DIRECTORY/v1/one.json",
"x-sourcemeta-one:priority": 0,
"baseUri": "http://localhost:8000"
}
}
Expand Down Expand Up @@ -363,6 +369,7 @@ TEST(Configuration_read, read_valid_007) {
"schemas": {
"path": "SELF_DIRECTORY/v1/schemas",
"x-sourcemeta-one:path": "SELF_DIRECTORY/v1/one.json",
"x-sourcemeta-one:priority": 0,
"baseUri": "http://localhost:8000"
}
}
Expand Down Expand Up @@ -409,6 +416,7 @@ TEST(Configuration_read, read_valid_008) {
"schemas": {
"path": "SELF_DIRECTORY/v1/schemas",
"x-sourcemeta-one:path": "SELF_DIRECTORY/v1/one.json",
"x-sourcemeta-one:priority": 0,
"baseUri": "http://localhost:8000"
}
}
Expand Down Expand Up @@ -455,6 +463,7 @@ TEST(Configuration_read, read_valid_009) {
"schemas": {
"path": "SELF_DIRECTORY/v1/schemas",
"x-sourcemeta-one:path": "SELF_DIRECTORY/v1/one.json",
"x-sourcemeta-one:priority": 0,
"baseUri": "http://localhost:8000"
}
}
Expand Down Expand Up @@ -498,6 +507,7 @@ TEST(Configuration_read, read_valid_010) {
"schemas": {
"path": "SELF_DIRECTORY/v1/schemas",
"x-sourcemeta-one:path": "SELF_DIRECTORY/v1/one.json",
"x-sourcemeta-one:priority": 0,
"baseUri": "http://localhost:8000"
}
}
Expand Down Expand Up @@ -537,7 +547,8 @@ TEST(Configuration_read, read_valid_011) {
"contents": {
"schemas": {
"path": "SELF_DIRECTORY/v1/schemas",
"x-sourcemeta-one:path": "SELF_DIRECTORY/v1/one.json"
"x-sourcemeta-one:path": "SELF_DIRECTORY/v1/one.json",
"x-sourcemeta-one:priority": 0
}
}
}
Expand Down Expand Up @@ -584,6 +595,7 @@ TEST(Configuration_read, read_valid_012) {
"schemas": {
"path": "SELF_DIRECTORY/v1/schemas",
"x-sourcemeta-one:path": "SELF_DIRECTORY/v1/one.json",
"x-sourcemeta-one:priority": 0,
"baseUri": "http://localhost:8000"
}
}
Expand Down Expand Up @@ -636,6 +648,7 @@ TEST(Configuration_read, read_valid_013) {
"schemas": {
"path": "SELF_DIRECTORY/v1/schemas",
"x-sourcemeta-one:path": "SELF_DIRECTORY/v1/one.json",
"x-sourcemeta-one:priority": 0,
"baseUri": "http://localhost:8000"
}
}
Expand Down Expand Up @@ -692,6 +705,7 @@ TEST(Configuration_read, read_valid_014) {
"schemas": {
"path": "SELF_DIRECTORY/v1/schemas",
"x-sourcemeta-one:path": "SELF_DIRECTORY/v1/one.json",
"x-sourcemeta-one:priority": 0,
"baseUri": "http://localhost:8000"
}
}
Expand Down Expand Up @@ -904,6 +918,7 @@ TEST(Configuration_read, read_valid_016_api_explicit_object) {
"schemas": {
"path": "SELF_DIRECTORY/v1/schemas",
"x-sourcemeta-one:path": "SELF_DIRECTORY/v1/one.json",
"x-sourcemeta-one:priority": 0,
"baseUri": "http://localhost:8000"
}
}
Expand Down Expand Up @@ -960,6 +975,7 @@ TEST(Configuration_read, read_valid_018_api_true_coerced_to_object) {
"schemas": {
"path": "SELF_DIRECTORY/v1/schemas",
"x-sourcemeta-one:path": "SELF_DIRECTORY/v1/one.json",
"x-sourcemeta-one:priority": 0,
"baseUri": "http://localhost:8000"
}
}
Expand Down
Loading
Loading