|
1 | 1 | #include <sourcemeta/core/jsonschema.h>
|
2 | 2 |
|
3 |
| -#include <algorithm> // std::transform |
4 |
| -#include <cassert> // assert |
5 |
| -#include <cctype> // std::tolower |
6 |
| -#include <sstream> // std::ostringstream |
| 3 | +#include <cassert> // assert |
| 4 | +#include <sstream> // std::ostringstream |
7 | 5 |
|
8 | 6 | namespace sourcemeta::core {
|
9 | 7 |
|
@@ -84,98 +82,4 @@ auto SchemaMapResolver::operator()(std::string_view identifier) const
|
84 | 82 | return std::nullopt;
|
85 | 83 | }
|
86 | 84 |
|
87 |
| -SchemaFlatFileResolver::SchemaFlatFileResolver() {} |
88 |
| - |
89 |
| -SchemaFlatFileResolver::SchemaFlatFileResolver(const SchemaResolver &resolver) |
90 |
| - : default_resolver{resolver} {} |
91 |
| - |
92 |
| -static auto to_lowercase(const std::string_view input) -> std::string { |
93 |
| - std::string result{input}; |
94 |
| - std::transform(result.cbegin(), result.cend(), result.begin(), |
95 |
| - [](const auto character) { |
96 |
| - return static_cast<char>(std::tolower(character)); |
97 |
| - }); |
98 |
| - return result; |
99 |
| -} |
100 |
| - |
101 |
| -auto SchemaFlatFileResolver::add( |
102 |
| - const std::filesystem::path &path, |
103 |
| - const std::optional<std::string> &default_dialect, |
104 |
| - const std::optional<std::string> &default_id, const Reader &reader, |
105 |
| - SchemaVisitorReference &&reference_visitor) -> const std::string & { |
106 |
| - const auto canonical{std::filesystem::weakly_canonical(path)}; |
107 |
| - const auto schema{reader(canonical)}; |
108 |
| - assert(sourcemeta::core::is_schema(schema)); |
109 |
| - const auto identifier{sourcemeta::core::identify( |
110 |
| - schema, *this, SchemaIdentificationStrategy::Loose, default_dialect, |
111 |
| - default_id)}; |
112 |
| - if (!identifier.has_value() && !default_id.has_value()) { |
113 |
| - std::ostringstream error; |
114 |
| - error << "Cannot identify schema: " << canonical.string(); |
115 |
| - throw SchemaError(error.str()); |
116 |
| - } |
117 |
| - |
118 |
| - // Filesystems behave differently with regards to casing. To unify |
119 |
| - // them, assume they are case-insensitive. |
120 |
| - const auto effective_identifier{to_lowercase( |
121 |
| - default_id.has_value() ? identifier.value_or(default_id.value()) |
122 |
| - : identifier.value())}; |
123 |
| - |
124 |
| - const auto result{this->schemas.emplace( |
125 |
| - effective_identifier, |
126 |
| - Entry{canonical, default_dialect, effective_identifier, reader, |
127 |
| - reference_visitor ? std::move(reference_visitor) |
128 |
| - : reference_visitor_relativize})}; |
129 |
| - if (!result.second && result.first->second.path != canonical) { |
130 |
| - std::ostringstream error; |
131 |
| - error << "Cannot register the same identifier twice: " |
132 |
| - << effective_identifier; |
133 |
| - throw SchemaError(error.str()); |
134 |
| - } |
135 |
| - |
136 |
| - return result.first->first; |
137 |
| -} |
138 |
| - |
139 |
| -auto SchemaFlatFileResolver::reidentify(const std::string &schema, |
140 |
| - const std::string &new_identifier) |
141 |
| - -> void { |
142 |
| - const auto result{this->schemas.find(to_lowercase(schema))}; |
143 |
| - assert(result != this->schemas.cend()); |
144 |
| - this->schemas.insert_or_assign(to_lowercase(new_identifier), |
145 |
| - std::move(result->second)); |
146 |
| - this->schemas.erase(result); |
147 |
| -} |
148 |
| - |
149 |
| -auto SchemaFlatFileResolver::operator()(std::string_view identifier) const |
150 |
| - -> std::optional<JSON> { |
151 |
| - const std::string string_identifier{to_lowercase(identifier)}; |
152 |
| - const auto result{this->schemas.find(string_identifier)}; |
153 |
| - if (result != this->schemas.cend()) { |
154 |
| - auto schema{result->second.reader(result->second.path)}; |
155 |
| - assert(sourcemeta::core::is_schema(schema)); |
156 |
| - if (schema.is_object() && !schema.defines("$schema") && |
157 |
| - result->second.default_dialect.has_value()) { |
158 |
| - schema.assign("$schema", JSON{result->second.default_dialect.value()}); |
159 |
| - } |
160 |
| - |
161 |
| - sourcemeta::core::reidentify(schema, result->second.original_identifier, |
162 |
| - *this, result->second.default_dialect); |
163 |
| - // Because we allow re-identification, we can get into issues unless we |
164 |
| - // always try to relativize references |
165 |
| - sourcemeta::core::reference_visit( |
166 |
| - schema, schema_official_walker, *this, result->second.reference_visitor, |
167 |
| - result->second.default_dialect, result->second.original_identifier); |
168 |
| - sourcemeta::core::reidentify(schema, result->first, *this, |
169 |
| - result->second.default_dialect); |
170 |
| - |
171 |
| - return schema; |
172 |
| - } |
173 |
| - |
174 |
| - if (this->default_resolver) { |
175 |
| - return this->default_resolver(identifier); |
176 |
| - } |
177 |
| - |
178 |
| - return std::nullopt; |
179 |
| -} |
180 |
| - |
181 | 85 | } // namespace sourcemeta::core
|
0 commit comments