Added rfl::Skip; resolves #99 (#103)

This commit is contained in:
liuzicheng1987
2024-05-21 18:13:42 +02:00
committed by GitHub
parent 579ec6e919
commit adf8463527
12 changed files with 389 additions and 5 deletions
+4 -2
View File
@@ -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
+57
View File
@@ -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<std::string> 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<std::string> 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<std::string> 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";
```
+1
View File
@@ -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"
+20
View File
@@ -0,0 +1,20 @@
#ifndef RFL_SKIP_HPP_
#define RFL_SKIP_HPP_
#include "internal/Skip.hpp"
namespace rfl {
template <class T>
using Skip = internal::Skip<T, true, true>;
template <class T>
using SkipSerialization = internal::Skip<T, true, false>;
template <class T>
using SkipDeserialization = internal::Skip<T, false, true>;
} // namespace rfl
#endif
+146
View File
@@ -0,0 +1,146 @@
#ifndef RFL_INTERNAL_SKIP_HPP_
#define RFL_INTERNAL_SKIP_HPP_
#include <optional>
namespace rfl::internal {
template <class T, bool _skip_serialization, bool _skip_deserialization>
class Skip {
private:
using SelfType = Skip<T, _skip_serialization, _skip_deserialization>;
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<T>;
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 <class U, bool _skip_s, bool _skip_d>
Skip(const Skip<U, _skip_s, _skip_d>& _other) : value_(_other.get()) {}
template <class U, bool _skip_s, bool _skip_d>
Skip(Skip<U, _skip_s, _skip_d>&& _other) : value_(_other.get()) {}
template <class U, typename std::enable_if<std::is_convertible_v<U, Type>,
bool>::type = true>
Skip(const U& _value) : value_(_value) {}
template <class U, typename std::enable_if<std::is_convertible_v<U, Type>,
bool>::type = true>
Skip(U&& _value) noexcept : value_(std::forward<U>(_value)) {}
template <class U, bool _skip_s, bool _skip_d,
typename std::enable_if<std::is_convertible_v<U, Type>,
bool>::type = true>
Skip(const Skip<U, _skip_s, _skip_d>& _skip) : value_(_skip.value()) {}
/// Assigns the underlying object to its default value.
template <class U = Type,
typename std::enable_if<std::is_default_constructible_v<U>,
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 <class U, typename std::enable_if<std::is_convertible_v<U, Type>,
bool>::type = true>
auto& operator=(const U& _value) {
value_ = _value;
return *this;
}
/// Assigns the underlying object to its default value.
template <class U = Type,
typename std::enable_if<std::is_default_constructible_v<U>,
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 <class U, bool _skip_s, bool _skip_d>
auto& operator=(const Skip<U, _skip_s, _skip_d>& _skip) {
value_ = _skip.get();
return *this;
}
/// Assigns the underlying object.
template <class U, bool _skip_s, bool _skip_d>
auto& operator=(Skip<U, _skip_s, _skip_d>&& _skip) {
value_ = std::forward<T>(_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
+26
View File
@@ -0,0 +1,26 @@
#ifndef RFL_INTERNAL_ISSKIP_HPP_
#define RFL_INTERNAL_ISSKIP_HPP_
#include <type_traits>
#include "Skip.hpp"
namespace rfl::internal {
template <class T>
class is_skip;
template <class T>
class is_skip : public std::false_type {};
template <class T, bool _skip_serialization, bool _skip_deserialization>
class is_skip<Skip<T, _skip_serialization, _skip_deserialization>>
: public std::true_type {};
template <class T>
constexpr bool is_skip_v =
is_skip<std::remove_cvref_t<std::remove_pointer_t<T>>>::value;
} // namespace rfl::internal
#endif
+6 -3
View File
@@ -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<FieldTypes...>::Fields>;
_values[std::string(F::name())] =
Parser<R, W, typename F::Type, ProcessorsType>::to_schema(
_definitions);
using U = typename F::Type;
if constexpr (!internal::is_skip_v<U>) {
_values[std::string(F::name())] =
Parser<R, W, U, ProcessorsType>::to_schema(_definitions);
}
return to_schema<_i + 1>(_definitions, _values);
}
};
+1
View File
@@ -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"
+68
View File
@@ -0,0 +1,68 @@
#ifndef RFL_PARSING_PARSER_SKIP_HPP_
#define RFL_PARSING_PARSER_SKIP_HPP_
#include <map>
#include <type_traits>
#include "../Result.hpp"
#include "../always_false.hpp"
#include "../internal/Skip.hpp"
#include "Parser_base.hpp"
#include "schema/Type.hpp"
namespace rfl {
namespace parsing {
template <class R, class W, class T, bool _skip_serialization,
bool _skip_deserialization, class ProcessorsType>
requires AreReaderAndWriter<
R, W, internal::Skip<T, _skip_serialization, _skip_deserialization>>
struct Parser<R, W,
internal::Skip<T, _skip_serialization, _skip_deserialization>,
ProcessorsType> {
using InputVarType = typename R::InputVarType;
using OutputVarType = typename W::OutputVarType;
static Result<internal::Skip<T, _skip_serialization, _skip_deserialization>>
read(const R& _r, const InputVarType& _var) noexcept {
if constexpr (_skip_deserialization) {
return internal::Skip<T, _skip_serialization, _skip_deserialization>(
std::remove_cvref_t<T>());
} else {
const auto to_skip = [&](auto&& _t) {
return internal::Skip<T, _skip_serialization, _skip_deserialization>(
std::move(_t));
};
return Parser<R, W, std::remove_cvref_t<T>, ProcessorsType>::read(_r,
_var)
.transform(to_skip);
}
}
template <class P>
static void write(const W& _w,
const internal::Skip<T, _skip_serialization,
_skip_deserialization>& _skip,
const P& _parent) noexcept {
if constexpr (_skip_serialization) {
using ReflectionType = std::remove_cvref_t<typename internal::Skip<
T, _skip_serialization, _skip_deserialization>::ReflectionType>;
Parser<R, W, ReflectionType, ProcessorsType>::write(_w, ReflectionType(),
_parent);
} else {
Parser<R, W, std::remove_cvref_t<T>, ProcessorsType>::write(
_w, _skip.value(), _parent);
}
}
static schema::Type to_schema(
std::map<std::string, schema::Type>* _definitions) {
return Parser<R, W, std::remove_cvref_t<T>, ProcessorsType>::to_schema(
_definitions);
}
};
} // namespace parsing
} // namespace rfl
#endif
+3
View File
@@ -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<T>;
if constexpr (std::is_pointer_v<Type>) {
return !_var || is_empty(*_var);
} else if constexpr (internal::is_skip_v<Type>) {
return Type::skip_serialization_;
} else if constexpr (internal::has_reflection_type_v<Type>) {
if constexpr (internal::has_reflection_method_v<Type>) {
return is_empty(_var.reflection());
+30
View File
@@ -0,0 +1,30 @@
#include <iostream>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <source_location>
#include <string>
#include <vector>
#include "write_and_read.hpp"
namespace test_skip {
using Age = rfl::Validator<unsigned int, rfl::Minimum<0>, rfl::Maximum<130>>;
struct Person {
rfl::Rename<"firstName", std::string> first_name;
rfl::Rename<"lastName", std::string> last_name;
rfl::Skip<std::string> 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
+27
View File
@@ -0,0 +1,27 @@
#include <iostream>
#include <source_location>
#include <string>
#include <vector>
#include "write_and_read.hpp"
namespace test_skip {
using Age = rfl::Validator<unsigned int, rfl::Minimum<0>, rfl::Maximum<130>>;
struct Person {
rfl::Skip<std::string> 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