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 @@ -31,6 +31,7 @@
#include "rfl/Generic.hpp"
#include "rfl/Hex.hpp"
#include "rfl/Literal.hpp"
#include "rfl/Metadata.hpp"
#include "rfl/NamedTuple.hpp"
#include "rfl/NoExtraFields.hpp"
#include "rfl/NoFieldNames.hpp"
Expand Down
157 changes: 157 additions & 0 deletions include/rfl/Metadata.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
#ifndef RFL_METADATA_HPP_
#define RFL_METADATA_HPP_

#include <concepts>
#include <optional>
#include <ranges>
#include <span>
#include <string>
#include <string_view>
#include <type_traits>

namespace rfl {

/// Out-of-band per-field metadata (description and optional deprecation) that
/// attaches schema/documentation prose to a reflected struct WITHOUT embedding
/// long `StringLiteral` non-type template parameters into C++ field types.
struct FieldMeta {
/// Processed wire field name, or pre-processor field name (C++ member name,
/// or explicit `rfl::Rename` name when renamed). Exact wire-name matches
/// take precedence over pre-processor names.
std::string_view name;

/// Optional human-readable description emitted in generated schemas.
std::string_view description = {};

/// Optional deprecation message. When non-empty, marks the field deprecated
/// in generated schemas (with `description`, matching `rfl::Deprecated`).
std::string_view deprecation_message = {};
};

/// Specialize `rfl::Metadata<T>` to attach zero-bloat out-of-band metadata to
/// struct `T` without wrapping its fields in `rfl::Description` /
/// `rfl::Deprecated`.
///
/// Supported members on a specialization `rfl::Metadata<MyStruct>`:
/// - `static constexpr std::array<rfl::FieldMeta, N> fields = ...;`
/// (or any contiguous range / `std::span<const rfl::FieldMeta>`), OR
/// - `static std::span<const rfl::FieldMeta> fields();` (or returning a
/// `const` lvalue reference to a contiguous range; may be defined out-of-line
/// in a `.cpp` file for complete compile-time isolation).
/// - Optional struct-level description:
/// `static constexpr std::string_view description = "...";` (or a non-null
/// `const char*`) or `static std::string_view description();`
///
/// Notes:
/// - `rfl::Metadata<Inner>` attaches to `Inner`'s own schema definition
/// (`$defs/Inner`); fields flattened via `rfl::Flatten<Inner>` are looked up
/// on the enclosing struct's `rfl::Metadata<Outer>`.
/// - For `rfl::TaggedUnion` alternatives wrapped in an internal tagged-union
/// wrapper, `rfl::Metadata<Alternative>` is consulted automatically.
/// - For custom types with `ReflectionType` or `rfl::Reflector<T>`,
/// struct-level `Metadata<T>::description` is honored; `Metadata<T>::fields`
/// applies to reflected aggregate structs.
template <class T>
struct Metadata {};

namespace internal {

template <class T>
concept HasFieldMetaVar = requires {
{ Metadata<T>::fields } -> std::ranges::contiguous_range;
{ Metadata<T>::fields } -> std::ranges::sized_range;
requires !std::is_member_object_pointer_v<decltype(&Metadata<T>::fields)>;
requires std::same_as<std::remove_cvref_t<std::ranges::range_value_t<
decltype(Metadata<T>::fields)>>,
FieldMeta>;
};

template <class T>
concept HasAnyFieldMetaFn = requires {
{ Metadata<T>::fields() } -> std::ranges::contiguous_range;
{ Metadata<T>::fields() } -> std::ranges::sized_range;
requires std::same_as<std::remove_cvref_t<std::ranges::range_value_t<
decltype(Metadata<T>::fields())>>,
FieldMeta>;
};

template <class T>
concept HasFieldMetaFn =
HasAnyFieldMetaFn<T> &&
std::ranges::borrowed_range<decltype(Metadata<T>::fields())>;

template <class T>
constexpr bool has_field_metadata_v =
HasFieldMetaVar<std::remove_cvref_t<T>> ||
HasAnyFieldMetaFn<std::remove_cvref_t<T>>;

template <class T>
std::span<const FieldMeta> get_field_metadata() {
using U = std::remove_cvref_t<T>;
static_assert(
!HasAnyFieldMetaFn<U> || HasFieldMetaFn<U>,
"rfl::Metadata<T>::fields() must return a borrowed contiguous range "
"(for example, std::span<const rfl::FieldMeta> or const auto&) so the "
"returned view does not dangle.");
if constexpr (HasFieldMetaFn<U>) {
auto&& r = Metadata<U>::fields();
return std::span<const FieldMeta>(std::ranges::data(r),
std::ranges::size(r));
} else if constexpr (HasFieldMetaVar<U>) {
return std::span<const FieldMeta>(std::ranges::data(Metadata<U>::fields),
std::ranges::size(Metadata<U>::fields));
} else {
return {};
}
}

template <class T>
concept HasStructDescriptionVar = requires {
{ Metadata<T>::description } -> std::convertible_to<std::string_view>;
requires !std::is_member_object_pointer_v<decltype(&Metadata<T>::description)>;
};

template <class T>
concept HasStructDescriptionFn = requires {
{ Metadata<T>::description() } -> std::convertible_to<std::string_view>;
};

template <class T>
constexpr bool has_struct_description_v =
HasStructDescriptionVar<std::remove_cvref_t<T>> ||
HasStructDescriptionFn<std::remove_cvref_t<T>>;

template <class T>
std::string get_struct_description() {
using U = std::remove_cvref_t<T>;
if constexpr (HasStructDescriptionFn<U>) {
return std::string(std::string_view(Metadata<U>::description()));
} else if constexpr (HasStructDescriptionVar<U>) {
return std::string(std::string_view(Metadata<U>::description));
} else {
return {};
}
}

inline std::optional<FieldMeta> find_field_metadata(
const std::span<const FieldMeta> _meta, const std::string_view _wire_name,
const std::string_view _raw_name = {}) noexcept {
for (const auto& entry : _meta) {
if (entry.name == _wire_name) {
return entry;
}
}
if (!_raw_name.empty() && _raw_name != _wire_name) {
for (const auto& entry : _meta) {
if (entry.name == _raw_name) {
return entry;
}
}
}
return std::nullopt;
}

} // namespace internal
} // namespace rfl

