Skip to content

Commit f5d551d

Browse files
16bit-ykikoclaude
andcommitted
refactor(serde): simplify type_info_of to constexpr function and add schema_opaque trait
- Replace type_info_instance struct specializations with a single constexpr type_info_of<T, Config>() function using if-constexpr dispatch - Add schema_opaque<T> trait to opt out types from recursive schema decomposition - Format with clang-format Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 33dd75d commit f5d551d

File tree

6 files changed

+263
-262
lines changed

6 files changed

+263
-262
lines changed

include/eventide/serde/schema/field_info.h

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,13 @@ struct type_info {
9797
return kind == type_kind::float32 || kind == type_kind::float64;
9898
}
9999

100-
constexpr bool is_numeric() const { return is_integer() || is_floating(); }
100+
constexpr bool is_numeric() const {
101+
return is_integer() || is_floating();
102+
}
101103

102-
constexpr bool is_scalar() const { return kind <= type_kind::enumeration; }
104+
constexpr bool is_scalar() const {
105+
return kind <= type_kind::enumeration;
106+
}
103107
};
104108

105109
/// array / set
@@ -149,12 +153,22 @@ struct field_info {
149153
const type_info* type; // recursive type descriptor (wire view)
150154

151155
// Level 1 flags
152-
bool has_default; // schema::default_value
153-
bool is_literal; // schema::literal
154-
bool has_skip_if; // behavior::skip_if present
155-
bool has_behavior; // with/as/enum_string present
156+
bool has_default; // schema::default_value
157+
bool is_literal; // schema::literal
158+
bool has_skip_if; // behavior::skip_if present
159+
bool has_behavior; // with/as/enum_string present
156160
};
157161

162+
// ---------------------------------------------------------------------------
163+
// schema_opaque — opt-out from recursive type decomposition
164+
// ---------------------------------------------------------------------------
165+
166+
/// Types marked schema_opaque are treated as opaque by the schema system.
167+
/// They get kind=unknown and are not recursively decomposed in type_info_instance.
168+
/// Backend-specific serialize/deserialize hooks handle these types directly.
169+
template <typename T>
170+
constexpr inline bool schema_opaque = false;
171+
158172
// ---------------------------------------------------------------------------
159173
// kind_of<T>() — map C++ types to type_kind values
160174
// ---------------------------------------------------------------------------
@@ -220,6 +234,10 @@ consteval type_kind kind_of() {
220234
if constexpr(serde::annotated_type<V>) {
221235
return kind_of<typename V::annotated_type>();
222236
}
237+
// Opaque types with custom hooks — do not decompose
238+
else if constexpr(schema_opaque<V>) {
239+
return type_kind::unknown;
240+
}
223241
// Enum -> enumeration
224242
else if constexpr(std::is_enum_v<V>) {
225243
return type_kind::enumeration;

include/eventide/serde/schema/field_slot.h

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ template <std::size_t I, typename List>
2020
struct type_list_element;
2121

2222
template <std::size_t I, typename First, typename... Rest>
23-
struct type_list_element<I, type_list<First, Rest...>>
24-
: type_list_element<I - 1, type_list<Rest...>> {};
23+
struct type_list_element<I, type_list<First, Rest...>> :
24+
type_list_element<I - 1, type_list<Rest...>> {};
2525

2626
template <typename First, typename... Rest>
2727
struct type_list_element<0, type_list<First, Rest...>> {
@@ -60,8 +60,8 @@ struct type_list_concat<List> {
6060
};
6161

6262
template <typename First, typename Second, typename... Rest>
63-
struct type_list_concat<First, Second, Rest...>
64-
: type_list_concat<type_list_cat_t<First, Second>, Rest...> {};
63+
struct type_list_concat<First, Second, Rest...> :
64+
type_list_concat<type_list_cat_t<First, Second>, Rest...> {};
6565

6666
template <typename... Lists>
6767
using type_list_concat_t = typename type_list_concat<Lists...>::type;
@@ -72,11 +72,10 @@ template <typename List>
7272
struct type_list_size;
7373

7474
template <typename... Ts>
75-
struct type_list_size<type_list<Ts...>>
76-
: std::integral_constant<std::size_t, sizeof...(Ts)> {};
75+
struct type_list_size<type_list<Ts...>> : std::integral_constant<std::size_t, sizeof...(Ts)> {};
7776

7877
template <typename List>
79-
inline constexpr std::size_t type_list_size_v = type_list_size<List>::value;
78+
constexpr inline std::size_t type_list_size_v = type_list_size<List>::value;
8079

8180
// ---------------------------------------------------------------------------
8281
// field_slot -- compile-time per-field descriptor
@@ -92,13 +91,11 @@ inline constexpr std::size_t type_list_size_v = type_list_size<List>::value;
9291
/// Defaults to RawType when no behavior attr is present.
9392
/// @tparam BehaviorAttrs A std::tuple<...> of the behavior attributes found
9493
/// on this field (skip_if, with, as, enum_string, tagged).
95-
template <typename RawType,
96-
typename WireType = RawType,
97-
typename BehaviorAttrs = std::tuple<>>
94+
template <typename RawType, typename WireType = RawType, typename BehaviorAttrs = std::tuple<>>
9895
struct field_slot {
99-
using raw_type = RawType;
96+
using raw_type = RawType;
10097
using wire_type = WireType;
101-
using attrs = BehaviorAttrs;
98+
using attrs = BehaviorAttrs;
10299
};
103100

104101
} // namespace eventide::serde::schema

0 commit comments

Comments
 (0)