77#include < variant>
88
99#include " ../NamedTuple.hpp"
10- #include " ../Ref.hpp"
1110#include " ../Result.hpp"
1211#include " ../Variant.hpp"
13- #include " ../always_false.hpp"
1412#include " ../internal/add_tags_to_variants_v.hpp"
1513#include " ../internal/all_fields.hpp"
1614#include " ../internal/to_ptr_field.hpp"
15+ #include " ../visit.hpp"
1716#include " FieldVariantParser.hpp"
1817#include " Parent.hpp"
1918#include " Parser_base.hpp"
@@ -66,7 +65,7 @@ class ParserVariant<R, W, std::variant<AlternativeTypes...>, ProcessorsType> {
6665 */
6766 static Result<std::variant<AlternativeTypes...>> read (
6867 const R& _r, const InputVarType& _var) noexcept {
69- if constexpr (internal::all_fields<std::tuple< AlternativeTypes...>>() ) {
68+ if constexpr (internal::all_fields_v< AlternativeTypes...>) {
7069 if constexpr (schemaful::IsSchemafulReader<R>) {
7170 using WrappedType = rfl::Variant<NamedTuple<AlternativeTypes>...>;
7271 return Parser<R, W, WrappedType, ProcessorsType>::read (_r, _var)
@@ -146,7 +145,7 @@ class ParserVariant<R, W, std::variant<AlternativeTypes...>, ProcessorsType> {
146145 static void write (const W& _w,
147146 const std::variant<AlternativeTypes...>& _variant,
148147 const P& _parent) {
149- if constexpr (internal::all_fields<std::tuple< AlternativeTypes...>>() ) {
148+ if constexpr (internal::all_fields_v< AlternativeTypes...>) {
150149 if constexpr (schemaful::IsSchemafulWriter<W>) {
151150 using WrappedType = rfl::Variant<
152151 NamedTuple<Field<AlternativeTypes::name_,
@@ -220,7 +219,7 @@ class ParserVariant<R, W, std::variant<AlternativeTypes...>, ProcessorsType> {
220219 */
221220 static schema::Type to_schema (
222221 std::map<std::string, schema::Type>* _definitions) {
223- if constexpr (internal::all_fields<std::tuple< AlternativeTypes...>>() ) {
222+ if constexpr (internal::all_fields_v< AlternativeTypes...>) {
224223 return FieldVariantParser<R, W, ProcessorsType,
225224 AlternativeTypes...>::to_schema (_definitions);
226225
@@ -235,42 +234,18 @@ class ParserVariant<R, W, std::variant<AlternativeTypes...>, ProcessorsType> {
235234 _definitions);
236235
237236 } else {
238- std::vector<schema::Type> types;
239- build_schema (
240- _definitions, &types,
241- std::make_integer_sequence<int , sizeof ...(AlternativeTypes)>());
242- return schema::Type{schema::Type::AnyOf{.types_ = std::move (types)}};
237+ return schema::Type{schema::Type::AnyOf{
238+ .types_ = std::vector<schema::Type>(
239+ {one_field_to_type<AlternativeTypes>(_definitions)...})}};
243240 }
244241 }
245242
246243 private:
247- /* *
248- * @brief Adds an alternative to the schema.
249- *
250- * @tparam _i The index of the alternative.
251- * @param _definitions The map of definitions to add the schema to.
252- * @param _types The vector of types to add the generated schema to.
253- */
254- template <size_t _i>
255- static void add_to_schema (std::map<std::string, schema::Type>* _definitions,
256- std::vector<schema::Type>* _types) noexcept {
257- using U = std::remove_cvref_t <
258- std::variant_alternative_t <_i, std::variant<AlternativeTypes...>>>;
259- _types->push_back (Parser<R, W, U, ProcessorsType>::to_schema (_definitions));
260- }
261-
262- /* *
263- * @brief Builds the schema for the variant.
264- *
265- * @tparam _is The indices of the alternatives.
266- * @param _definitions The map of definitions to add the schema to.
267- * @param _types The vector of types to add the generated schemas to.
268- */
269- template <int ... _is>
270- static void build_schema (std::map<std::string, schema::Type>* _definitions,
271- std::vector<schema::Type>* _types,
272- std::integer_sequence<int , _is...>) noexcept {
273- (add_to_schema<_is>(_definitions, _types), ...);
244+ template <class AltType >
245+ static schema::Type one_field_to_type (
246+ std::map<std::string, schema::Type>* _definitions) noexcept {
247+ return Parser<R, W, std::remove_cvref_t <AltType>,
248+ ProcessorsType>::to_schema (_definitions);
274249 }
275250
276251 /* *
0 commit comments