Skip to content
Open
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 include/rfl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include "rfl/Processors.hpp"
#include "rfl/Ref.hpp"
#include "rfl/Rename.hpp"
#include "rfl/SchemaExt.hpp"
#include "rfl/Settings.hpp"
#include "rfl/Short.hpp"
#include "rfl/Size.hpp"
Expand Down
196 changes: 196 additions & 0 deletions include/rfl/SchemaExt.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
#ifndef RFL_SCHEMAEXT_HPP_
#define RFL_SCHEMAEXT_HPP_

#include <concepts>
#include <optional>
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>
#include <vector>

#include "Generic.hpp"
#include "Object.hpp"
#include "Processors.hpp"

namespace rfl {

/// Optional Draft 2020-12 JSON Schema annotations (`title`, `examples`, and
/// arbitrary `x-*` extension properties) attachable via `rfl::SchemaExtension`.
struct SchemaAnnotations {
std::optional<std::string> title = std::nullopt;
std::optional<std::vector<Generic>> examples = std::nullopt;
Object<Generic> extensions = {};

bool empty() const noexcept {
return (!title.has_value() || title->empty()) &&
(!examples.has_value() || examples->empty()) &&
extensions.empty();
}
};

/// Specialize `rfl::SchemaName<T>` to override the `$defs` identifier emitted
/// for type `T` (instead of the default mangled C++ type name).
///
/// Supported members:
/// - `static constexpr std::string_view value = "ExportConfig";` OR
/// - `static std::string_view value();` (or returning `std::string`).
template <class T>
struct SchemaName {};

/// Specialize `rfl::SchemaExtension<T>` on a custom wrapper type (such as
/// `ConfigOption<T>`) or struct to attach `title`, `examples`, and arbitrary
/// `x-*` extension keywords to its generated JSON Schema node.
///
/// Supported members:
/// - `static rfl::SchemaAnnotations annotations();` AND/OR
/// - `static rfl::Object<rfl::Generic> extensions();`
/// - `static constexpr bool inline_schema = true;` (optional, for reflection
/// wrappers that should emit their schema inline rather than via `$defs`).
template <class T>
struct SchemaExtension {};

/// Opt-in JSON Schema processor: emit `minimum` and `maximum` bounds matching
/// C++ integer widths (`int8_t`..`int64_t`, `uint8_t`..`uint64_t`).
struct IntegerBounds {
template <class StructType>
static auto process(auto&& _named_tuple) {
return std::forward<decltype(_named_tuple)>(_named_tuple);
}
};

/// Opt-in JSON Schema processor: emit `minItems` and `maxItems` equal to
/// `prefixItems.size()` on fixed-arity tuple schemas.
struct TupleBounds {
template <class StructType>
static auto process(auto&& _named_tuple) {
return std::forward<decltype(_named_tuple)>(_named_tuple);
}
};

/// Opt-in JSON Schema processor: omit `"required": []` on object schemas that
/// have no required properties.
struct OmitEmptyRequired {
template <class StructType>
static auto process(auto&& _named_tuple) {
return std::forward<decltype(_named_tuple)>(_named_tuple);
}
};

namespace internal {

template <class Tag, class T>
struct has_schema_processor : std::false_type {};

template <class Tag>
struct has_schema_processor<Tag, Tag> : std::true_type {};

template <class Tag, class... Ps>
struct has_schema_processor<Tag, Processors<Ps...>>
: std::bool_constant<(has_schema_processor<Tag, Ps>::value || ...)> {};

template <class ProcessorsType>
inline constexpr bool integer_bounds_v =
has_schema_processor<IntegerBounds,
std::remove_cvref_t<ProcessorsType>>::value;

template <class ProcessorsType>
inline constexpr bool tuple_bounds_v =
has_schema_processor<TupleBounds,
std::remove_cvref_t<ProcessorsType>>::value;

template <class ProcessorsType>
inline constexpr bool omit_empty_required_v =
has_schema_processor<OmitEmptyRequired,
std::remove_cvref_t<ProcessorsType>>::value;

// --- SchemaName detection ---

template <class T>
concept HasSchemaNameTraitVar = requires {
{ SchemaName<T>::value } -> std::convertible_to<std::string_view>;
};

template <class T>
concept HasSchemaNameTraitFn = requires {
{ SchemaName<T>::value() } -> std::convertible_to<std::string_view>;
};

template <class T>
inline constexpr bool has_schema_name_v =
HasSchemaNameTraitVar<std::remove_cvref_t<T>> ||
HasSchemaNameTraitFn<std::remove_cvref_t<T>>;

template <class T>
inline std::string get_schema_name() {
using U = std::remove_cvref_t<T>;
if constexpr (HasSchemaNameTraitFn<U>) {
return std::string(std::string_view(SchemaName<U>::value()));
} else if constexpr (HasSchemaNameTraitVar<U>) {
return std::string(std::string_view(SchemaName<U>::value));
} else {
return {};
}
}

// --- SchemaExtension annotations ---

template <class T>
concept HasSchemaExtensionAnnotationsFn = requires {
{ SchemaExtension<T>::annotations() } -> std::convertible_to<SchemaAnnotations>;
};

template <class T>
concept HasSchemaExtensionExtensionsFn = requires {
{ SchemaExtension<T>::extensions() } -> std::convertible_to<Object<Generic>>;
};

template <class T>
concept HasSchemaExtensionInlineFlag = requires {
{ SchemaExtension<T>::inline_schema } -> std::convertible_to<bool>;
};

template <class T>
consteval bool schema_extension_inline() {
using U = std::remove_cvref_t<T>;
if constexpr (HasSchemaExtensionInlineFlag<U>) {
return static_cast<bool>(SchemaExtension<U>::inline_schema);
} else {
return false;
}
}

template <class T>
inline constexpr bool schema_extension_inline_v = schema_extension_inline<T>();

template <class T>
inline constexpr bool has_schema_annotations_v =
HasSchemaExtensionAnnotationsFn<std::remove_cvref_t<T>> ||
HasSchemaExtensionExtensionsFn<std::remove_cvref_t<T>>;

template <class T>
inline SchemaAnnotations get_schema_annotations() {
using U = std::remove_cvref_t<T>;
SchemaAnnotations out;
if constexpr (HasSchemaExtensionAnnotationsFn<U>) {
auto ext = SchemaExtension<U>::annotations();
if (ext.title && !ext.title->empty()) out.title = std::move(ext.title);
if (ext.examples && !ext.examples->empty()) {
out.examples = std::move(ext.examples);
}
for (const auto& [k, v] : ext.extensions) {
out.extensions[k] = v;
}
}
if constexpr (HasSchemaExtensionExtensionsFn<U>) {
for (const auto& [k, v] : SchemaExtension<U>::extensions()) {
out.extensions[k] = v;
}
}
return out;
}

} // namespace internal
} // namespace rfl

