From adf8463527233e75efd9f3065f39d6d889e20984 Mon Sep 17 00:00:00 2001 From: liuzicheng1987 Date: Tue, 21 May 2024 18:13:42 +0200 Subject: [PATCH] Added rfl::Skip; resolves #99 (#103) --- docs/README.md | 6 +- docs/rfl_skip.md | 57 +++++++++ include/rfl.hpp | 1 + include/rfl/Skip.hpp | 20 ++++ include/rfl/internal/Skip.hpp | 146 +++++++++++++++++++++++ include/rfl/internal/is_skip.hpp | 26 ++++ include/rfl/parsing/NamedTupleParser.hpp | 9 +- include/rfl/parsing/Parser.hpp | 1 + include/rfl/parsing/Parser_skip.hpp | 68 +++++++++++ include/rfl/parsing/is_empty.hpp | 3 + tests/json/test_skip.cpp | 30 +++++ tests/msgpack/test_skip.cpp | 27 +++++ 12 files changed, 389 insertions(+), 5 deletions(-) create mode 100644 docs/rfl_skip.md create mode 100644 include/rfl/Skip.hpp create mode 100644 include/rfl/internal/Skip.hpp create mode 100644 include/rfl/internal/is_skip.hpp create mode 100644 include/rfl/parsing/Parser_skip.hpp create mode 100644 tests/json/test_skip.cpp create mode 100644 tests/msgpack/test_skip.cpp diff --git a/docs/README.md b/docs/README.md index 6640a773..94125faf 100644 --- a/docs/README.md +++ b/docs/README.md @@ -24,9 +24,11 @@ 1.10) [rfl::Timestamp](https://github.com/getml/reflect-cpp/blob/main/docs/timestamps.md) - For serializing and deserializing timestamps. -1.11) [rfl::Result](https://github.com/getml/reflect-cpp/blob/main/docs/result.md) - For error handling without exceptions. +1.11) [rfl::Skip](https://github.com/getml/reflect-cpp/blob/main/docs/rfl_skip.md) - For skipping fields during serialization and/or deserialization. -1.12) [Standard containers](https://github.com/getml/reflect-cpp/blob/main/docs/standard_containers.md) - Describes how reflect-cpp treats containers in the standard library. +1.12) [rfl::Result](https://github.com/getml/reflect-cpp/blob/main/docs/result.md) - For error handling without exceptions. + +1.13) [Standard containers](https://github.com/getml/reflect-cpp/blob/main/docs/standard_containers.md) - Describes how reflect-cpp treats containers in the standard library. ## 2) Validation diff --git a/docs/rfl_skip.md b/docs/rfl_skip.md new file mode 100644 index 00000000..e256e206 --- /dev/null +++ b/docs/rfl_skip.md @@ -0,0 +1,57 @@ +# `rfl::Skip` + +It is possible to skip the serialization and deserialization of fields using `rfl::Skip`. + +```cpp +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name; + rfl::Skip town; + Age age; +}; +``` + +This means that the field `town` will be ignored: + +```json +{"firstName":"Homer","lastName":"Simpson","age":45} +``` + +If you only want to skip the deserialization, but not the serialization, you can use `rfl::SkipDeserialization`: + + +```cpp +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name; + rfl::SkipDeserialization town; + Age age; +}; +``` + +If you only want to skip the serialization, but not the deserialization, you can use `rfl::SkipSerialization`. +Note that this implies that the serializied data cannot be deserialized, because the field `town` +is expected during deserialization, but not serialized. + +```cpp +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name; + rfl::SkipSerialization town; + Age age; +}; +``` + +You can access the underlying value in the field `town` using any of the following operators: + +```cpp +person.town(); +person.town.get(); +person.town.value(); +``` + +You can assign the underlying field just like any other field: + +```cpp +person.town = "Springfield"; +``` diff --git a/include/rfl.hpp b/include/rfl.hpp index 705e47bf..4a53a0a7 100644 --- a/include/rfl.hpp +++ b/include/rfl.hpp @@ -24,6 +24,7 @@ #include "rfl/Ref.hpp" #include "rfl/Rename.hpp" #include "rfl/Size.hpp" +#include "rfl/Skip.hpp" #include "rfl/SnakeCaseToCamelCase.hpp" #include "rfl/SnakeCaseToPascalCase.hpp" #include "rfl/TaggedUnion.hpp" diff --git a/include/rfl/Skip.hpp b/include/rfl/Skip.hpp new file mode 100644 index 00000000..9adc8b60 --- /dev/null +++ b/include/rfl/Skip.hpp @@ -0,0 +1,20 @@ +#ifndef RFL_SKIP_HPP_ +#define RFL_SKIP_HPP_ + +#include "internal/Skip.hpp" + +namespace rfl { + +template +using Skip = internal::Skip; + +template +using SkipSerialization = internal::Skip; + +template +using SkipDeserialization = internal::Skip; + +} // namespace rfl + +#endif + diff --git a/include/rfl/internal/Skip.hpp b/include/rfl/internal/Skip.hpp new file mode 100644 index 00000000..2f8b698e --- /dev/null +++ b/include/rfl/internal/Skip.hpp @@ -0,0 +1,146 @@ +#ifndef RFL_INTERNAL_SKIP_HPP_ +#define RFL_INTERNAL_SKIP_HPP_ + +#include + +namespace rfl::internal { + +template +class Skip { + private: + using SelfType = Skip; + + public: + static constexpr bool skip_serialization_ = _skip_serialization; + static constexpr bool skip_deserialization_ = _skip_deserialization; + + /// The underlying type. + using Type = T; + using ReflectionType = std::optional; + + Skip() : value_(Type()) {} + + Skip(const Type& _value) : value_(_value) {} + + Skip(ReflectionType&& _value) noexcept + : value_(_value ? std::move(*_value) : Type()) {} + + Skip(const ReflectionType& _value) : value_(_value ? *_value : Type()) {} + + Skip(Type&& _value) noexcept : value_(std::move(_value)) {} + + Skip(SelfType&& _skip) noexcept = default; + + Skip(const SelfType& _skip) = default; + + template + Skip(const Skip& _other) : value_(_other.get()) {} + + template + Skip(Skip&& _other) : value_(_other.get()) {} + + template , + bool>::type = true> + Skip(const U& _value) : value_(_value) {} + + template , + bool>::type = true> + Skip(U&& _value) noexcept : value_(std::forward(_value)) {} + + template , + bool>::type = true> + Skip(const Skip& _skip) : value_(_skip.value()) {} + + /// Assigns the underlying object to its default value. + template , + bool>::type = true> + Skip(const Default& _default) : value_(Type()) {} + + ~Skip() = default; + + /// Returns the underlying object. + Type& get() { return value_; } + + /// Returns the underlying object. + const Type& get() const { return value_; } + + /// Returns the underlying object. + Type& operator()() { return value_; } + + /// Returns the underlying object. + const Type& operator()() const { return value_; } + + /// Assigns the underlying object. + auto& operator=(const Type& _value) { + value_ = _value; + return *this; + } + + /// Assigns the underlying object. + auto& operator=(Type&& _value) noexcept { + value_ = std::move(_value); + return *this; + } + + /// Assigns the underlying object. + template , + bool>::type = true> + auto& operator=(const U& _value) { + value_ = _value; + return *this; + } + + /// Assigns the underlying object to its default value. + template , + bool>::type = true> + auto& operator=(const Default& _default) { + value_ = Type(); + return *this; + } + + /// Assigns the underlying object. + SelfType& operator=(const SelfType& _other) = default; + + /// Assigns the underlying object. + SelfType& operator=(SelfType&& _other) = default; + + /// Assigns the underlying object. + template + auto& operator=(const Skip& _skip) { + value_ = _skip.get(); + return *this; + } + + /// Assigns the underlying object. + template + auto& operator=(Skip&& _skip) { + value_ = std::forward(_skip.value_); + return *this; + } + + /// Returns the ReflectionType - necessary for the serialization to work. + ReflectionType reflection() const { return value_; } + + /// Assigns the underlying object. + void set(const Type& _value) { value_ = _value; } + + /// Assigns the underlying object. + void set(Type&& _value) { value_ = std::move(_value); } + + /// Returns the underlying object. + Type& value() { return value_; } + + /// Returns the underlying object. + const Type& value() const { return value_; } + + private: + /// The underlying value + T value_; +}; + +} // namespace rfl::internal + +#endif diff --git a/include/rfl/internal/is_skip.hpp b/include/rfl/internal/is_skip.hpp new file mode 100644 index 00000000..d3357d0e --- /dev/null +++ b/include/rfl/internal/is_skip.hpp @@ -0,0 +1,26 @@ +#ifndef RFL_INTERNAL_ISSKIP_HPP_ +#define RFL_INTERNAL_ISSKIP_HPP_ + +#include + +#include "Skip.hpp" + +namespace rfl::internal { + +template +class is_skip; + +template +class is_skip : public std::false_type {}; + +template +class is_skip> + : public std::true_type {}; + +template +constexpr bool is_skip_v = + is_skip>>::value; + +} // namespace rfl::internal + +#endif diff --git a/include/rfl/parsing/NamedTupleParser.hpp b/include/rfl/parsing/NamedTupleParser.hpp index 1e723cde..98828686 100644 --- a/include/rfl/parsing/NamedTupleParser.hpp +++ b/include/rfl/parsing/NamedTupleParser.hpp @@ -14,6 +14,7 @@ #include "../internal/is_array.hpp" #include "../internal/is_attribute.hpp" #include "../internal/is_basic_type.hpp" +#include "../internal/is_skip.hpp" #include "../internal/strings/replace_all.hpp" #include "../to_view.hpp" #include "AreReaderAndWriter.hpp" @@ -99,9 +100,11 @@ struct NamedTupleParser { } else { using F = std::tuple_element_t<_i, typename NamedTuple::Fields>; - _values[std::string(F::name())] = - Parser::to_schema( - _definitions); + using U = typename F::Type; + if constexpr (!internal::is_skip_v) { + _values[std::string(F::name())] = + Parser::to_schema(_definitions); + } return to_schema<_i + 1>(_definitions, _values); } }; diff --git a/include/rfl/parsing/Parser.hpp b/include/rfl/parsing/Parser.hpp index 889c1202..7d2e8ade 100644 --- a/include/rfl/parsing/Parser.hpp +++ b/include/rfl/parsing/Parser.hpp @@ -18,6 +18,7 @@ #include "Parser_rfl_array.hpp" #include "Parser_rfl_variant.hpp" #include "Parser_shared_ptr.hpp" +#include "Parser_skip.hpp" #include "Parser_string_view.hpp" #include "Parser_tagged_union.hpp" #include "Parser_tuple.hpp" diff --git a/include/rfl/parsing/Parser_skip.hpp b/include/rfl/parsing/Parser_skip.hpp new file mode 100644 index 00000000..135b0e25 --- /dev/null +++ b/include/rfl/parsing/Parser_skip.hpp @@ -0,0 +1,68 @@ +#ifndef RFL_PARSING_PARSER_SKIP_HPP_ +#define RFL_PARSING_PARSER_SKIP_HPP_ + +#include +#include + +#include "../Result.hpp" +#include "../always_false.hpp" +#include "../internal/Skip.hpp" +#include "Parser_base.hpp" +#include "schema/Type.hpp" + +namespace rfl { +namespace parsing { + +template +requires AreReaderAndWriter< + R, W, internal::Skip> +struct Parser, + ProcessorsType> { + using InputVarType = typename R::InputVarType; + using OutputVarType = typename W::OutputVarType; + + static Result> + read(const R& _r, const InputVarType& _var) noexcept { + if constexpr (_skip_deserialization) { + return internal::Skip( + std::remove_cvref_t()); + } else { + const auto to_skip = [&](auto&& _t) { + return internal::Skip( + std::move(_t)); + }; + return Parser, ProcessorsType>::read(_r, + _var) + .transform(to_skip); + } + } + + template + static void write(const W& _w, + const internal::Skip& _skip, + const P& _parent) noexcept { + if constexpr (_skip_serialization) { + using ReflectionType = std::remove_cvref_t::ReflectionType>; + Parser::write(_w, ReflectionType(), + _parent); + } else { + Parser, ProcessorsType>::write( + _w, _skip.value(), _parent); + } + } + + static schema::Type to_schema( + std::map* _definitions) { + return Parser, ProcessorsType>::to_schema( + _definitions); + } +}; + +} // namespace parsing +} // namespace rfl + +#endif diff --git a/include/rfl/parsing/is_empty.hpp b/include/rfl/parsing/is_empty.hpp index 3d9dfd4b..4c8a752c 100644 --- a/include/rfl/parsing/is_empty.hpp +++ b/include/rfl/parsing/is_empty.hpp @@ -6,6 +6,7 @@ #include "../internal/has_reflection_method_v.hpp" #include "../internal/has_reflection_type_v.hpp" #include "../internal/is_rename.hpp" +#include "../internal/is_skip.hpp" #include "is_map_like.hpp" #include "is_vector_like.hpp" @@ -17,6 +18,8 @@ static const bool is_empty(const T& _var) { using Type = std::remove_cvref_t; if constexpr (std::is_pointer_v) { return !_var || is_empty(*_var); + } else if constexpr (internal::is_skip_v) { + return Type::skip_serialization_; } else if constexpr (internal::has_reflection_type_v) { if constexpr (internal::has_reflection_method_v) { return is_empty(_var.reflection()); diff --git a/tests/json/test_skip.cpp b/tests/json/test_skip.cpp new file mode 100644 index 00000000..6ceebd46 --- /dev/null +++ b/tests/json/test_skip.cpp @@ -0,0 +1,30 @@ +#include +#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(json, test_skip) { + const auto homer = Person{.first_name = "Homer", + .last_name = "Simpson", + .town = "Springfield", + .age = 45}; + + write_and_read(homer, + R"({"firstName":"Homer","lastName":"Simpson","age":45})"); +} +} // namespace test_skip diff --git a/tests/msgpack/test_skip.cpp b/tests/msgpack/test_skip.cpp new file mode 100644 index 00000000..f864e232 --- /dev/null +++ b/tests/msgpack/test_skip.cpp @@ -0,0 +1,27 @@ +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_skip { + +using Age = rfl::Validator, rfl::Maximum<130>>; + +struct Person { + rfl::Skip town; + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name; + Age age; +}; + +TEST(msgpack, test_skip) { + const auto homer = Person{.town = "Springfield", + .first_name = "Homer", + .last_name = "Simpson", + .age = 45}; + + write_and_read(homer); +} +} // namespace test_skip