.
This commit is contained in:
parent
ab3fc26003
commit
ed4276338d
4 changed files with 277 additions and 3 deletions
|
@ -2,6 +2,7 @@
|
|||
#include <libxml++/libxml++.h>
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
#include <larra/serialization/error.hpp>
|
||||
#include <nameof.hpp>
|
||||
#include <string>
|
||||
#include <utempl/utils.hpp>
|
||||
|
@ -96,7 +97,8 @@ template <typename T>
|
|||
struct Serialization : SerializationBase<T> {
|
||||
[[nodiscard]] static constexpr auto Parse(xmlpp::Element* element) -> T {
|
||||
if(!Serialization::StartCheck(element)) {
|
||||
throw std::runtime_error("StartCheck failed");
|
||||
throw serialization::SerializationError{
|
||||
std::format("[{}: {}] serialization error: [ StartCheck failed ]", Serialization::kDefaultName, nameof::nameof_full_type<T>())};
|
||||
}
|
||||
return T::Parse(element);
|
||||
}
|
||||
|
|
175
library/include/larra/serialization/auto.hpp
Normal file
175
library/include/larra/serialization/auto.hpp
Normal file
|
@ -0,0 +1,175 @@
|
|||
#pragma once
|
||||
#include <libxml++/libxml++.h>
|
||||
|
||||
#include <boost/pfr.hpp>
|
||||
#include <larra/serialization.hpp>
|
||||
#include <larra/serialization/error.hpp>
|
||||
#include <utempl/constexpr_string.hpp>
|
||||
#include <utempl/tuple.hpp>
|
||||
#include <utempl/utils.hpp>
|
||||
#include <variant>
|
||||
|
||||
namespace larra::xmpp::serialization {
|
||||
|
||||
template <typename T>
|
||||
struct Tag {};
|
||||
|
||||
template <typename T>
|
||||
inline constexpr auto kSerializationConfig = std::monostate{};
|
||||
|
||||
template <typename T>
|
||||
inline constexpr auto kDeserializationConfig = kSerializationConfig<T>;
|
||||
|
||||
template <typename T>
|
||||
constexpr auto Parse(xmlpp::Element* element, Tag<T> = {}) -> T
|
||||
requires(!std::same_as<std::decay_t<decltype(kDeserializationConfig<T>)>, std::monostate>);
|
||||
|
||||
template <typename T>
|
||||
constexpr auto Parse(xmlpp::Element* element, Tag<T> = {}) -> T;
|
||||
|
||||
struct AttributeConfig {};
|
||||
|
||||
template <typename T>
|
||||
struct MetaInfo {
|
||||
static constexpr std::size_t kSize = boost::pfr::tuple_size_v<T>;
|
||||
template <std::size_t I>
|
||||
using TupleElement = boost::pfr::tuple_element_t<I, T>;
|
||||
|
||||
template <std::size_t I>
|
||||
static constexpr std::string_view kFieldName = boost::pfr::get_name<I, T>();
|
||||
|
||||
template <typename Self, std::size_t I>
|
||||
static constexpr auto Get(Self&& self) -> decltype(auto) {
|
||||
return boost::pfr::get<I>(std::forward<Self>(self));
|
||||
}
|
||||
};
|
||||
|
||||
template <typename MainT, std::size_t Element>
|
||||
struct FieldInfo {
|
||||
using Main = MainT;
|
||||
using Info = MetaInfo<Main>;
|
||||
static inline const std::string kName = [] {
|
||||
if constexpr(requires { Info::template TupleElement<Element>::kDefaultName; }) {
|
||||
return Info::template TupleElement<Element>::kDefaultName;
|
||||
} else {
|
||||
return static_cast<std::string>(Info::template kFieldName<Element>);
|
||||
}
|
||||
}();
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct Config {
|
||||
std::optional<T> defaultValue;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Config<std::string> {
|
||||
std::optional<std::string_view> defaultValue;
|
||||
};
|
||||
|
||||
namespace impl {
|
||||
|
||||
template <typename T, typename V>
|
||||
struct Config : V {
|
||||
using V::V;
|
||||
constexpr auto Base() const -> const V& {
|
||||
return static_cast<const V&>(*this);
|
||||
}
|
||||
using type = T;
|
||||
};
|
||||
|
||||
} // namespace impl
|
||||
|
||||
template <typename T>
|
||||
struct ElementConfig {
|
||||
using type = impl::Config<T, std::variant<AttributeConfig, Config<T>>>;
|
||||
};
|
||||
|
||||
template <typename T, auto& Config, typename Info>
|
||||
struct ElementSerializer {
|
||||
static constexpr auto Parse(xmlpp::Element* element) {
|
||||
auto node = element->get_first_child(Info::kName);
|
||||
if(!node) {
|
||||
throw ElementSerializationError(
|
||||
std::format("[{}: {}] serialization error: [ Not found ]", Info::kName, nameof::nameof_full_type<T>()));
|
||||
}
|
||||
auto elementNode = dynamic_cast<xmlpp::Element*>(node);
|
||||
if(!node) {
|
||||
throw ElementSerializationError(
|
||||
std::format("[{}: {}] serialization error: [ Invalid node ]", Info::kName, nameof::nameof_full_type<T>()));
|
||||
}
|
||||
try {
|
||||
return ::larra::xmpp::serialization::Parse(elementNode, Tag<T>{});
|
||||
} catch(const SerializationError& error) {
|
||||
throw ElementSerializationError(
|
||||
std::format("[{}: {}] serialization error: [ {} ]", Info::kName, nameof::nameof_full_type<T>(), error.what()));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
namespace impl {
|
||||
|
||||
template <typename T>
|
||||
consteval auto FindElement(std::string_view field, utempl::TypeList<T> = {}) {
|
||||
auto fields = boost::pfr::names_as_array<T>();
|
||||
return std::ranges::find(fields, field) - fields.begin();
|
||||
}
|
||||
|
||||
template <auto& Config, typename Info>
|
||||
auto ParseField(xmlpp::Element* main) {
|
||||
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 AttributeSerializationError(std::format("Attribute [{}] serialization error", Info::kName));
|
||||
}
|
||||
return node->get_value();
|
||||
} else {
|
||||
return ElementSerializer<Type, Config, Info>::Parse(main);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace impl
|
||||
|
||||
template <typename T>
|
||||
struct SerializationConfig {
|
||||
decltype([] {
|
||||
return [](auto... is) -> utempl::Tuple<typename ElementConfig<boost::pfr::tuple_element_t<*is, T>>::type...> {
|
||||
std::unreachable();
|
||||
} | utempl::kSeq<boost::pfr::tuple_size_v<T>>;
|
||||
}()) tuple{};
|
||||
template <std::size_t I, typename Self> // NOLINTNEXTLINE
|
||||
consteval auto With(this Self&& self, ElementConfig<boost::pfr::tuple_element_t<I, T>>::type config) -> SerializationConfig {
|
||||
auto tuple = std::forward_like<Self>(self.tuple);
|
||||
Get<I>(tuple) = std::move(config);
|
||||
return {std::move(tuple)};
|
||||
}
|
||||
template <utempl::ConstexprString Name, typename Self>
|
||||
constexpr auto With(this Self&& self, ElementConfig<boost::pfr::tuple_element_t<impl::FindElement<T>(Name), T>>::type config)
|
||||
-> SerializationConfig {
|
||||
return std::forward<Self>(self).template With<impl::FindElement<T>(Name)>(std::move(config));
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
constexpr auto Parse(xmlpp::Element* element, Tag<T>) -> T {
|
||||
return Serialization<T>::Parse(element);
|
||||
}
|
||||
template <typename T>
|
||||
constexpr auto Parse(xmlpp::Element* element, Tag<T>) -> T
|
||||
requires(!std::same_as<std::decay_t<decltype(kDeserializationConfig<T>)>, std::monostate>)
|
||||
{
|
||||
static constexpr SerializationConfig config = kSerializationConfig<T>;
|
||||
constexpr auto tuple = utempl::Map(config.tuple, [](auto& ref) {
|
||||
return &ref;
|
||||
});
|
||||
return utempl::Unpack(utempl::PackConstexprWrapper<utempl::Enumerate(tuple)>(), [&](auto... configs) {
|
||||
try {
|
||||
return T{impl::ParseField<*((*configs).second), FieldInfo<T, (*configs).first>>(element)...};
|
||||
} catch(const SerializationError& error) {
|
||||
throw ElementSerializationError(std::format("[{}] serialization error: [ {} ]", nameof::nameof_full_type<T>(), error.what()));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace larra::xmpp::serialization
|
18
library/include/larra/serialization/error.hpp
Normal file
18
library/include/larra/serialization/error.hpp
Normal file
|
@ -0,0 +1,18 @@
|
|||
#pragma once
|
||||
#include <stdexcept>
|
||||
|
||||
namespace larra::xmpp::serialization {
|
||||
|
||||
struct SerializationError : std::runtime_error {
|
||||
using std::runtime_error::runtime_error;
|
||||
};
|
||||
|
||||
struct AttributeSerializationError : SerializationError {
|
||||
using SerializationError::SerializationError;
|
||||
};
|
||||
|
||||
struct ElementSerializationError : SerializationError {
|
||||
using SerializationError::SerializationError;
|
||||
};
|
||||
|
||||
} // namespace larra::xmpp::serialization
|
|
@ -1,8 +1,12 @@
|
|||
#include <gtest/gtest.h>
|
||||
|
||||
#include <larra/serialization.hpp>
|
||||
#include <larra/serialization/auto.hpp>
|
||||
#include <larra/serialization/error.hpp>
|
||||
#include <larra/stream_error.hpp>
|
||||
|
||||
using namespace std::literals;
|
||||
|
||||
namespace larra::xmpp {
|
||||
|
||||
TEST(Parse, Variant) {
|
||||
|
@ -21,8 +25,83 @@ TEST(Serialize, Variant) {
|
|||
auto node = doc.create_root_node("stream:error");
|
||||
S::Serialize(node, data);
|
||||
EXPECT_EQ(doc.write_to_string(),
|
||||
std::string_view{"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<stream:error><unsupported-stanza-type "
|
||||
"xmlns=\"urn:ietf:params:xml:ns:xmpp-streams\"/></stream:error>\n"});
|
||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<stream:error><unsupported-stanza-type "
|
||||
"xmlns=\"urn:ietf:params:xml:ns:xmpp-streams\"/></stream:error>\n"sv);
|
||||
}
|
||||
|
||||
namespace tests::serialization {
|
||||
|
||||
struct SomeStruct {
|
||||
static constexpr auto kDefaultName = "some";
|
||||
std::string value;
|
||||
[[nodiscard]] static auto Parse(xmlpp::Element* element) -> SomeStruct;
|
||||
};
|
||||
|
||||
struct SomeStruct2 {
|
||||
static constexpr auto kDefaultName = "some2";
|
||||
SomeStruct value;
|
||||
[[nodiscard]] static auto Parse(xmlpp::Element* element) -> SomeStruct2;
|
||||
};
|
||||
|
||||
struct SomeStruct3 {
|
||||
static constexpr auto kDefaultName = "some3";
|
||||
int value;
|
||||
[[nodiscard]] static auto Parse(xmlpp::Element* element) -> SomeStruct3;
|
||||
};
|
||||
|
||||
struct SomeStruct4 {
|
||||
static constexpr auto kDefaultName = "some4";
|
||||
SomeStruct3 value;
|
||||
[[nodiscard]] static auto Parse(xmlpp::Element* element) -> SomeStruct4;
|
||||
};
|
||||
|
||||
} // namespace tests::serialization
|
||||
|
||||
namespace serialization {
|
||||
|
||||
template <>
|
||||
constexpr auto kSerializationConfig<tests::serialization::SomeStruct> = SerializationConfig<tests::serialization::SomeStruct>{};
|
||||
|
||||
template <>
|
||||
constexpr auto kSerializationConfig<tests::serialization::SomeStruct2> =
|
||||
SerializationConfig<tests::serialization::SomeStruct2>{}.With<"value">(serialization::Config<tests::serialization::SomeStruct>{});
|
||||
|
||||
template <>
|
||||
constexpr auto kSerializationConfig<tests::serialization::SomeStruct4> =
|
||||
SerializationConfig<tests::serialization::SomeStruct4>{}.With<"value">(serialization::Config<tests::serialization::SomeStruct3>{});
|
||||
|
||||
} // namespace serialization
|
||||
|
||||
auto tests::serialization::SomeStruct::Parse(xmlpp::Element* element) -> SomeStruct {
|
||||
return ::larra::xmpp::serialization::Parse<tests::serialization::SomeStruct>(element);
|
||||
}
|
||||
|
||||
auto tests::serialization::SomeStruct2::Parse(xmlpp::Element* element) -> SomeStruct2 {
|
||||
return ::larra::xmpp::serialization::Parse<tests::serialization::SomeStruct2>(element);
|
||||
}
|
||||
|
||||
auto tests::serialization::SomeStruct3::Parse(xmlpp::Element*) -> SomeStruct3 {
|
||||
return {.value = 42}; // NOLINT
|
||||
}
|
||||
|
||||
auto tests::serialization::SomeStruct4::Parse(xmlpp::Element* element) -> SomeStruct4 {
|
||||
return ::larra::xmpp::serialization::Parse<tests::serialization::SomeStruct4>(element);
|
||||
}
|
||||
|
||||
TEST(Parse, Auto) {
|
||||
xmlpp::Document doc;
|
||||
auto node = doc.create_root_node("some2");
|
||||
node = node->add_child_element("some");
|
||||
node->set_attribute("value", "Hello");
|
||||
auto a = Serialization<tests::serialization::SomeStruct>::Parse(node);
|
||||
EXPECT_EQ(a.value, "Hello"sv);
|
||||
auto b = Serialization<tests::serialization::SomeStruct2>::Parse(doc.get_root_node());
|
||||
EXPECT_EQ(b.value.value, "Hello"sv);
|
||||
EXPECT_THROW(std::ignore = tests::serialization::SomeStruct2::Parse(node), serialization::SerializationError);
|
||||
auto node2 = node->add_child_element("some4");
|
||||
node2->add_child_element("some3");
|
||||
auto c = Serialization<tests::serialization::SomeStruct4>::Parse(node2);
|
||||
EXPECT_EQ(c.value.value, 42);
|
||||
}
|
||||
|
||||
} // namespace larra::xmpp
|
||||
|
|
Loading…
Reference in a new issue