#endif
12 changes: 8 additions & 4 deletions include/rfl/capnproto/is_named_type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ namespace rfl::capnproto {
inline bool is_named_type(const parsing::schema::Type& _type) {
return _type.variant_.visit([&](const auto& _v) -> bool {
using T = std::remove_cvref_t<decltype(_v)>;
return std::is_same<T, parsing::schema::Type::Object>() ||
std::is_same<T, parsing::schema::Type::AnyOf>() ||
std::is_same<T, parsing::schema::Type::StringMap>() ||
std::is_same<T, parsing::schema::Type::Literal>();
if constexpr (std::is_same_v<T, parsing::schema::Type::Annotated>) {
return is_named_type(*_v.type_);
} else {
return std::is_same<T, parsing::schema::Type::Object>() ||
std::is_same<T, parsing::schema::Type::AnyOf>() ||
std::is_same<T, parsing::schema::Type::StringMap>() ||
std::is_same<T, parsing::schema::Type::Literal>();
}
});
}

Expand Down
14 changes: 12 additions & 2 deletions include/rfl/json/schema/Type.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#ifndef RFL_JSON_SCHEMA_TYPE_HPP_
#define RFL_JSON_SCHEMA_TYPE_HPP_

#include <cstdint>
#include <memory>
#include <optional>
#include <string>
#include <vector>

#include "../../ExtraFields.hpp"
#include "../../Flatten.hpp"
#include "../../Generic.hpp"
#include "../../Literal.hpp"
Expand All @@ -19,20 +22,25 @@ namespace rfl::json::schema {
struct Type {
/// Common annotation fields shared by all schema subtypes.
struct Annotations {
std::optional<std::string> title{};
std::optional<std::string> description{};
std::optional<bool> deprecated{};
std::optional<std::string> deprecationMessage{};
rfl::Rename<"default", std::optional<rfl::Generic>> defaultValue{};
std::optional<std::vector<rfl::Generic>> examples{};
rfl::ExtraFields<rfl::Generic> extensions{};
};

struct Boolean {
rfl::Flatten<Annotations> annotations{};
Literal<"boolean"> type{};
rfl::Flatten<Annotations> annotations{};
};

struct Integer {
Literal<"integer"> type{};
rfl::Flatten<Annotations> annotations{};
std::optional<int64_t> minimum{};
std::optional<rfl::Variant<int64_t, uint64_t>> maximum{};
};

struct Number {
Expand Down Expand Up @@ -101,7 +109,7 @@ struct Type {
Literal<"object"> type{};
rfl::Flatten<Annotations> annotations{};
rfl::Object<Type> properties{};
std::vector<std::string> required{};
std::optional<std::vector<std::string>> required{};
std::optional<rfl::Variant<std::shared_ptr<Type>, bool>>
additionalProperties{};
};
Expand Down Expand Up @@ -144,6 +152,8 @@ struct Type {
rfl::Flatten<Annotations> annotations{};
std::vector<Type> prefixItems{};
bool items = false;
std::optional<size_t> minItems{};
std::optional<size_t> maxItems{};
};

struct TypedArray {
Expand Down
19 changes: 17 additions & 2 deletions include/rfl/json/to_schema.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <string>

#include "../Processors.hpp"
#include "../SchemaExt.hpp"
#include "../Variant.hpp"
#include "../common.hpp"
#include "../internal/default_if_missing_v.hpp"
Expand All @@ -28,6 +29,16 @@ struct TypeHelper<rfl::Variant<Ts...>> {
using JSONSchemaType = rfl::Variant<schema::JSONSchema<Ts>...>;
};

struct SchemaRenderOptions {
bool no_required = false;
bool omit_empty_required = false;
bool tuple_bounds = false;
};

RFL_API std::string to_schema_internal_schema(
const parsing::schema::Definition& internal_schema, const yyjson_write_flag,
const SchemaRenderOptions& _options, const std::string& comment = "");

RFL_API std::string to_schema_internal_schema(
const parsing::schema::Definition& internal_schema, const yyjson_write_flag,
const bool _no_required, const std::string& comment = "");
Expand All @@ -38,8 +49,12 @@ std::string to_schema(const yyjson_write_flag _flag = 0,
const std::string& comment = "") {
using P = Processors<Ps...>;
const auto internal_schema = parsing::schema::make<Reader, Writer, T, P>();
return to_schema_internal_schema(internal_schema, _flag,
internal::default_if_missing_v<P>, comment);
const SchemaRenderOptions options{
.no_required = internal::default_if_missing_v<P>,
.omit_empty_required = internal::omit_empty_required_v<P>,
.tuple_bounds = internal::tuple_bounds_v<P>,
};
return to_schema_internal_schema(internal_schema, _flag, options, comment);
}
} // namespace rfl::json

Expand Down
19 changes: 19 additions & 0 deletions include/rfl/parsing/ParserBasicType.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#ifndef RFL_PARSING_PARSER_BASIC_TYPE_HPP_
#define RFL_PARSING_PARSER_BASIC_TYPE_HPP_

#include <cstdint>
#include <limits>
#include <map>
#include <type_traits>

#include "../Result.hpp"
#include "../SchemaExt.hpp"
#include "../always_false.hpp"
#include "../enums.hpp"
#include "../from_named_tuple.hpp"
Expand Down Expand Up @@ -78,6 +81,22 @@ struct ParserBasicType {
if constexpr (std::is_same<U, bool>()) {
return Type{Type::Boolean{}};

} else if constexpr (std::is_integral_v<U> &&
internal::integer_bounds_v<ProcessorsType> &&
!schemaful::IsSchemafulReader<R> &&
!schemaful::IsSchemafulWriter<W>) {
if constexpr (std::is_unsigned_v<U>) {
return Type{Type::BoundedInteger{
.minimum_ = 0,
.maximum_ = rfl::Variant<int64_t, uint64_t>(
static_cast<uint64_t>(std::numeric_limits<U>::max()))}};
} else {
return Type{Type::BoundedInteger{
.minimum_ = static_cast<int64_t>(std::numeric_limits<U>::min()),
.maximum_ = rfl::Variant<int64_t, uint64_t>(
static_cast<int64_t>(std::numeric_limits<U>::max()))}};
}

} else if constexpr (std::is_same<U, std::int32_t>()) {
return Type{Type::Int32{}};

Expand Down
Loading
Loading