Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
jsonschema_types.h
1#ifndef SOURCEMETA_CORE_JSONSCHEMA_TYPES_H_
2#define SOURCEMETA_CORE_JSONSCHEMA_TYPES_H_
3
4#include <sourcemeta/core/json.h>
5#include <sourcemeta/core/jsonpointer.h>
6#include <sourcemeta/core/jsonschema_vocabularies.h>
7#include <sourcemeta/core/memory.h>
8
9#include <cstdint> // std::uint8_t
10#include <format> // std::formatter, std::format_to
11#include <functional> // std::function, std::reference_wrapper
12#include <optional> // std::optional
13#include <ostream> // std::ostream
14#include <span> // std::span
15#include <sstream> // std::ostringstream
16#include <string> // std::string
17#include <string_view> // std::string_view
18
19namespace sourcemeta::core {
20
26
41using SchemaResolver = std::function<SchemaResolverResult(std::string_view)>;
42
45enum class SchemaReferenceType : std::uint8_t {
50};
51
86
89SOURCEMETA_CORE_JSONSCHEMA_EXPORT
90auto operator<<(std::ostream &stream, const SchemaBaseDialect base_dialect)
91 -> std::ostream &;
92
93#if defined(__GNUC__)
94#pragma GCC diagnostic push
95// For some strange reason, GCC on Debian 11 believes that a member of
96// an enum class (which is namespaced by definition), can shadow an
97// alias defined even on a different namespace.
98#pragma GCC diagnostic ignored "-Wshadow"
99#endif
205#if defined(__GNUC__)
206#pragma GCC diagnostic pop
207#endif
208
211struct SchemaWalkerResult {
215 std::optional<SchemaVocabularies::URIView> vocabulary;
218 std::span<const std::string_view> dependencies;
221 std::span<const std::string_view> order_dependencies;
224
225 // Prevent accidental copies, as walker results are always returned by
226 // reference
227 SchemaWalkerResult(const SchemaWalkerResult &) = delete;
228 auto operator=(const SchemaWalkerResult &) -> SchemaWalkerResult & = delete;
230 SchemaWalkerResult(SchemaWalkerResult &&) = default;
231 auto operator=(SchemaWalkerResult &&) -> SchemaWalkerResult & = default;
232 ~SchemaWalkerResult() = default;
233
237 SchemaKeywordType keyword_type,
238 std::optional<SchemaVocabularies::URIView> keyword_vocabulary,
239 std::span<const std::string_view> keyword_dependencies,
240 std::span<const std::string_view> keyword_order_dependencies,
241 sourcemeta::core::JSON::TypeSet keyword_instances)
242 : type{keyword_type}, vocabulary{keyword_vocabulary},
243 dependencies{keyword_dependencies},
244 order_dependencies{keyword_order_dependencies},
245 instances{keyword_instances} {}
246};
247
254using SchemaWalker = std::function<const SchemaWalkerResult &(
255 std::string_view, const SchemaVocabularies &)>;
256
257} // namespace sourcemeta::core
258
260template <> struct std::formatter<sourcemeta::core::SchemaBaseDialect> {
261 constexpr auto parse(std::format_parse_context &context)
262 -> decltype(context.begin()) {
263 return context.begin();
264 }
265
266 auto format(const sourcemeta::core::SchemaBaseDialect value,
267 std::format_context &context) const -> decltype(context.out()) {
268 std::ostringstream stream;
269 stream << value;
270 return std::format_to(context.out(), "{}", stream.str());
271 }
272};
274
275#endif
std::bitset< 8 > TypeSet
A set of types.
Definition json_value.h:89
std::optional< SchemaVocabularies::URIView > vocabulary
The vocabulary associated with the keyword, if any.
Definition jsonschema_types.h:215
sourcemeta::core::JSON::TypeSet instances
The JSON instance types that this keyword applies to (empty means all).
Definition jsonschema_types.h:223
SchemaKeywordType type
The walker strategy to continue traversing across the schema.
Definition jsonschema_types.h:213
constexpr SchemaWalkerResult(SchemaKeywordType keyword_type, std::optional< SchemaVocabularies::URIView > keyword_vocabulary, std::span< const std::string_view > keyword_dependencies, std::span< const std::string_view > keyword_order_dependencies, sourcemeta::core::JSON::TypeSet keyword_instances)
Definition jsonschema_types.h:236
std::span< const std::string_view > dependencies
Definition jsonschema_types.h:218
SchemaWalkerResult(SchemaWalkerResult &&)=default
Move a walker result.
std::span< const std::string_view > order_dependencies
Definition jsonschema_types.h:221
SchemaReferenceType
Definition jsonschema_types.h:45
SchemaKeywordType
Definition jsonschema_types.h:102
sourcemeta::core::OwnedOrReference< sourcemeta::core::JSON > SchemaResolverResult
Definition jsonschema_types.h:24
std::function< SchemaResolverResult(std::string_view)> SchemaResolver
Definition jsonschema_types.h:41
std::function< const SchemaWalkerResult &( std::string_view, const SchemaVocabularies &)> SchemaWalker
Definition jsonschema_types.h:254
SchemaBaseDialect
Definition jsonschema_types.h:54
@ Static
A reference that resolves at framing time.
Definition jsonschema_types.h:47
@ Dynamic
A reference that resolves at evaluation time.
Definition jsonschema_types.h:49
@ Comment
Definition jsonschema_types.h:115
@ ApplicatorValueTraverseAnyPropertyKey
Definition jsonschema_types.h:139
@ ApplicatorMembersTraversePropertyRegex
Definition jsonschema_types.h:131
@ ApplicatorMembersInPlaceSome
Definition jsonschema_types.h:171
@ ApplicatorValueInPlaceMaybe
Definition jsonschema_types.h:193
@ Assertion
The JSON Schema keyword is a non-applicator assertion.
Definition jsonschema_types.h:106
@ ApplicatorValueTraverseSomeProperty
Definition jsonschema_types.h:135
@ ApplicatorElementsInPlaceSomeNegate
Definition jsonschema_types.h:188
@ Annotation
The JSON Schema keyword is a non-applicator annotation.
Definition jsonschema_types.h:108
@ Other
The JSON Schema keyword is known but doesn't match any other type.
Definition jsonschema_types.h:112
@ Reference
The JSON Schema keyword is a reference.
Definition jsonschema_types.h:110
@ ApplicatorElementsTraverseItem
Definition jsonschema_types.h:156
@ ApplicatorValueTraverseParent
Definition jsonschema_types.h:151
@ ApplicatorElementsInPlace
Definition jsonschema_types.h:176
@ ApplicatorValueOrElementsTraverseAnyItemOrItem
Definition jsonschema_types.h:161
@ Unknown
The JSON Schema keyword is unknown.
Definition jsonschema_types.h:104
@ LocationMembers
Definition jsonschema_types.h:119
@ ApplicatorValueInPlaceOther
Definition jsonschema_types.h:198
@ ApplicatorValueInPlaceNegate
Definition jsonschema_types.h:203
@ ApplicatorValueTraverseSomeItem
Definition jsonschema_types.h:147
@ ApplicatorValueTraverseAnyItem
Definition jsonschema_types.h:143
@ ApplicatorElementsInPlaceSome
Definition jsonschema_types.h:182
@ ApplicatorMembersTraversePropertyStatic
Definition jsonschema_types.h:125
@ ApplicatorValueOrElementsInPlace
Definition jsonschema_types.h:166
@ JSON_SCHEMA_DRAFT_4
The Draft 4 validation base dialect.
Definition jsonschema_types.h:72
@ JSON_SCHEMA_DRAFT_3
The Draft 3 validation base dialect.
Definition jsonschema_types.h:76
@ JSON_SCHEMA_DRAFT_7
The Draft 7 validation base dialect.
Definition jsonschema_types.h:64
@ JSON_SCHEMA_2019_09_HYPER
The 2019-09 hyper-schema base dialect.
Definition jsonschema_types.h:62
@ JSON_SCHEMA_2019_09
The 2019-09 validation base dialect.
Definition jsonschema_types.h:60
@ JSON_SCHEMA_DRAFT_6
The Draft 6 validation base dialect.
Definition jsonschema_types.h:68
@ JSON_SCHEMA_DRAFT_0_HYPER
The Draft 0 hyper-schema base dialect.
Definition jsonschema_types.h:84
@ JSON_SCHEMA_DRAFT_4_HYPER
The Draft 4 hyper-schema base dialect.
Definition jsonschema_types.h:74
@ JSON_SCHEMA_DRAFT_3_HYPER
The Draft 3 hyper-schema base dialect.
Definition jsonschema_types.h:78
@ JSON_SCHEMA_DRAFT_7_HYPER
The Draft 7 hyper-schema base dialect.
Definition jsonschema_types.h:66
@ JSON_SCHEMA_DRAFT_2_HYPER
The Draft 2 hyper-schema base dialect.
Definition jsonschema_types.h:80
@ JSON_SCHEMA_DRAFT_6_HYPER
The Draft 6 hyper-schema base dialect.
Definition jsonschema_types.h:70
@ JSON_SCHEMA_DRAFT_1_HYPER
The Draft 1 hyper-schema base dialect.
Definition jsonschema_types.h:82
@ JSON_SCHEMA_2020_12_HYPER
The 2020-12 hyper-schema base dialect.
Definition jsonschema_types.h:58
@ JSON_SCHEMA_2020_12
The 2020-12 validation base dialect.
Definition jsonschema_types.h:56
Definition jsonschema_vocabularies.h:30
Definition jsonschema_types.h:211
Definition memory_owned_or_reference.h:48