From c3764e83ce7f9a3bebe0db4a417a351066efb045 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dr=2E=20Patrick=20Urbanke=20=28=E5=8A=89=E8=87=AA=E6=88=90?= =?UTF-8?q?=29?= Date: Thu, 20 Feb 2025 22:14:32 +0100 Subject: [PATCH] Added remove_rename (#360); fixes #359 --- include/rfl/internal/is_attribute.hpp | 5 +-- include/rfl/internal/remove_rename.hpp | 34 ++++++++++++++++ tests/xml/test_attribute_rename.cpp | 55 ++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 include/rfl/internal/remove_rename.hpp create mode 100644 tests/xml/test_attribute_rename.cpp diff --git a/include/rfl/internal/is_attribute.hpp b/include/rfl/internal/is_attribute.hpp index 724c0881..bf125c11 100644 --- a/include/rfl/internal/is_attribute.hpp +++ b/include/rfl/internal/is_attribute.hpp @@ -7,9 +7,9 @@ #include "../Attribute.hpp" #include "../Field.hpp" +#include "remove_rename.hpp" namespace rfl { - namespace internal { template @@ -22,8 +22,7 @@ template class is_attribute> : public std::true_type {}; template -constexpr bool is_attribute_v = - is_attribute>>::value; +constexpr bool is_attribute_v = is_attribute>::value; } // namespace internal } // namespace rfl diff --git a/include/rfl/internal/remove_rename.hpp b/include/rfl/internal/remove_rename.hpp new file mode 100644 index 00000000..d371dffb --- /dev/null +++ b/include/rfl/internal/remove_rename.hpp @@ -0,0 +1,34 @@ +#ifndef RFL_INTERNAL_REMOVERENAME_HPP_ +#define RFL_INTERNAL_REMOVERENAME_HPP_ + +#include +#include +#include + +#include "../Rename.hpp" +#include "StringLiteral.hpp" + +namespace rfl { +namespace internal { + +template +struct remove_rename; + +template +struct remove_rename { + using Type = T; +}; + +template +struct remove_rename> { + using Type = _Type; +}; + +template +using remove_rename_t = + typename remove_rename>>::Type; + +} // namespace internal +} // namespace rfl + +#endif diff --git a/tests/xml/test_attribute_rename.cpp b/tests/xml/test_attribute_rename.cpp new file mode 100644 index 00000000..c5326771 --- /dev/null +++ b/tests/xml/test_attribute_rename.cpp @@ -0,0 +1,55 @@ +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_attribute_rename { + +using Age = rfl::Validator, rfl::Maximum<130>>; + +struct Person { + rfl::Rename<"firstName", rfl::Attribute> first_name; + rfl::Rename<"lastName", rfl::Attribute> last_name = "Simpson"; + rfl::Attribute town = "Springfield"; + rfl::Attribute> birthday; + rfl::Attribute age; + rfl::Attribute email; + std::vector child; +}; + +TEST(xml, test_attribute_rename) { + const auto bart = Person{.first_name = "Bart", + .birthday = "1987-04-19", + .age = 10, + .email = "bart@simpson.com"}; + + const auto lisa = Person{.first_name = "Lisa", + .birthday = "1987-04-19", + .age = 8, + .email = "lisa@simpson.com"}; + + const auto maggie = Person{.first_name = "Maggie", + .birthday = "1987-04-19", + .age = 0, + .email = "maggie@simpson.com"}; + + const auto homer = Person{.first_name = "Homer", + .birthday = "1987-04-19", + .age = 45, + .email = "homer@simpson.com", + .child = std::vector({bart, lisa, maggie})}; + + const auto expected = R"( + + + + + +)"; + + EXPECT_EQ(rfl::xml::write(homer), expected); +} + +} // namespace test_attribute_rename