Add Serialization and Deserialization generation for std::optional
All checks were successful
PR Check / on-push-commit-check (push) Successful in 13m30s
All checks were successful
PR Check / on-push-commit-check (push) Successful in 13m30s
This commit is contained in:
parent
9273c473f8
commit
662392509f
2 changed files with 110 additions and 29 deletions
|
@ -28,6 +28,11 @@ constexpr auto Parse(xmlpp::Element* element, Tag<T> = {}) -> T
|
|||
template <typename 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>
|
||||
constexpr auto Serialize(xmlpp::Element* node, const T& element) -> void
|
||||
requires(!std::same_as<std::decay_t<decltype(kSerializationConfig<T>)>, std::monostate>);
|
||||
|
@ -64,9 +69,7 @@ struct FieldInfo {
|
|||
};
|
||||
|
||||
template <typename T>
|
||||
struct Config {
|
||||
std::optional<T> defaultValue;
|
||||
};
|
||||
struct Config {};
|
||||
|
||||
// GCC workaround: operator==
|
||||
|
||||
|
@ -87,9 +90,11 @@ constexpr auto operator==(const Config<T>&, const Config<T>&) -> bool {
|
|||
return false;
|
||||
}
|
||||
|
||||
template <>
|
||||
struct Config<std::string> {
|
||||
std::optional<std::string_view> defaultValue;
|
||||
template <typename T>
|
||||
struct Config<std::optional<T>> {
|
||||
std::optional<T> defaultValue = std::nullopt;
|
||||
bool strict = true;
|
||||
std::optional<Config<T>> main = std::nullopt;
|
||||
};
|
||||
|
||||
namespace impl {
|
||||
|
@ -131,7 +136,7 @@ struct ElementSerializer {
|
|||
throw ElementParsingError(std::format("[{}: {}] parsing error: [ Not found ]", Info::kName, nameof::nameof_full_type<T>()));
|
||||
}
|
||||
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>()));
|
||||
}
|
||||
try {
|
||||
|
@ -140,6 +145,7 @@ struct ElementSerializer {
|
|||
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) {
|
||||
auto created = node->add_child_element(Info::kName);
|
||||
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>
|
||||
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) {
|
||||
try {
|
||||
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 {
|
||||
|
||||
template <typename T>
|
||||
|
@ -186,15 +262,7 @@ template <auto& Config, typename Info>
|
|||
auto ParseField(xmlpp::Element* main) -> std::decay_t<decltype(Config)>::type {
|
||||
using Type = std::decay_t<decltype(Config)>::type;
|
||||
if constexpr(std::holds_alternative<AttributeConfig>(Config.Base())) {
|
||||
xmlpp::Attribute* node = main->get_attribute(Info::kName);
|
||||
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();
|
||||
}
|
||||
return AttributeSerializer<Type, Info>::Parse(main);
|
||||
} else {
|
||||
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>
|
||||
auto SerializeField(xmlpp::Element* main, const T& obj) {
|
||||
if constexpr(std::holds_alternative<AttributeConfig>(Config.Base())) {
|
||||
auto node = main->set_attribute(Info::kName, [&] -> decltype(auto) {
|
||||
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>()));
|
||||
}
|
||||
AttributeSerializer<T, Info>::Serialize(main, obj);
|
||||
} else {
|
||||
ElementSerializer<T, Config, Info>::Serialize(main, obj);
|
||||
}
|
||||
|
|
|
@ -72,6 +72,12 @@ struct 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 serialization {
|
||||
|
@ -91,6 +97,10 @@ constexpr auto kSerializationConfig<tests::serialization::SomeStruct5> = Seriali
|
|||
template <>
|
||||
constexpr auto kSerializationConfig<tests::serialization::SomeStruct6> =
|
||||
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 tests::serialization {
|
||||
|
@ -119,6 +129,10 @@ auto SomeStruct6::Parse(xmlpp::Element* element) -> SomeStruct6 {
|
|||
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) {
|
||||
::larra::xmpp::serialization::Serialize(element, self);
|
||||
}
|
||||
|
@ -171,6 +185,17 @@ TEST(AutoParse, Vector) {
|
|||
}) | 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) {
|
||||
xmlpp::Document doc;
|
||||
auto node = doc.create_root_node("some2");
|
||||
|
|
Loading…
Reference in a new issue