mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-06 18:56:19 +08:00
Added AnyOf and more comparisons
This commit is contained in:
+5
-4
@@ -1,8 +1,10 @@
|
||||
#ifndef RFL_RFL_HPP_
|
||||
#define RFL_RFL_HPP_
|
||||
|
||||
#include "rfl/AllOf.hpp"
|
||||
#include "rfl/AnyOf.hpp"
|
||||
#include "rfl/Box.hpp"
|
||||
#include "rfl/Constrained.hpp"
|
||||
#include "rfl/ConStr.hpp"
|
||||
#include "rfl/Field.hpp"
|
||||
#include "rfl/Flatten.hpp"
|
||||
#include "rfl/Literal.hpp"
|
||||
@@ -10,9 +12,10 @@
|
||||
#include "rfl/Ref.hpp"
|
||||
#include "rfl/TaggedUnion.hpp"
|
||||
#include "rfl/Timestamp.hpp"
|
||||
#include "rfl/Validator.hpp"
|
||||
#include "rfl/always_false.hpp"
|
||||
#include "rfl/as.hpp"
|
||||
#include "rfl/constraints.hpp"
|
||||
#include "rfl/comparisons.hpp"
|
||||
#include "rfl/default.hpp"
|
||||
#include "rfl/define_literal.hpp"
|
||||
#include "rfl/define_named_tuple.hpp"
|
||||
@@ -30,7 +33,5 @@
|
||||
#include "rfl/replace.hpp"
|
||||
#include "rfl/to_named_tuple.hpp"
|
||||
#include "rfl/visit.hpp"
|
||||
#include "rfl/ConStr.hpp"
|
||||
#include "rfl/Validator.hpp"
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
#ifndef RFL_ALLOF_HPP_
|
||||
#define RFL_ALLOF_HPP_
|
||||
|
||||
#include "rfl/Result.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
/// Requires that all of the contraints C and Cs be true.
|
||||
template <class C, class... Cs>
|
||||
struct AllOf {
|
||||
template <class T>
|
||||
static rfl::Result<T> validate(T _value) noexcept {
|
||||
return validate_impl<T, C, Cs...>(_value);
|
||||
}
|
||||
|
||||
private:
|
||||
template <class T, class Head, class... Tail>
|
||||
static rfl::Result<T> validate_impl(T _value) noexcept {
|
||||
if constexpr (sizeof...(Tail) == 0) {
|
||||
return Head::validate(_value);
|
||||
} else {
|
||||
return Head::validate(_value).and_then(validate_impl<T, Tail...>);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,49 @@
|
||||
#ifndef RFL_ANYOF_HPP_
|
||||
#define RFL_ANYOF_HPP_
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "rfl/Result.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
/// Requires that all of the contraints C and Cs be true.
|
||||
template <class C, class... Cs>
|
||||
struct AnyOf {
|
||||
template <class T>
|
||||
static rfl::Result<T> validate(const T& _value) noexcept {
|
||||
return validate_impl<T, C, Cs...>(_value, {});
|
||||
}
|
||||
|
||||
private:
|
||||
static Error make_error_message(const std::vector<Error>& _errors) {
|
||||
std::string msg =
|
||||
"Expected at least one of the following validations to pass, but none "
|
||||
"of them did:";
|
||||
for (size_t i = 0; i < _errors.size(); ++i) {
|
||||
msg += "\n" + std::to_string(i + 1) + ") " + _errors.at(i).what();
|
||||
}
|
||||
return Error(msg);
|
||||
}
|
||||
|
||||
template <class T, class Head, class... Tail>
|
||||
static rfl::Result<T> validate_impl(const T& _value,
|
||||
std::vector<Error> _errors) {
|
||||
const auto handle_err = [&](Error&& _err) {
|
||||
_errors.push_back(std::forward<Error>(_err));
|
||||
if constexpr (sizeof...(Tail) == 0) {
|
||||
return make_error_message(_errors);
|
||||
} else {
|
||||
return validate_impl<T, Tail...>(
|
||||
_value, std::forward<std::vector<Error>>(_errors));
|
||||
}
|
||||
};
|
||||
return Head::validate(_value).or_else(handle_err);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -1,35 +0,0 @@
|
||||
#ifndef RFL_CONSTRAINED_HPP_
|
||||
#define RFL_CONSTRAINED_HPP_
|
||||
|
||||
#include "rfl/Result.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
/// A constrained type is a numeric value that is constrained is some way, such
|
||||
/// as it must be greater than a specific threshold known at compile time.
|
||||
template <class T, class C>
|
||||
class Constrained {
|
||||
public:
|
||||
using ReflectionType = T;
|
||||
|
||||
Constrained(T _value) : value_(C::check(_value).value()) {}
|
||||
|
||||
~Constrained() = default;
|
||||
|
||||
/// Exposes the underlying value.
|
||||
T& value() { return value_; }
|
||||
|
||||
/// Exposes the underlying value.
|
||||
T value() const { return value_; }
|
||||
|
||||
/// Necessary for the serialization to work.
|
||||
T reflection() const { return value_; }
|
||||
|
||||
private:
|
||||
/// The underlying value.
|
||||
T value_;
|
||||
};
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
+22
-49
@@ -7,63 +7,36 @@
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
|
||||
#include "internal/HasValidation.hpp"
|
||||
#include "internal/StringLiteral.hpp"
|
||||
#include "rfl/Result.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
template <class Class, typename Type>
|
||||
concept HasValidation = requires(Class obj, Type value) {
|
||||
{ Class::validate(value) } -> std::same_as<std::optional<rfl::Error>>;
|
||||
};
|
||||
|
||||
template <typename _Type>
|
||||
concept HasDescription = requires(_Type obj) {
|
||||
{ obj.description } -> std::same_as<std::optional<rfl::Error>>;
|
||||
};
|
||||
|
||||
|
||||
template <typename _Type, class Class>
|
||||
requires HasValidation<Class, _Type>
|
||||
struct Annotate {
|
||||
template <class T, class V>
|
||||
requires internal::HasValidation<V, T>
|
||||
struct Validator {
|
||||
public:
|
||||
using ReflectionType = _Type;
|
||||
ReflectionType value;
|
||||
using ReflectionType = T;
|
||||
|
||||
template <class T,
|
||||
std::enable_if_t<std::is_convertible_v<T, _Type>, bool> = true >
|
||||
Annotate(const T& _value) : value(_value) {
|
||||
std::optional<rfl::Error> validate_check = Class::validate(value);
|
||||
if (validate_check.has_value()) {
|
||||
throw std::runtime_error(validate_check.value().what());
|
||||
}
|
||||
};
|
||||
Validator(const T& _value) : value_(V::validate(_value).value()) {}
|
||||
|
||||
template <class T,
|
||||
std::enable_if_t<std::is_convertible_v<T, _Type>, bool> = true >
|
||||
Annotate(T&& _value) : value(std::forward<T>(_value)) {
|
||||
std::optional<rfl::Error> validate_check = Class::validate(value);
|
||||
if (validate_check.has_value()) {
|
||||
throw std::runtime_error(validate_check.value().what());
|
||||
}
|
||||
}
|
||||
~Validator() = default;
|
||||
|
||||
Annotate(ReflectionType _value) : value(_value) {
|
||||
std::optional<rfl::Error> validate_check = Class::validate(value);
|
||||
if (validate_check.has_value()) {
|
||||
throw std::runtime_error(validate_check.value().what());
|
||||
}
|
||||
}
|
||||
|
||||
/// Exposes the underlying value.
|
||||
T& value() { return value_; }
|
||||
|
||||
/// Exposes the underlying value.
|
||||
T value() const { return value_; }
|
||||
|
||||
/// Necessary for the serialization to work.
|
||||
T reflection() const { return value_; }
|
||||
|
||||
private:
|
||||
/// The underlying value.
|
||||
T value_;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
std::string to_string(const T& _value) {
|
||||
std::ostringstream oss;
|
||||
oss << _value;
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
/*
|
||||
template <typename T>
|
||||
concept HasSize = requires(T obj) {
|
||||
{ obj.size() } -> std::same_as<size_t>;
|
||||
@@ -111,8 +84,8 @@ struct Pattern {
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
};
|
||||
};*/
|
||||
|
||||
} // namespace rfl
|
||||
//
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
#ifndef RFL_COMPARISONS_HPP_
|
||||
#define RFL_COMPARISONS_HPP_
|
||||
|
||||
#include "rfl/Result.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
template <double _threshold>
|
||||
struct EqualTo {
|
||||
template <class T>
|
||||
static Result<T> validate(T _value) noexcept {
|
||||
constexpr auto threshold = static_cast<T>(_threshold);
|
||||
if (_value != threshold) {
|
||||
return Error("Value expected to be equal to" + std::to_string(threshold) +
|
||||
", but got " + std::to_string(_value) + ".");
|
||||
}
|
||||
return _value;
|
||||
}
|
||||
};
|
||||
|
||||
template <double _threshold>
|
||||
struct GreaterEqual {
|
||||
template <class T>
|
||||
static Result<T> validate(T _value) noexcept {
|
||||
constexpr auto threshold = static_cast<T>(_threshold);
|
||||
if (_value < threshold) {
|
||||
return Error("Value expected to be greater than or equal to" +
|
||||
std::to_string(threshold) + ", but got " +
|
||||
std::to_string(_value) + ".");
|
||||
}
|
||||
return _value;
|
||||
}
|
||||
};
|
||||
|
||||
template <double _threshold>
|
||||
struct GreaterThan {
|
||||
template <class T>
|
||||
static Result<T> validate(T _value) noexcept {
|
||||
constexpr auto threshold = static_cast<T>(_threshold);
|
||||
if (_value <= threshold) {
|
||||
return Error("Value expected to be greater than " +
|
||||
std::to_string(threshold) + ", but got " +
|
||||
std::to_string(_value) + ".");
|
||||
}
|
||||
return _value;
|
||||
}
|
||||
};
|
||||
|
||||
template <double _threshold>
|
||||
struct LessEqual {
|
||||
template <class T>
|
||||
static Result<T> validate(T _value) noexcept {
|
||||
constexpr auto threshold = static_cast<T>(_threshold);
|
||||
if (_value > threshold) {
|
||||
return Error("Value expected to be less than or equal to" +
|
||||
std::to_string(threshold) + ", but got " +
|
||||
std::to_string(_value) + ".");
|
||||
}
|
||||
return _value;
|
||||
}
|
||||
};
|
||||
|
||||
template <double _threshold>
|
||||
struct LessThan {
|
||||
template <class T>
|
||||
static Result<T> validate(T _value) noexcept {
|
||||
constexpr auto threshold = static_cast<T>(_threshold);
|
||||
if (_value >= threshold) {
|
||||
return Error("Value expected to be less than " +
|
||||
std::to_string(threshold) + ", but got " +
|
||||
std::to_string(_value) + ".");
|
||||
}
|
||||
return _value;
|
||||
}
|
||||
};
|
||||
|
||||
template <double _threshold>
|
||||
struct NotEqualTo {
|
||||
template <class T>
|
||||
static Result<T> validate(T _value) noexcept {
|
||||
constexpr auto threshold = static_cast<T>(_threshold);
|
||||
if (_value == threshold) {
|
||||
return Error("Value expected to not be equal to" +
|
||||
std::to_string(threshold) + ", but got " +
|
||||
std::to_string(_value) + ".");
|
||||
}
|
||||
return _value;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -1,58 +0,0 @@
|
||||
#ifndef RFL_CONSTRAINTS_HPP_
|
||||
#define RFL_CONSTRAINTS_HPP_
|
||||
|
||||
#include "rfl/Result.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
template <int _threshold>
|
||||
struct greater_than {
|
||||
template <class T>
|
||||
static Result<T> check(T _value) noexcept {
|
||||
if (_value <= static_cast<T>(_threshold)) {
|
||||
return Error("Value expected to be greater than " +
|
||||
std::to_string(_threshold) + ", but got " +
|
||||
std::to_string(_value) + ".");
|
||||
}
|
||||
return _value;
|
||||
}
|
||||
};
|
||||
|
||||
template <int _threshold>
|
||||
struct less_than {
|
||||
template <class T>
|
||||
static Result<T> check(T _value) noexcept {
|
||||
if (_value >= static_cast<T>(_threshold)) {
|
||||
return Error("Value expected to be less than " +
|
||||
std::to_string(_threshold) + ", but got " +
|
||||
std::to_string(_value) + ".");
|
||||
}
|
||||
return _value;
|
||||
}
|
||||
};
|
||||
|
||||
/// Requires that all of the contraints C and Cs be true.
|
||||
template <class C, class... Cs>
|
||||
struct all_of {
|
||||
template <class T>
|
||||
static rfl::Result<T> check(T _value) noexcept {
|
||||
return check_impl<T, C, Cs...>(_value);
|
||||
}
|
||||
|
||||
private:
|
||||
template <class T, class Head, class... Tail>
|
||||
static rfl::Result<T> check_impl(T _value) noexcept {
|
||||
if constexpr (sizeof...(Tail) == 0) {
|
||||
return Head::check(_value);
|
||||
} else {
|
||||
return Head::check(_value).and_then(check_impl<T, Tail...>);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/// Requires that any of the contraints C and Cs be true.
|
||||
// ...
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,19 @@
|
||||
#ifndef RFL_INTERNAL_HASVALIDATION_HPP_
|
||||
#define RFL_INTERNAL_HASVALIDATION_HPP_
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include "rfl/Result.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
template <class Class, typename T>
|
||||
concept HasValidation = requires(Class obj, T value) {
|
||||
{ Class::validate(value) } -> std::same_as<rfl::Result<T>>;
|
||||
};
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
+13
-12
@@ -7,20 +7,21 @@
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_all_of() {
|
||||
std::cout << "test_all_of" << std::endl;
|
||||
std::cout << "test_all_of" << std::endl;
|
||||
|
||||
using Age = rfl::Constrained<
|
||||
int, rfl::all_of<rfl::greater_than<0>, rfl::less_than<130>>>;
|
||||
using Age =
|
||||
rfl::Validator<unsigned int,
|
||||
rfl::AllOf<rfl::GreaterThan<0.>, rfl::LessThan<130.>>>;
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name;
|
||||
rfl::Field<"age", Age> age;
|
||||
};
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name;
|
||||
rfl::Field<"age", Age> age;
|
||||
};
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .last_name = "Simpson", .age = 45};
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .last_name = "Simpson", .age = 45};
|
||||
|
||||
write_and_read(homer,
|
||||
R"({"firstName":"Homer","lastName":"Simpson","age":45})");
|
||||
write_and_read(homer,
|
||||
R"({"firstName":"Homer","lastName":"Simpson","age":45})");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_any_of() {
|
||||
std::cout << "test_any_of" << std::endl;
|
||||
|
||||
using Age = rfl::Validator<
|
||||
unsigned int,
|
||||
rfl::AnyOf<rfl::AllOf<rfl::GreaterThan<0.>, rfl::LessThan<10.>>,
|
||||
rfl::AllOf<rfl::GreaterThan<40.>, rfl::LessThan<130.>>>>;
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name;
|
||||
rfl::Field<"age", Age> age;
|
||||
};
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .last_name = "Simpson", .age = 45};
|
||||
|
||||
write_and_read(homer,
|
||||
R"({"firstName":"Homer","lastName":"Simpson","age":45})");
|
||||
}
|
||||
+46
-43
@@ -1,4 +1,5 @@
|
||||
#include "test_all_of.hpp"
|
||||
#include "test_any_of.hpp"
|
||||
#include "test_array.hpp"
|
||||
#include "test_as.hpp"
|
||||
#include "test_as_flatten.hpp"
|
||||
@@ -32,57 +33,59 @@
|
||||
#include "test_timestamp.hpp"
|
||||
#include "test_unique_ptr.hpp"
|
||||
#include "test_unique_ptr2.hpp"
|
||||
#include "test_constr.hpp"
|
||||
//#include "test_constr.hpp"
|
||||
#include "test_unordered_map.hpp"
|
||||
#include "test_unordered_set.hpp"
|
||||
#include "test_unordered_multimap.hpp"
|
||||
#include "test_unordered_multiset.hpp"
|
||||
#include "test_unordered_set.hpp"
|
||||
#include "test_variant.hpp"
|
||||
|
||||
int main() {
|
||||
test_readme_example();
|
||||
test_default_values();
|
||||
test_optional_fields();
|
||||
test_unique_ptr();
|
||||
test_unique_ptr2();
|
||||
test_literal();
|
||||
test_variant();
|
||||
test_tagged_union();
|
||||
test_field_variant();
|
||||
test_ref();
|
||||
test_box();
|
||||
test_array();
|
||||
test_timestamp();
|
||||
test_flatten();
|
||||
test_deque();
|
||||
test_forward_list();
|
||||
test_list();
|
||||
test_map();
|
||||
test_unordered_map();
|
||||
test_set();
|
||||
test_unordered_set();
|
||||
test_multimap();
|
||||
test_unordered_multimap();
|
||||
test_multiset();
|
||||
test_unordered_multiset();
|
||||
test_string_map();
|
||||
test_string_unordered_map();
|
||||
test_error_messages();
|
||||
test_all_of();
|
||||
test_readme_example();
|
||||
test_default_values();
|
||||
test_optional_fields();
|
||||
test_unique_ptr();
|
||||
test_unique_ptr2();
|
||||
test_literal();
|
||||
test_variant();
|
||||
test_tagged_union();
|
||||
test_field_variant();
|
||||
test_ref();
|
||||
test_box();
|
||||
test_array();
|
||||
test_timestamp();
|
||||
test_flatten();
|
||||
test_deque();
|
||||
test_forward_list();
|
||||
test_list();
|
||||
test_map();
|
||||
test_unordered_map();
|
||||
test_set();
|
||||
test_unordered_set();
|
||||
test_multimap();
|
||||
test_unordered_multimap();
|
||||
test_multiset();
|
||||
test_unordered_multiset();
|
||||
test_string_map();
|
||||
test_string_unordered_map();
|
||||
test_error_messages();
|
||||
|
||||
test_custom_class1();
|
||||
test_custom_class2();
|
||||
test_custom_class3();
|
||||
test_custom_class4();
|
||||
test_custom_class1();
|
||||
test_custom_class2();
|
||||
test_custom_class3();
|
||||
test_custom_class4();
|
||||
|
||||
test_replace();
|
||||
test_replace_flatten();
|
||||
test_replace_with_other_struct();
|
||||
test_as();
|
||||
test_as_flatten();
|
||||
test_constr();
|
||||
test_replace();
|
||||
test_replace_flatten();
|
||||
test_replace_with_other_struct();
|
||||
test_as();
|
||||
test_as_flatten();
|
||||
|
||||
test_custom_constructor();
|
||||
test_custom_constructor();
|
||||
|
||||
return 0;
|
||||
test_all_of();
|
||||
test_any_of();
|
||||
// test_constr();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user