mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-04 01:06:19 +08:00
Fixed the validator so it can be supported as an anonymous field
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
#include <regex>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
#include "internal/HasValidation.hpp"
|
||||
#include "internal/StringLiteral.hpp"
|
||||
@@ -34,8 +35,15 @@ struct Validator {
|
||||
|
||||
Validator(const Validator<T, V>& _other) = default;
|
||||
|
||||
Validator(T&& _value) : value_(V::validate(_value).value()) {}
|
||||
|
||||
Validator(const T& _value) : value_(V::validate(_value).value()) {}
|
||||
|
||||
template <class U, typename std::enable_if<std::is_convertible_v<U, T>,
|
||||
bool>::type = true>
|
||||
Validator(U&& _value)
|
||||
: value_(V::validate(T(std::forward<U>(_value))).value()) {}
|
||||
|
||||
template <class U, typename std::enable_if<std::is_convertible_v<U, T>,
|
||||
bool>::type = true>
|
||||
Validator(const U& _value) : value_(V::validate(T(_value)).value()) {}
|
||||
@@ -50,7 +58,7 @@ struct Validator {
|
||||
|
||||
/// Assigns the underlying object.
|
||||
auto& operator=(T&& _value) {
|
||||
value_ = V::validate(_value).value();
|
||||
value_ = V::validate(std::forward<T>(_value)).value();
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -58,13 +66,13 @@ struct Validator {
|
||||
Validator<T, V>& operator=(const Validator<T, V>& _other) = default;
|
||||
|
||||
/// Assigns the underlying object.
|
||||
Validator<T, V>& operator=(Validator<T, V>&& _other) = default;
|
||||
Validator<T, V>& operator=(Validator<T, V>&& _other) noexcept = default;
|
||||
|
||||
/// Assigns the underlying object.
|
||||
template <class U, typename std::enable_if<std::is_convertible_v<U, T>,
|
||||
bool>::type = true>
|
||||
auto& operator=(U&& _value) {
|
||||
value_ = V::validate(T(std::forward<T>(_value))).value();
|
||||
auto& operator=(U&& _value) noexcept {
|
||||
value_ = V::validate(T(std::forward<U>(_value))).value();
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
@@ -16,8 +16,8 @@ void test_anonymous_fields() {
|
||||
std::string first_name;
|
||||
std::string last_name;
|
||||
rfl::Timestamp<"%Y-%m-%d"> birthday;
|
||||
unsigned int age;
|
||||
std::string email;
|
||||
Age age;
|
||||
rfl::Email email;
|
||||
std::vector<Person> children;
|
||||
};
|
||||
|
||||
@@ -25,8 +25,7 @@ void test_anonymous_fields() {
|
||||
.last_name = "Simpson",
|
||||
.birthday = "1987-04-19",
|
||||
.age = 10,
|
||||
.email = "bart@simpson.com",
|
||||
.children = std::vector<Person>()};
|
||||
.email = "bart@simpson.com"};
|
||||
|
||||
const auto lisa = Person{.first_name = "Lisa",
|
||||
.last_name = "Simpson",
|
||||
|
||||
Reference in New Issue
Block a user