Skip to content

Commit 81cf10a

Browse files
authored
Implement a SchemaConfig::applies_to helper (#2078)
Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
1 parent 626949e commit 81cf10a

File tree

5 files changed

+119
-2
lines changed

5 files changed

+119
-2
lines changed

src/extension/schemaconfig/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
sourcemeta_library(NAMESPACE sourcemeta PROJECT core NAME schemaconfig
2-
PRIVATE_HEADERS error.h SOURCES parse.cc find.cc)
2+
PRIVATE_HEADERS error.h SOURCES parse.cc schemaconfig.cc)
33

44
if(SOURCEMETA_CORE_INSTALL)
55
sourcemeta_library_install(NAMESPACE sourcemeta PROJECT core NAME schemaconfig)

src/extension/schemaconfig/include/sourcemeta/core/schemaconfig.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ struct SOURCEMETA_CORE_SCHEMACONFIG_EXPORT SchemaConfig {
4949
std::unordered_map<JSON::String, JSON::String> resolve;
5050
JSON extra = JSON::make_object();
5151

52+
/// Check if the given path represents a schema described by this
53+
/// configuration
54+
[[nodiscard]]
55+
auto applies_to(const std::filesystem::path &path) const -> bool;
56+
5257
/// Parse a configuration file from its contents
5358
[[nodiscard]]
5459
static auto from_json(const JSON &value,

src/extension/schemaconfig/find.cc renamed to src/extension/schemaconfig/schemaconfig.cc

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#include <sourcemeta/core/io.h>
22
#include <sourcemeta/core/schemaconfig.h>
33

4-
#include <cassert> // assert
4+
#include <algorithm> // std::ranges::any_of
5+
#include <cassert> // assert
6+
#include <string> // std::string
57

68
namespace sourcemeta::core {
79

@@ -31,4 +33,11 @@ auto SchemaConfig::find(const std::filesystem::path &path)
3133
return std::nullopt;
3234
}
3335

36+
auto SchemaConfig::applies_to(const std::filesystem::path &path) const -> bool {
37+
const std::string filename{path.filename().string()};
38+
return std::ranges::any_of(this->extension, [&filename](const auto &suffix) {
39+
return filename.ends_with(suffix);
40+
});
41+
}
42+
3443
} // namespace sourcemeta::core

test/schemaconfig/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
sourcemeta_googletest(NAMESPACE sourcemeta PROJECT core NAME schemaconfig
22
SOURCES
3+
schemaconfig_applies_to_test.cc
34
schemaconfig_find_test.cc
45
schemaconfig_from_json_test.cc
56
schemaconfig_read_json_test.cc)
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#include <gtest/gtest.h>
2+
3+
#include <sourcemeta/core/json.h>
4+
#include <sourcemeta/core/schemaconfig.h>
5+
6+
TEST(SchemaConfig_applies_to, default_extensions) {
7+
const auto input{sourcemeta::core::parse_json(R"JSON({
8+
"baseUri": "https://example.com"
9+
})JSON")};
10+
11+
const auto config{
12+
sourcemeta::core::SchemaConfig::from_json(input, TEST_DIRECTORY)};
13+
14+
EXPECT_TRUE(config.applies_to("foo.json"));
15+
EXPECT_TRUE(config.applies_to("bar.yaml"));
16+
EXPECT_TRUE(config.applies_to("baz.yml"));
17+
EXPECT_FALSE(config.applies_to("qux.schema"));
18+
EXPECT_FALSE(config.applies_to("no_extension"));
19+
}
20+
21+
TEST(SchemaConfig_applies_to, single_extension_match) {
22+
const auto input{sourcemeta::core::parse_json(R"JSON({
23+
"baseUri": "https://example.com",
24+
"extension": ".json"
25+
})JSON")};
26+
27+
const auto config{
28+
sourcemeta::core::SchemaConfig::from_json(input, TEST_DIRECTORY)};
29+
30+
EXPECT_TRUE(config.applies_to("foo.json"));
31+
EXPECT_TRUE(config.applies_to("/path/to/schema.json"));
32+
}
33+
34+
TEST(SchemaConfig_applies_to, single_extension_no_match) {
35+
const auto input{sourcemeta::core::parse_json(R"JSON({
36+
"baseUri": "https://example.com",
37+
"extension": ".json"
38+
})JSON")};
39+
40+
const auto config{
41+
sourcemeta::core::SchemaConfig::from_json(input, TEST_DIRECTORY)};
42+
43+
EXPECT_FALSE(config.applies_to("foo.yaml"));
44+
EXPECT_FALSE(config.applies_to("foo.schema"));
45+
EXPECT_FALSE(config.applies_to("no_extension"));
46+
}
47+
48+
TEST(SchemaConfig_applies_to, multiple_extensions_match) {
49+
const auto input{sourcemeta::core::parse_json(R"JSON({
50+
"baseUri": "https://example.com",
51+
"extension": [ ".json", ".yaml", ".yml" ]
52+
})JSON")};
53+
54+
const auto config{
55+
sourcemeta::core::SchemaConfig::from_json(input, TEST_DIRECTORY)};
56+
57+
EXPECT_TRUE(config.applies_to("foo.json"));
58+
EXPECT_TRUE(config.applies_to("bar.yaml"));
59+
EXPECT_TRUE(config.applies_to("baz.yml"));
60+
}
61+
62+
TEST(SchemaConfig_applies_to, multiple_extensions_no_match) {
63+
const auto input{sourcemeta::core::parse_json(R"JSON({
64+
"baseUri": "https://example.com",
65+
"extension": [ ".json", ".yaml" ]
66+
})JSON")};
67+
68+
const auto config{
69+
sourcemeta::core::SchemaConfig::from_json(input, TEST_DIRECTORY)};
70+
71+
EXPECT_FALSE(config.applies_to("foo.schema"));
72+
EXPECT_FALSE(config.applies_to("bar.txt"));
73+
EXPECT_FALSE(config.applies_to("no_extension"));
74+
}
75+
76+
TEST(SchemaConfig_applies_to, double_extension_match) {
77+
const auto input{sourcemeta::core::parse_json(R"JSON({
78+
"baseUri": "https://example.com",
79+
"extension": ".json"
80+
})JSON")};
81+
82+
const auto config{
83+
sourcemeta::core::SchemaConfig::from_json(input, TEST_DIRECTORY)};
84+
85+
EXPECT_TRUE(config.applies_to("foo.schema.json"));
86+
EXPECT_TRUE(config.applies_to("/path/to/bar.test.json"));
87+
}
88+
89+
TEST(SchemaConfig_applies_to, compound_extension_match) {
90+
const auto input{sourcemeta::core::parse_json(R"JSON({
91+
"baseUri": "https://example.com",
92+
"extension": ".schema.json"
93+
})JSON")};
94+
95+
const auto config{
96+
sourcemeta::core::SchemaConfig::from_json(input, TEST_DIRECTORY)};
97+
98+
EXPECT_TRUE(config.applies_to("foo.schema.json"));
99+
EXPECT_TRUE(config.applies_to("/path/to/bar.schema.json"));
100+
EXPECT_FALSE(config.applies_to("baz.json"));
101+
EXPECT_FALSE(config.applies_to("qux.schema.yaml"));
102+
}

0 commit comments

Comments
 (0)