#endif
15 changes: 11 additions & 4 deletions include/rfl/capnproto/is_named_type.hpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
#ifndef RFL_CAPNPROTO_IS_NAMED_TYPE_HPP_
#define RFL_CAPNPROTO_IS_NAMED_TYPE_HPP_

#include <type_traits>

#include "../parsing/schema/Type.hpp"

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::Description> ||
std::is_same_v<T, parsing::schema::Type::Deprecated>) {
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
54 changes: 48 additions & 6 deletions include/rfl/parsing/NamedTupleParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <limits>
#include <map>
#include <optional>
#include <span>
#include <sstream>
#include <string>
#include <string_view>
Expand All @@ -15,6 +16,7 @@
#include <vector>

#include "../Generic.hpp"
#include "../Metadata.hpp"
#include "../NamedTuple.hpp"
#include "../Result.hpp"
#include "../internal/always_inline.hpp"
Expand Down Expand Up @@ -148,6 +150,22 @@ inline void set_schema_default_value(Generic _default, schema::Type* _schema) {
});
}

/// Applies out-of-band `rfl::FieldMeta` (description / deprecation) to a field
/// schema node without embedding long StringLiteral NTTPs in field types.
inline void apply_field_metadata(schema::Type* _schema,
const FieldMeta& _meta) {
if (!_meta.deprecation_message.empty()) {
*_schema = schema::Type{schema::Type::Deprecated{
.deprecation_message_ = std::string(_meta.deprecation_message),
.description_ = std::string(_meta.description),
.type_ = Ref<schema::Type>::make(std::move(*_schema))}};
} else if (!_meta.description.empty()) {
*_schema = schema::Type{schema::Type::Description{
.description_ = std::string(_meta.description),
.type_ = Ref<schema::Type>::make(std::move(*_schema))}};
}
}

