Add Serialization and Deserialization generation for std::optional

This commit is contained in:
sha512sum 2024-11-12 14:36:18 +00:00 committed by Anton
parent 23070660f9
commit 810ebbfa86
2 changed files with 110 additions and 29 deletions

View file

@ -28,6 +28,11 @@ constexpr auto Parse(xmlpp::Element* element, Tag<T> = {}) -> T
template <typename T> template <typename T>
constexpr auto Parse(xmlpp::Element* element, Tag<T> = {}) -> T; constexpr auto Parse(xmlpp::Element* element, Tag<T> = {}) -> T;
template <typename T>
constexpr auto TryParse(xmlpp::Element* element, Tag<T> = {}) -> std::optional<T> {
return Serialization<std::optional<T>>::Parse(element);
}
template <typename T> template <typename T>
constexpr auto Serialize(xmlpp::Element* node, const T& element) -> void constexpr auto Serialize(xmlpp::Element* node, const T& element) -> void
requires(!std::same_as<std::decay_t<decltype(kSerializationConfig<T>)>, std::monostate>); requires(!std::same_as<std::decay_t<decltype(kSerializationConfig<T>)>, std::monostate>);
@ -64,9 +69,7 @@ struct FieldInfo {
}; };
template <typename T> template <typename T>
struct Config { struct Config {};
std::optional<T> defaultValue;
};
// GCC workaround: operator== // GCC workaround: operator==
@ -87,9 +90,11 @@ constexpr auto operator==(const Config<T>&, const Config<T>&) -> bool {
return false; return false;
} }
template <> template <typename T>
struct Config<std::string> { struct Config<std::optional<T>> {
std::optional<std::string_view> defaultValue; std::optional<T> defaultValue = std::nullopt;
bool strict = true;
std::optional<Config<T>> main = std::nullopt;
}; };
namespace impl { namespace impl {
@ -131,7 +136,7 @@ struct ElementSerializer {
throw ElementParsingError(std::format("[{}: {}] parsing error: [ Not found ]", Info::kName, nameof::nameof_full_type<T>())); throw ElementParsingError(std::format("[{}: {}] parsing error: [ Not found ]", Info::kName, nameof::nameof_full_type<T>()));
} }
auto elementNode = dynamic_cast<xmlpp::Element*>(node); auto elementNode = dynamic_cast<xmlpp::Element*>(node);
if(!node) { if(!elementNode) {
throw ElementParsingError(std::format("[{}: {}] parsing error: [ Invalid node ]", Info::kName, nameof::nameof_full_type<T>())); throw ElementParsingError(std::format("[{}: {}] parsing error: [ Invalid node ]", Info::kName, nameof::nameof_full_type<T>()));
} }
try { try {
@ -140,6 +145,7 @@ struct ElementSerializer {
throw ElementParsingError(std::format("[{}: {}] parsing error: [ {} ]", Info::kName, nameof::nameof_full_type<T>(), error.what())); throw ElementParsingError(std::format("[{}: {}] parsing error: [ {} ]", Info::kName, nameof::nameof_full_type<T>(), error.what()));
} }
} }
static constexpr auto Serialize(xmlpp::Element* node, const T& element) { static constexpr auto Serialize(xmlpp::Element* node, const T& element) {
auto created = node->add_child_element(Info::kName); auto created = node->add_child_element(Info::kName);
if(!node) { if(!node) {
@ -155,8 +161,40 @@ struct ElementSerializer {
} }
}; };
template <typename T, auto& Config, typename Info>
struct ElementSerializer<std::optional<T>, Config, Info> {
static constexpr auto Parse(xmlpp::Element* element) -> std::optional<T> {
return [&] {
auto node = element->get_first_child(Info::kName);
if(!node) {
return std::nullopt;
}
auto elementNode = dynamic_cast<xmlpp::Element*>(node);
if(!elementNode) {
return std::nullopt;
}
if constexpr(Config.strict) {
return ElementSerializer<T, Config.main, Info>::Parse(element);
} else {
return ElementSerializer<T, Config.main, Info>::TryParse(element);
}
}()
.or_else([] {
return Config.defaultValue;
});
}
static constexpr auto Serialize(xmlpp::Element* node, const std::optional<T>& element) {
if(element) {
ElementSerializer<T, Config.main, Info>::Serialize(node, *element);
} else if(Config.defaultValue) {
Serialize(node, Config.defaultValue);
}
}
};
template <typename T, auto& Config, typename Info> template <typename T, auto& Config, typename Info>
struct ElementSerializer<std::vector<T>, Config, Info> { struct ElementSerializer<std::vector<T>, Config, Info> {
// TODO(sha512sum): Add Config and main options instead use Serialization<std::vector<T>>
static constexpr auto Parse(xmlpp::Element* element) { static constexpr auto Parse(xmlpp::Element* element) {
try { try {
return Serialization<std::vector<T>>::Parse(element); return Serialization<std::vector<T>>::Parse(element);
@ -174,6 +212,44 @@ struct ElementSerializer<std::vector<T>, Config, Info> {
} }
} }
}; };
template <typename T, typename Info>
struct AttributeSerializer {
static constexpr auto Parse(xmlpp::Element* element) -> T {
auto node = element->get_attribute(Info::kName);
if(!node) {
throw AttributeParsingError(std::format("Attribute [{}: {}] parsing error", Info::kName, nameof::nameof_full_type<T>()));
}
if constexpr(requires(std::string_view view) { T::Parse(view); }) {
return T::Parse(node->get_value());
} else {
return node->get_value();
}
}
static constexpr auto Serialize(xmlpp::Element* element, const T& obj) {
if constexpr(requires {
{ ToString(obj) } -> std::convertible_to<const std::string&>;
}) {
element->set_attribute(Info::kName, ToString(obj));
} else {
element->set_attribute(Info::kName, obj);
}
}
};
template <typename T, typename Info>
struct AttributeSerializer<std::optional<T>, Info> {
static constexpr auto Parse(xmlpp::Element* element) -> std::optional<T> {
auto node = element->get_attribute(Info::kName);
return node ? std::optional{AttributeSerializer<T, Info>::Parse(element)} : std::nullopt;
}
static constexpr auto Serialize(xmlpp::Element* element, const std::optional<T>& obj) {
if(obj) {
AttributeSerializer<T, Info>::Serialize(element, *obj);
}
}
};
namespace impl { namespace impl {
template <typename T> template <typename T>
@ -186,15 +262,7 @@ template <auto& Config, typename Info>
auto ParseField(xmlpp::Element* main) -> std::decay_t<decltype(Config)>::type { auto ParseField(xmlpp::Element* main) -> std::decay_t<decltype(Config)>::type {
using Type = std::decay_t<decltype(Config)>::type; using Type = std::decay_t<decltype(Config)>::type;
if constexpr(std::holds_alternative<AttributeConfig>(Config.Base())) { if constexpr(std::holds_alternative<AttributeConfig>(Config.Base())) {
xmlpp::Attribute* node = main->get_attribute(Info::kName); return AttributeSerializer<Type, Info>::Parse(main);
if(!node) {
throw AttributeParsingError(std::format("Attribute [{}: {}] parsing error", Info::kName, nameof::nameof_full_type<Type>()));
}
if constexpr(requires(std::string_view view) { Type::Parse(view); }) {
return Type::Parse(node->get_value());
} else {
return node->get_value();
}
} else { } else {
return ElementSerializer<Type, Config, Info>::Parse(main); return ElementSerializer<Type, Config, Info>::Parse(main);
} }
@ -203,19 +271,7 @@ auto ParseField(xmlpp::Element* main) -> std::decay_t<decltype(Config)>::type {
template <auto& Config, typename Info, typename T> template <auto& Config, typename Info, typename T>
auto SerializeField(xmlpp::Element* main, const T& obj) { auto SerializeField(xmlpp::Element* main, const T& obj) {
if constexpr(std::holds_alternative<AttributeConfig>(Config.Base())) { if constexpr(std::holds_alternative<AttributeConfig>(Config.Base())) {
auto node = main->set_attribute(Info::kName, [&] -> decltype(auto) { AttributeSerializer<T, Info>::Serialize(main, obj);
if constexpr(requires {
{ ToString(obj) } -> std::convertible_to<const std::string&>;
}) {
return ToString(obj);
} else {
return obj;
}
}());
if(!node) {
throw AttributeSerializationError(
std::format("[{}: {}] parsing error: [ node creation failed ]", Info::kName, nameof::nameof_full_type<T>()));
}
} else { } else {
ElementSerializer<T, Config, Info>::Serialize(main, obj); ElementSerializer<T, Config, Info>::Serialize(main, obj);
} }

View file

@ -72,6 +72,12 @@ struct SomeStruct6 {
[[nodiscard]] static auto Parse(xmlpp::Element* element) -> SomeStruct6; [[nodiscard]] static auto Parse(xmlpp::Element* element) -> SomeStruct6;
}; };
struct SomeStruct7 {
static constexpr auto kDefaultName = "some7";
std::optional<FullJid> value;
[[nodiscard]] static auto Parse(xmlpp::Element* element) -> SomeStruct7;
};
} // namespace tests::serialization } // namespace tests::serialization
namespace serialization { namespace serialization {
@ -91,6 +97,10 @@ constexpr auto kSerializationConfig<tests::serialization::SomeStruct5> = Seriali
template <> template <>
constexpr auto kSerializationConfig<tests::serialization::SomeStruct6> = constexpr auto kSerializationConfig<tests::serialization::SomeStruct6> =
SerializationConfig<tests::serialization::SomeStruct6>{}.With<"some">({Config<std::vector<tests::serialization::SomeStruct>>{}}); SerializationConfig<tests::serialization::SomeStruct6>{}.With<"some">({Config<std::vector<tests::serialization::SomeStruct>>{}});
template <>
constexpr auto kSerializationConfig<tests::serialization::SomeStruct7> = SerializationConfig<tests::serialization::SomeStruct7>{};
} // namespace serialization } // namespace serialization
namespace tests::serialization { namespace tests::serialization {
@ -119,6 +129,10 @@ auto SomeStruct6::Parse(xmlpp::Element* element) -> SomeStruct6 {
return ::larra::xmpp::serialization::Parse<tests::serialization::SomeStruct6>(element); return ::larra::xmpp::serialization::Parse<tests::serialization::SomeStruct6>(element);
} }
auto SomeStruct7::Parse(xmlpp::Element* element) -> SomeStruct7 {
return ::larra::xmpp::serialization::Parse<tests::serialization::SomeStruct7>(element);
}
auto operator<<(xmlpp::Element* element, const SomeStruct& self) { auto operator<<(xmlpp::Element* element, const SomeStruct& self) {
::larra::xmpp::serialization::Serialize(element, self); ::larra::xmpp::serialization::Serialize(element, self);
} }
@ -171,6 +185,17 @@ TEST(AutoParse, Vector) {
}) | std::ranges::to<std::vector<tests::serialization::SomeStruct>>()); }) | std::ranges::to<std::vector<tests::serialization::SomeStruct>>());
} }
TEST(AutoParse, Optional) {
xmlpp::Document doc;
auto node = doc.create_root_node("some7");
auto value = Serialization<tests::serialization::SomeStruct7>::Parse(node).value;
EXPECT_EQ(value, std::nullopt);
node->set_attribute("value", "user@server.i2p/resource");
auto value2 = Serialization<tests::serialization::SomeStruct7>::Parse(node).value;
FullJid expectData{.username = "user", .server = "server.i2p", .resource = "resource"};
EXPECT_EQ(value2, expectData);
}
TEST(AutoSerialize, Basic) { TEST(AutoSerialize, Basic) {
xmlpp::Document doc; xmlpp::Document doc;
auto node = doc.create_root_node("some2"); auto node = doc.create_root_node("some2");