Compare commits
3 commits
b11e198134
...
d1dc6f8e24
Author | SHA1 | Date | |
---|---|---|---|
d1dc6f8e24 | |||
0d471b579a | |||
2c0111f9d7 |
6 changed files with 476 additions and 27 deletions
|
@ -10,7 +10,7 @@ FROM archlinux@sha256:a10e51dd0694d6c4142754e9d06cbce7baf91ace8031a30df37064d109
|
|||
RUN pacman -Syyu --noconfirm \
|
||||
&& pacman -Syyu --noconfirm git less vim sudo python-pip wget which pkgconf \
|
||||
&& pacman -Syyu --noconfirm cmake make gcc ninja meson shellcheck \
|
||||
&& pacman -Syyu --noconfirm gtk4 gtkmm-4.0 boost spdlog fmt libxml++-5.0
|
||||
&& pacman -Syyu --noconfirm gtk4 gtkmm-4.0 boost spdlog fmt libxml++-5.0 gtest
|
||||
|
||||
# Create a non-root user 'dev'
|
||||
RUN useradd -ms /bin/bash dev \
|
||||
|
@ -38,6 +38,7 @@ ADD https://github.com/llvm/llvm-project/releases/download/llvmorg-${LLVM_VER}/L
|
|||
# Create the LLVM directory and extract only binaries into it
|
||||
RUN sudo mkdir -p /home/LLVM-${LLVM_VER}
|
||||
RUN sudo tar -xJf /home/artifacts/LLVM-${LLVM_VER}-Linux-X64.tar.xz -C /home/LLVM-${LLVM_VER} --strip-components=1 \
|
||||
LLVM-${LLVM_VER}-Linux-X64/bin/clangd \
|
||||
LLVM-${LLVM_VER}-Linux-X64/bin/clang-19 \
|
||||
LLVM-${LLVM_VER}-Linux-X64/bin/clang \
|
||||
LLVM-${LLVM_VER}-Linux-X64/bin/clang++ \
|
||||
|
|
|
@ -120,6 +120,8 @@ jobs:
|
|||
echo "No clang-format violations detected!"
|
||||
|
||||
- name: GCC build
|
||||
id: gcc_build
|
||||
continue-on-error: true
|
||||
run: |
|
||||
mkdir -p ${{ github.workspace }}/build_gcc
|
||||
cmake -Wno-dev \
|
||||
|
@ -129,32 +131,56 @@ jobs:
|
|||
-B ${{ github.workspace }}/build_gcc \
|
||||
-GNinja -DCMAKE_BUILD_TYPE=Release \
|
||||
-DENABLE_EXAMPLES=ON \
|
||||
-DENABLE_TESTS=ON
|
||||
-DENABLE_TESTS=ON \
|
||||
-DCMAKE_CXX_FLAGS="-ftemplate-backtrace-limit=0"
|
||||
cmake --build ${{ github.workspace }}/build_gcc --parallel `nproc`
|
||||
|
||||
- name: GCC unit tests
|
||||
id: gcc_unit_tests
|
||||
continue-on-error: true
|
||||
run: |
|
||||
cd ${{ github.workspace }}/build_gcc
|
||||
./larra_xmpp_tests
|
||||
|
||||
- name: Clang build (only configuring)
|
||||
id: clang_build
|
||||
continue-on-error: true
|
||||
run: |
|
||||
export LLVM_VER=19.1.1
|
||||
export PATH="/home/LLVM-${LLVM_VER}/bin:${PATH}"
|
||||
mkdir -p ${{ github.workspace }}/build_clang
|
||||
|
||||
cmake -Wno-dev \
|
||||
-DCMAKE_C_COMPILER=clang \
|
||||
-DCMAKE_CXX_COMPILER=clang++ \
|
||||
-S ${{ github.workspace }} \
|
||||
-B ${{ github.workspace }}/build_clang \
|
||||
-GNinja -DCMAKE_BUILD_TYPE=Release \
|
||||
-DENABLE_EXAMPLES=ON \
|
||||
-DENABLE_TESTS=ON \
|
||||
-DCMAKE_CXX_FLAGS="-stdlib=libc++ -I/home/LLVM-${LLVM_VER}/include/c++/v1 -fno-modules"
|
||||
|
||||
echo "::group::compile_commands.json content"
|
||||
cat ${{ github.workspace }}/build_clang/compile_commands.json
|
||||
echo "::endgroup::"
|
||||
|
||||
sed -i 's|@.*\.modmap||' ${{ github.workspace }}/build_clang/compile_commands.json
|
||||
|
||||
- name: Check clang-tidy
|
||||
id: clang_tidy
|
||||
continue-on-error: true
|
||||
run: |
|
||||
export LLVM_VER=19.1.1
|
||||
export PATH="/home/LLVM-${LLVM_VER}/bin:${PATH}"
|
||||
cd ${{ github.workspace }}
|
||||
|
||||
sed -i 's|/usr/sbin/g++|/home/LLVM-19.1.1/bin/clang++ -stdlib=libc++ -I/home/LLVM-${LLVM_VER}/include/c++/v1|' build_gcc/compile_commands.json
|
||||
sed -i 's|-fdeps-format=p1689r5||' build_gcc/compile_commands.json
|
||||
sed -i 's|-fmodules-ts||' build_gcc/compile_commands.json
|
||||
sed -i 's|-fmodule-mapper=.*\.modmap||' build_gcc/compile_commands.json
|
||||
|
||||
wget https://raw.githubusercontent.com/llvm/llvm-project/refs/heads/main/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py
|
||||
python run-clang-tidy.py \
|
||||
-warnings-as-errors=* \
|
||||
-use-color \
|
||||
-exclude-header-filter .*build.* \
|
||||
-header-filter .hpp \
|
||||
-p ${{ github.workspace }}/build_gcc/ \
|
||||
-p ${{ github.workspace }}/build_clang/ \
|
||||
-config-file ${{ github.workspace }}/.clang-tidy \
|
||||
\
|
||||
${{ github.workspace }}/examples \
|
||||
|
@ -163,22 +189,9 @@ jobs:
|
|||
|
||||
echo "No clang-tidy violations detected!"
|
||||
|
||||
#- name: Clang build with -fsanitize=address
|
||||
# run: |
|
||||
# export LLVM_VER=19.1.1
|
||||
# export PATH="/home/LLVM-${LLVM_VER}/bin:${PATH}"
|
||||
# mkdir -p ${{ github.workspace }}/build_clang
|
||||
# cmake -Wno-dev \
|
||||
# -DCMAKE_C_COMPILER=clang \
|
||||
# -DCMAKE_CXX_COMPILER=clang++ \
|
||||
# -S ${{ github.workspace }} \
|
||||
# -B ${{ github.workspace }}/build_clang \
|
||||
# -GNinja -DCMAKE_BUILD_TYPE=Release \
|
||||
# -DENABLE_EXAMPLES=ON \
|
||||
# -DENABLE_TESTS=ON \
|
||||
# -DCMAKE_CXX_FLAGS="-stdlib=libc++ -I/home/LLVM-${LLVM_VER}/include/c++/v1 -fno-omit-frame-pointer -g -fsanitize=address,undefined,leak,function,nullability,vptr" \
|
||||
# -DCMAKE_EXE_LINKER_FLAGS="-L/home/LLVM-${LLVM_VER}/lib/ -Wl,-rpath,/home/LLVM-${LLVM_VER}/lib -lc++ -lc++abi -lm -lc -lgcc_s -lgcc -fno-omit-frame-pointer -g -fsanitize=address,undefined,leak,function,nullability,vptr"
|
||||
# cmake --build ${{ github.workspace }}/build_clang --parallel `nproc`
|
||||
- name: Check on failures
|
||||
if: steps.gcc_build.outcome != 'success' || steps.gcc_unit_tests.outcome != 'success' || steps.clang_build.outcome != 'success' || steps.clang_tidy.outcome != 'success'
|
||||
run: exit 1
|
||||
|
||||
#- name: Clang unit tests with -fsanitize=address
|
||||
# run: |
|
||||
|
|
|
@ -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::ParsingError{
|
||||
std::format("[{}: {}] parsing error: [ StartCheck failed ]", Serialization::kDefaultName, nameof::nameof_full_type<T>())};
|
||||
}
|
||||
return T::Parse(element);
|
||||
}
|
||||
|
|
270
library/include/larra/serialization/auto.hpp
Normal file
270
library/include/larra/serialization/auto.hpp
Normal file
|
@ -0,0 +1,270 @@
|
|||
#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;
|
||||
|
||||
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>);
|
||||
|
||||
template <typename T>
|
||||
constexpr auto Serialize(xmlpp::Element* node, const T& element) -> void;
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
// GCC workaround: operator==
|
||||
|
||||
struct AttributeConfig {
|
||||
auto operator==(const AttributeConfig&) const -> bool = default;
|
||||
template <typename T>
|
||||
constexpr auto operator==(const Config<T>&) const -> bool {
|
||||
return false;
|
||||
}
|
||||
template <typename T>
|
||||
friend constexpr auto operator==(const Config<T>&, const AttributeConfig&) -> bool {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
constexpr auto operator==(const Config<T>&, const Config<T>&) -> bool {
|
||||
return false;
|
||||
}
|
||||
|
||||
template <>
|
||||
struct Config<std::string> {
|
||||
std::optional<std::string_view> defaultValue;
|
||||
};
|
||||
|
||||
namespace impl {
|
||||
template <typename T>
|
||||
concept HasParse = requires(xmlpp::Element* e) {
|
||||
{ T::Parse(e) } -> std::same_as<T>;
|
||||
};
|
||||
|
||||
template <typename T, typename V>
|
||||
struct Config : V {
|
||||
using V::V;
|
||||
constexpr Config()
|
||||
requires HasParse<T>
|
||||
: V(::larra::xmpp::serialization::Config<T>{}) {
|
||||
}
|
||||
|
||||
constexpr Config()
|
||||
requires(!HasParse<T>)
|
||||
: V(AttributeConfig{}) {
|
||||
}
|
||||
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 ElementParsingError(std::format("[{}: {}] parsing error: [ Not found ]", Info::kName, nameof::nameof_full_type<T>()));
|
||||
}
|
||||
auto elementNode = dynamic_cast<xmlpp::Element*>(node);
|
||||
if(!node) {
|
||||
throw ElementParsingError(std::format("[{}: {}] parsing error: [ Invalid node ]", Info::kName, nameof::nameof_full_type<T>()));
|
||||
}
|
||||
try {
|
||||
return ::larra::xmpp::serialization::Parse(elementNode, Tag<T>{});
|
||||
} catch(const std::exception& error) {
|
||||
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) {
|
||||
throw ElementSerializaionError(
|
||||
std::format("[{}: {}] serialization error: [ node creation failed ]", Info::kName, nameof::nameof_full_type<T>()));
|
||||
}
|
||||
try {
|
||||
::larra::xmpp::serialization::Serialize(created, element);
|
||||
} catch(const std::exception& err) {
|
||||
throw ElementSerializaionError(
|
||||
std::format("[{}: {}] serialization error: [ {} ]", Info::kName, nameof::nameof_full_type<T>(), err.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) -> 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();
|
||||
}
|
||||
} else {
|
||||
return ElementSerializer<Type, Config, Info>::Parse(main);
|
||||
}
|
||||
}
|
||||
|
||||
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>()));
|
||||
}
|
||||
} else {
|
||||
ElementSerializer<T, Config, Info>::Serialize(main, obj);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace impl
|
||||
|
||||
template <typename T>
|
||||
struct SerializationConfig {
|
||||
decltype([] {
|
||||
return [](auto... is) {
|
||||
return utempl::Tuple<typename ElementConfig<boost::pfr::tuple_element_t<*is, T>>::type...>{};
|
||||
} | 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 = kDeserializationConfig<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 ParsingError& error) {
|
||||
throw ElementParsingError(std::format("[{}] parsing error: [ {} ]", nameof::nameof_full_type<T>(), error.what()));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
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>)
|
||||
{
|
||||
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 {
|
||||
(impl::SerializeField<*((*configs).second), FieldInfo<T, (*configs).first>>(node, boost::pfr::get<(*configs).first>(element)), ...);
|
||||
} catch(const ParsingError& error) {
|
||||
throw ElementParsingError(std::format("[{}] parsing error: [ {} ]", nameof::nameof_full_type<T>(), error.what()));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr auto Serialize(xmlpp::Element* node, const T& element) -> void {
|
||||
Serialization<T>::Serialize(node, element);
|
||||
}
|
||||
|
||||
} // namespace larra::xmpp::serialization
|
30
library/include/larra/serialization/error.hpp
Normal file
30
library/include/larra/serialization/error.hpp
Normal file
|
@ -0,0 +1,30 @@
|
|||
#pragma once
|
||||
#include <stdexcept>
|
||||
|
||||
namespace larra::xmpp::serialization {
|
||||
|
||||
struct ParsingError : std::runtime_error {
|
||||
using std::runtime_error::runtime_error;
|
||||
};
|
||||
|
||||
struct AttributeParsingError : ParsingError {
|
||||
using ParsingError::ParsingError;
|
||||
};
|
||||
|
||||
struct ElementParsingError : ParsingError {
|
||||
using ParsingError::ParsingError;
|
||||
};
|
||||
|
||||
struct SerializationError : std::runtime_error {
|
||||
using std::runtime_error::runtime_error;
|
||||
};
|
||||
|
||||
struct AttributeSerializationError : SerializationError {
|
||||
using SerializationError::SerializationError;
|
||||
};
|
||||
|
||||
struct ElementSerializaionError : SerializationError {
|
||||
using SerializationError::SerializationError;
|
||||
};
|
||||
|
||||
} // namespace larra::xmpp::serialization
|
|
@ -1,8 +1,13 @@
|
|||
#include <gtest/gtest.h>
|
||||
|
||||
#include <larra/jid.hpp>
|
||||
#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 +26,136 @@ 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;
|
||||
friend auto operator<<(xmlpp::Element* element, const SomeStruct& self);
|
||||
};
|
||||
|
||||
struct SomeStruct2 {
|
||||
static constexpr auto kDefaultName = "some2";
|
||||
SomeStruct value;
|
||||
[[nodiscard]] static auto Parse(xmlpp::Element* element) -> SomeStruct2;
|
||||
friend auto operator<<(xmlpp::Element* element, const SomeStruct2& self);
|
||||
};
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
struct SomeStruct5 {
|
||||
static constexpr auto kDefaultName = "some5";
|
||||
BareJid value;
|
||||
[[nodiscard]] static auto Parse(xmlpp::Element* element) -> SomeStruct5;
|
||||
friend auto operator<<(xmlpp::Element* element, const SomeStruct5& self);
|
||||
};
|
||||
|
||||
} // 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>{};
|
||||
|
||||
template <>
|
||||
constexpr auto kSerializationConfig<tests::serialization::SomeStruct4> = SerializationConfig<tests::serialization::SomeStruct4>{};
|
||||
|
||||
template <>
|
||||
constexpr auto kSerializationConfig<tests::serialization::SomeStruct5> = SerializationConfig<tests::serialization::SomeStruct5>{};
|
||||
|
||||
} // namespace serialization
|
||||
|
||||
namespace tests::serialization {
|
||||
|
||||
auto SomeStruct::Parse(xmlpp::Element* element) -> SomeStruct {
|
||||
return ::larra::xmpp::serialization::Parse<tests::serialization::SomeStruct>(element);
|
||||
}
|
||||
|
||||
auto SomeStruct2::Parse(xmlpp::Element* element) -> SomeStruct2 {
|
||||
return ::larra::xmpp::serialization::Parse<tests::serialization::SomeStruct2>(element);
|
||||
}
|
||||
|
||||
auto SomeStruct3::Parse(xmlpp::Element*) -> SomeStruct3 {
|
||||
return {.value = 42}; // NOLINT
|
||||
}
|
||||
|
||||
auto SomeStruct4::Parse(xmlpp::Element* element) -> SomeStruct4 {
|
||||
return ::larra::xmpp::serialization::Parse<tests::serialization::SomeStruct4>(element);
|
||||
}
|
||||
|
||||
auto SomeStruct5::Parse(xmlpp::Element* element) -> SomeStruct5 {
|
||||
return ::larra::xmpp::serialization::Parse<tests::serialization::SomeStruct5>(element);
|
||||
}
|
||||
|
||||
auto operator<<(xmlpp::Element* element, const SomeStruct& self) {
|
||||
::larra::xmpp::serialization::Serialize(element, self);
|
||||
}
|
||||
|
||||
auto operator<<(xmlpp::Element* element, const SomeStruct2& self) {
|
||||
::larra::xmpp::serialization::Serialize(element, self);
|
||||
}
|
||||
|
||||
auto operator<<(xmlpp::Element* element, const SomeStruct5& self) {
|
||||
::larra::xmpp::serialization::Serialize(element, self);
|
||||
}
|
||||
|
||||
} // namespace tests::serialization
|
||||
|
||||
TEST(AutoParse, Basic) {
|
||||
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::ParsingError);
|
||||
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);
|
||||
}
|
||||
|
||||
TEST(AutoParse, Attribute) {
|
||||
xmlpp::Document doc;
|
||||
auto node = doc.create_root_node("some5");
|
||||
node->set_attribute("value", "user@server.i2p");
|
||||
auto a = Serialization<tests::serialization::SomeStruct5>::Parse(node);
|
||||
EXPECT_EQ(a.value.server, "server.i2p"sv);
|
||||
EXPECT_EQ(a.value.username, "user"sv);
|
||||
}
|
||||
|
||||
TEST(AutoSerialize, Basic) {
|
||||
xmlpp::Document doc;
|
||||
auto node = doc.create_root_node("some2");
|
||||
node << tests::serialization::SomeStruct2{.value = {.value = "testData"}};
|
||||
EXPECT_EQ(doc.write_to_string(), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<some2><some value=\"testData\"/></some2>\n");
|
||||
}
|
||||
|
||||
TEST(AutoSerialize, Attribute) {
|
||||
xmlpp::Document doc;
|
||||
auto node = doc.create_root_node("some5");
|
||||
node << tests::serialization::SomeStruct5{.value = {.username = "user", .server = "server.i2p"}};
|
||||
EXPECT_EQ(doc.write_to_string(), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<some5 value=\"user@server.i2p\"/>\n");
|
||||
}
|
||||
|
||||
} // namespace larra::xmpp
|
||||
|
|
Loading…
Reference in a new issue