From 23ce24f00198f511e084b959a4e2064e9649cd52 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: Sun, 22 Dec 2024 15:43:17 +0100 Subject: [PATCH] Make sure you can insert unsupported types into rfl::Skip<...>; #299 (#304) --- include/rfl/parsing/Parser_skip.hpp | 11 +++++------ tests/json/test_skip2.cpp | 27 +++++++++++++++++++++++++++ tests/yaml/test_skip.cpp | 28 ++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 tests/json/test_skip2.cpp create mode 100644 tests/yaml/test_skip.cpp diff --git a/include/rfl/parsing/Parser_skip.hpp b/include/rfl/parsing/Parser_skip.hpp index abcfb0c6..dab03012 100644 --- a/include/rfl/parsing/Parser_skip.hpp +++ b/include/rfl/parsing/Parser_skip.hpp @@ -7,6 +7,7 @@ #include "../Result.hpp" #include "../always_false.hpp" #include "../internal/Skip.hpp" +#include "Parent.hpp" #include "Parser_base.hpp" #include "schema/Type.hpp" @@ -15,12 +16,13 @@ namespace parsing { template -requires AreReaderAndWriter< - R, W, internal::Skip> + requires AreReaderAndWriter< + R, W, internal::Skip> struct Parser, ProcessorsType> { using InputVarType = typename R::InputVarType; + using ParentType = Parent; static Result> read(const R& _r, const InputVarType& _var) noexcept { @@ -44,10 +46,7 @@ struct Parser& _skip, const P& _parent) noexcept { if constexpr (_skip_serialization) { - using ReflectionType = std::remove_cvref_t::ReflectionType>; - Parser::write(_w, ReflectionType(), - _parent); + ParentType::add_null(_w, _parent); } else { Parser, ProcessorsType>::write( _w, _skip.value(), _parent); diff --git a/tests/json/test_skip2.cpp b/tests/json/test_skip2.cpp new file mode 100644 index 00000000..50d8273b --- /dev/null +++ b/tests/json/test_skip2.cpp @@ -0,0 +1,27 @@ +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_skip2 { + +using Age = rfl::Validator, rfl::Maximum<130>>; + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name; + rfl::Skip town; // Is unsupported by the JSON format + Age age; +}; + +TEST(json, test_skip2) { + const auto homer = + Person{.first_name = "Homer", .last_name = "Simpson", .age = 45}; + + write_and_read(homer, + R"({"firstName":"Homer","lastName":"Simpson","age":45})"); +} +} // namespace test_skip2 diff --git a/tests/yaml/test_skip.cpp b/tests/yaml/test_skip.cpp new file mode 100644 index 00000000..523007a7 --- /dev/null +++ b/tests/yaml/test_skip.cpp @@ -0,0 +1,28 @@ +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_skip { + +using Age = rfl::Validator, rfl::Maximum<130>>; + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name; + rfl::Skip town; + Age age; +}; + +TEST(yaml, test_skip) { + const auto homer = Person{.first_name = "Homer", + .last_name = "Simpson", + .town = "Springfield", + .age = 45}; + + write_and_read(homer); +} +} // namespace test_skip