/// Free per-field serialization helper parameterized only by ValueType (never
/// the enclosing NamedTuple's FieldTypes... pack).
template <class R, class W, bool _ignore_empty_containers, bool _all_required,
Expand Down Expand Up @@ -215,7 +233,9 @@ template <class R, class W, bool _no_field_names, class ProcessorsType, class U,
class SchemaType, class FieldValPtrT>
void build_named_tuple_field_schema(
std::map<std::string, schema::Type>* _definitions, SchemaType* _schema,
const std::string_view _name, const FieldValPtrT _field_val) noexcept {
const std::string_view _wire_name, const std::string_view _raw_name,
const FieldValPtrT _field_val,
const std::span<const FieldMeta> _field_meta) noexcept {
using PtrT = std::remove_cvref_t<FieldValPtrT>;
if constexpr (!internal::is_skip_v<U> && !internal::is_extra_fields_v<U>) {
auto s = Parser<R, W, U, ProcessorsType>::to_schema(_definitions);
Expand All @@ -230,10 +250,17 @@ void build_named_tuple_field_schema(
&s);
}
}
if (!_field_meta.empty()) {
if (const auto meta =
internal::find_field_metadata(_field_meta, _wire_name, _raw_name);
meta.has_value()) {
detail::apply_field_metadata(&s, *meta);
}
}
if constexpr (_no_field_names) {
_schema->types_.emplace_back(std::move(s));
} else {
_schema->types_[std::string(_name)] = std::move(s);
_schema->types_[std::string(_wire_name)] = std::move(s);
}
}
}
Expand Down Expand Up @@ -489,14 +516,19 @@ struct NamedTupleParser {
* @brief Generates the schema for the named tuple.
*
* @param _definitions The map of definitions to add to.
* @param _view Optional view into a default-constructed instance.
* @param _field_meta Optional out-of-band field metadata (`rfl::Metadata<U>`).
* @param _raw_names Optional pre-processor field names for metadata lookup.
* @return The schema type.
*/
template <typename View = void>
static schema::Type to_schema(
std::map<std::string, schema::Type>* _definitions,
View* _view = nullptr) noexcept {
View* _view = nullptr,
const std::span<const FieldMeta> _field_meta = {},
const std::span<const std::string_view> _raw_names = {}) noexcept {
SchemaType schema;
build_schema(_definitions, &schema, _view,
build_schema(_definitions, &schema, _view, _field_meta, _raw_names,
std::make_integer_sequence<int, size_>());
return schema::Type{schema};
}
Expand All @@ -517,6 +549,8 @@ struct NamedTupleParser {
template <typename View, int... _is>
static void build_schema(std::map<std::string, schema::Type>* _definitions,
SchemaType* _schema, View* _view,
const std::span<const FieldMeta> _field_meta,
const std::span<const std::string_view> _raw_names,
std::integer_sequence<int, _is...>) noexcept {
if constexpr (!std::is_same_v<View, void>) {
(detail::build_named_tuple_field_schema<
Expand All @@ -525,16 +559,24 @@ struct NamedTupleParser {
typename internal::nth_element_t<_is, FieldTypes...>::Type>>(
_definitions, _schema,
internal::nth_element_t<_is, FieldTypes...>::name(),
static_cast<size_t>(_is) < _raw_names.size()
? _raw_names[static_cast<size_t>(_is)]
: std::string_view{},
_view ? detail::extract_schema_field_ptr(rfl::get<_is>(*_view))
: nullptr),
: nullptr,
_field_meta),
...);
} else {
(detail::build_named_tuple_field_schema<
R, W, _no_field_names, ProcessorsType,
std::remove_cvref_t<
typename internal::nth_element_t<_is, FieldTypes...>::Type>>(
_definitions, _schema,
internal::nth_element_t<_is, FieldTypes...>::name(), nullptr),
internal::nth_element_t<_is, FieldTypes...>::name(),
static_cast<size_t>(_is) < _raw_names.size()
? _raw_names[static_cast<size_t>(_is)]
: std::string_view{},
nullptr, _field_meta),
...);
}

Expand Down
Loading
Loading