From ce5ec988153cbaca4849b3edbaaa69ddc6cca079 Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Tue, 3 Oct 2023 11:27:42 +0200 Subject: [PATCH] Added the field variant --- include/rfl/internal/all_fields.hpp | 26 ++++ include/rfl/parsing/Parser.hpp | 207 +++++++++++++++------------- tests/test_field_variant.hpp | 34 +++++ tests/tests.cpp | 2 + 4 files changed, 171 insertions(+), 98 deletions(-) create mode 100644 include/rfl/internal/all_fields.hpp create mode 100644 tests/test_field_variant.hpp diff --git a/include/rfl/internal/all_fields.hpp b/include/rfl/internal/all_fields.hpp new file mode 100644 index 00000000..ef7fcc03 --- /dev/null +++ b/include/rfl/internal/all_fields.hpp @@ -0,0 +1,26 @@ +#ifndef RFL_INTERNAL_ALLFIELDS_HPP_ +#define RFL_INTERNAL_ALLFIELDS_HPP_ + +#include +#include +#include + +#include "rfl/internal/is_field.hpp" + +namespace rfl { +namespace internal { + +template +constexpr bool all_fields() { + if constexpr (_i == std::tuple_size_v) { + return true; + } else { + using T = std::tuple_element_t<_i, TupleType>; + return is_field_v && all_fields(); + } +} + +} // namespace internal +} // namespace rfl + +#endif diff --git a/include/rfl/parsing/Parser.hpp b/include/rfl/parsing/Parser.hpp index 4032d147..5c33916e 100644 --- a/include/rfl/parsing/Parser.hpp +++ b/include/rfl/parsing/Parser.hpp @@ -27,6 +27,7 @@ #include "rfl/field_type.hpp" #include "rfl/from_named_tuple.hpp" #include "rfl/internal/StringLiteral.hpp" +#include "rfl/internal/all_fields.hpp" #include "rfl/internal/has_reflection_method_v.hpp" #include "rfl/internal/has_reflection_type_v.hpp" #include "rfl/named_tuple_t.hpp" @@ -127,102 +128,6 @@ struct Parser> { // ---------------------------------------------------------------------------- -template -struct Parser> { - using ResultType = Result>; - - public: - using InputObjectType = typename ReaderType::InputObjectType; - using InputVarType = typename ReaderType::InputVarType; - - using OutputObjectType = typename WriterType::OutputObjectType; - using OutputVarType = typename WriterType::OutputVarType; - - /// Expresses the variables as type T. - static ResultType read(const ReaderType& _r, InputVarType* _var) noexcept { - const auto to_map = [&](auto _obj) { return _r.to_map(&_obj); }; - - const auto to_result = [&](auto _map) -> ResultType { - if (_map.size() != 1) { - return Error( - "Could not parse Enum: Expected the object to have exactly " - "one " - "field, but found " + - std::to_string(_map.size()) + " fields."); - } - const auto it = _map.begin(); - const auto& disc_value = it->first; - auto& var = it->second; - return find_matching_alternative(_r, disc_value, &var); - }; - - return _r.to_object(_var).transform(to_map).and_then(to_result); - } - - /// Expresses the variables as a JSON type. - static OutputVarType write(const WriterType& _w, - const Enum& _enum) noexcept { - const auto handle = [&](const auto& _field) { - using NamedTupleType = NamedTuple>; - return Parser::write( - _w, NamedTupleType(_field)); - }; - return std::visit(handle, _enum.variant_); - } - - private: - template - static ResultType find_matching_alternative(const ReaderType& _r, - const std::string& _disc_value, - InputVarType* _var) noexcept { - if constexpr (_i == sizeof...(FieldTypes)) { - return Error("Could not parse enum, could not match field named '" + - _disc_value + "'."); - } else { - const auto optional = try_option<_i>(_r, _disc_value, _var); - if (optional) { - return *optional; - } else { - return find_matching_alternative<_i + 1>(_r, _disc_value, _var); - } - } - } - - /// Tries to parse one particular option. - template - static std::optional try_option(const ReaderType& _r, - const std::string& _disc_value, - InputVarType* _var) noexcept { - using FieldType = std::decay_t< - typename std::tuple_element<_i, std::tuple>::type>; - - using ValueType = std::decay_t; - - const auto key = FieldType::name_.str(); - - if (key == _disc_value) { - const auto to_enum = [](ValueType&& _val) { - return Enum( - FieldType(std::forward(_val))); - }; - - const auto embellish_error = [&](const Error& _e) { - return Error("Could not parse enum with field '" + _disc_value + - "': " + _e.what()); - }; - - return Parser::read(_r, _var) - .transform(to_enum) - .or_else(embellish_error); - - } else { - return std::optional(); - } - } -}; - -// ---------------------------------------------------------------------------- - template struct Parser> { @@ -736,6 +641,104 @@ struct Parser> { // ---------------------------------------------------------------------------- +/// To be used when all options of the variants are rfl::Field. Essentially, +/// this is an externally tagged union. +template +struct FieldVariantParser { + using ResultType = Result>; + + public: + using InputObjectType = typename ReaderType::InputObjectType; + using InputVarType = typename ReaderType::InputVarType; + + using OutputObjectType = typename WriterType::OutputObjectType; + using OutputVarType = typename WriterType::OutputVarType; + + /// Expresses the variables as type T. + static ResultType read(const ReaderType& _r, InputVarType* _var) noexcept { + const auto to_map = [&](auto _obj) { return _r.to_map(&_obj); }; + + const auto to_result = [&](auto _map) -> ResultType { + if (_map.size() != 1) { + return Error( + "Could not parse Field: Expected the object to have " + "exactly one field, but found " + + std::to_string(_map.size()) + " fields."); + } + const auto it = _map.begin(); + const auto& disc_value = it->first; + auto& var = it->second; + return find_matching_alternative(_r, disc_value, &var); + }; + + return _r.to_object(_var).transform(to_map).and_then(to_result); + } + + /// Expresses the variables as a JSON type. + static OutputVarType write(const WriterType& _w, + const std::variant& _v) noexcept { + const auto handle = [&](const auto& _field) { + using NamedTupleType = NamedTuple>; + return Parser::write( + _w, NamedTupleType(_field)); + }; + return std::visit(handle, _v); + } + + private: + template + static ResultType find_matching_alternative(const ReaderType& _r, + const std::string& _disc_value, + InputVarType* _var) noexcept { + if constexpr (_i == sizeof...(FieldTypes)) { + return Error( + "Could not parse std::variant, could not match field named '" + + _disc_value + "'."); + } else { + const auto optional = try_option<_i>(_r, _disc_value, _var); + if (optional) { + return *optional; + } else { + return find_matching_alternative<_i + 1>(_r, _disc_value, _var); + } + } + } + + /// Tries to parse one particular option. + template + static std::optional try_option(const ReaderType& _r, + const std::string& _disc_value, + InputVarType* _var) noexcept { + using FieldType = std::decay_t< + typename std::tuple_element<_i, std::tuple>::type>; + + using ValueType = std::decay_t; + + const auto key = FieldType::name_.str(); + + if (key == _disc_value) { + const auto to_variant = [](ValueType&& _val) { + return std::variant( + FieldType(std::forward(_val))); + }; + + const auto embellish_error = [&](const Error& _e) { + return Error("Could not parse std::variant with field '" + + _disc_value + "': " + _e.what()); + }; + + return Parser::read(_r, _var) + .transform(to_variant) + .or_else(embellish_error); + + } else { + return std::optional(); + } + } +}; + +// ---------------------------------------------------------------------------- + template struct Parser> { using InputVarType = typename ReaderType::InputVarType; @@ -746,7 +749,11 @@ struct Parser> { static Result> read( const ReaderType& _r, InputVarType* _var, const std::string _errors = "") noexcept { - if constexpr (_i == sizeof...(FieldTypes)) { + if constexpr (_i == 0 && + internal::all_fields>()) { + return FieldVariantParser::read(_r, _var); + } else if constexpr (_i == sizeof...(FieldTypes)) { return Error("Could not parse variant: " + _errors); } else { const auto to_variant = [](const auto& _val) { @@ -775,7 +782,11 @@ struct Parser> { const std::variant& _variant) noexcept { using AltType = std::variant_alternative_t<_i, std::variant>; - if constexpr (_i + 1 == sizeof...(FieldTypes)) { + if constexpr (_i == 0 && + internal::all_fields>()) { + return FieldVariantParser::write(_w, _variant); + } else if constexpr (_i + 1 == sizeof...(FieldTypes)) { return Parser>::write( _w, std::get(_variant)); } else { diff --git a/tests/test_field_variant.hpp b/tests/test_field_variant.hpp new file mode 100644 index 00000000..73a99452 --- /dev/null +++ b/tests/test_field_variant.hpp @@ -0,0 +1,34 @@ +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +void test_field_variant() { + std::cout << "test_field_variant" << std::endl; + + struct Circle { + rfl::Field<"radius", double> radius; + }; + + struct Rectangle { + rfl::Field<"height", double> height; + rfl::Field<"width", double> width; + }; + + struct Square { + rfl::Field<"width", double> width; + }; + + using Shapes = std::variant, + rfl::Field<"rectangle", Rectangle>, + rfl::Field<"square", Square>>; + + const Shapes r = + rfl::make_field<"rectangle">(Rectangle{.height = 10, .width = 5}); + + write_and_read(r, "{\"rectangle\":{\"height\":10.0,\"width\":5.0}}"); +} diff --git a/tests/tests.cpp b/tests/tests.cpp index f8c38d28..b2e4b313 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -1,4 +1,5 @@ #include "test_default_values.hpp" +#include "test_field_variant.hpp" #include "test_literal.hpp" #include "test_readme_example.hpp" #include "test_ref.hpp" @@ -11,6 +12,7 @@ int main() { test_literal(); test_variant(); test_tagged_union(); + test_field_variant(); test_ref(); return 0;