diff --git a/include/rfl.hpp b/include/rfl.hpp index a3ee0210..2e121aa7 100644 --- a/include/rfl.hpp +++ b/include/rfl.hpp @@ -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 diff --git a/include/rfl/AllOf.hpp b/include/rfl/AllOf.hpp new file mode 100644 index 00000000..36c0db25 --- /dev/null +++ b/include/rfl/AllOf.hpp @@ -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 +struct AllOf { + template + static rfl::Result validate(T _value) noexcept { + return validate_impl(_value); + } + + private: + template + static rfl::Result validate_impl(T _value) noexcept { + if constexpr (sizeof...(Tail) == 0) { + return Head::validate(_value); + } else { + return Head::validate(_value).and_then(validate_impl); + } + } +}; + +} // namespace rfl + +#endif diff --git a/include/rfl/AnyOf.hpp b/include/rfl/AnyOf.hpp new file mode 100644 index 00000000..90d83f6c --- /dev/null +++ b/include/rfl/AnyOf.hpp @@ -0,0 +1,49 @@ +#ifndef RFL_ANYOF_HPP_ +#define RFL_ANYOF_HPP_ + +#include +#include +#include + +#include "rfl/Result.hpp" + +namespace rfl { + +/// Requires that all of the contraints C and Cs be true. +template +struct AnyOf { + template + static rfl::Result validate(const T& _value) noexcept { + return validate_impl(_value, {}); + } + + private: + static Error make_error_message(const std::vector& _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 + static rfl::Result validate_impl(const T& _value, + std::vector _errors) { + const auto handle_err = [&](Error&& _err) { + _errors.push_back(std::forward(_err)); + if constexpr (sizeof...(Tail) == 0) { + return make_error_message(_errors); + } else { + return validate_impl( + _value, std::forward>(_errors)); + } + }; + return Head::validate(_value).or_else(handle_err); + } +}; + +} // namespace rfl + +#endif diff --git a/include/rfl/Constrained.hpp b/include/rfl/Constrained.hpp deleted file mode 100644 index de2c1638..00000000 --- a/include/rfl/Constrained.hpp +++ /dev/null @@ -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 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 diff --git a/include/rfl/Validator.hpp b/include/rfl/Validator.hpp index 0a656b0e..7c9aebec 100644 --- a/include/rfl/Validator.hpp +++ b/include/rfl/Validator.hpp @@ -7,63 +7,36 @@ #include #include +#include "internal/HasValidation.hpp" #include "internal/StringLiteral.hpp" #include "rfl/Result.hpp" namespace rfl { -template -concept HasValidation = requires(Class obj, Type value) { - { Class::validate(value) } -> std::same_as>; -}; - -template -concept HasDescription = requires(_Type obj) { - { obj.description } -> std::same_as>; -}; - - -template -requires HasValidation -struct Annotate { +template +requires internal::HasValidation +struct Validator { public: - using ReflectionType = _Type; - ReflectionType value; + using ReflectionType = T; - template , bool> = true > - Annotate(const T& _value) : value(_value) { - std::optional 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 , bool> = true > - Annotate(T&& _value) : value(std::forward(_value)) { - std::optional 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 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 -std::string to_string(const T& _value) { - std::ostringstream oss; - oss << _value; - return oss.str(); -} - +/* template concept HasSize = requires(T obj) { { obj.size() } -> std::same_as; @@ -111,8 +84,8 @@ struct Pattern { } return std::nullopt; } -}; +};*/ } // namespace rfl -// + #endif diff --git a/include/rfl/comparisons.hpp b/include/rfl/comparisons.hpp new file mode 100644 index 00000000..b40daa1d --- /dev/null +++ b/include/rfl/comparisons.hpp @@ -0,0 +1,93 @@ +#ifndef RFL_COMPARISONS_HPP_ +#define RFL_COMPARISONS_HPP_ + +#include "rfl/Result.hpp" + +namespace rfl { + +template +struct EqualTo { + template + static Result validate(T _value) noexcept { + constexpr auto threshold = static_cast(_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 +struct GreaterEqual { + template + static Result validate(T _value) noexcept { + constexpr auto threshold = static_cast(_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 +struct GreaterThan { + template + static Result validate(T _value) noexcept { + constexpr auto threshold = static_cast(_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 +struct LessEqual { + template + static Result validate(T _value) noexcept { + constexpr auto threshold = static_cast(_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 +struct LessThan { + template + static Result validate(T _value) noexcept { + constexpr auto threshold = static_cast(_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 +struct NotEqualTo { + template + static Result validate(T _value) noexcept { + constexpr auto threshold = static_cast(_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 diff --git a/include/rfl/constraints.hpp b/include/rfl/constraints.hpp deleted file mode 100644 index 80f3815e..00000000 --- a/include/rfl/constraints.hpp +++ /dev/null @@ -1,58 +0,0 @@ -#ifndef RFL_CONSTRAINTS_HPP_ -#define RFL_CONSTRAINTS_HPP_ - -#include "rfl/Result.hpp" - -namespace rfl { - -template -struct greater_than { - template - static Result check(T _value) noexcept { - if (_value <= static_cast(_threshold)) { - return Error("Value expected to be greater than " + - std::to_string(_threshold) + ", but got " + - std::to_string(_value) + "."); - } - return _value; - } -}; - -template -struct less_than { - template - static Result check(T _value) noexcept { - if (_value >= static_cast(_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 -struct all_of { - template - static rfl::Result check(T _value) noexcept { - return check_impl(_value); - } - - private: - template - static rfl::Result check_impl(T _value) noexcept { - if constexpr (sizeof...(Tail) == 0) { - return Head::check(_value); - } else { - return Head::check(_value).and_then(check_impl); - } - } -}; - -/// Requires that any of the contraints C and Cs be true. -// ... - -} // namespace rfl - -#endif diff --git a/include/rfl/internal/HasValidation.hpp b/include/rfl/internal/HasValidation.hpp new file mode 100644 index 00000000..debdf587 --- /dev/null +++ b/include/rfl/internal/HasValidation.hpp @@ -0,0 +1,19 @@ +#ifndef RFL_INTERNAL_HASVALIDATION_HPP_ +#define RFL_INTERNAL_HASVALIDATION_HPP_ + +#include + +#include "rfl/Result.hpp" + +namespace rfl { +namespace internal { + +template +concept HasValidation = requires(Class obj, T value) { + { Class::validate(value) } -> std::same_as>; +}; + +} // namespace internal +} // namespace rfl + +#endif diff --git a/tests/test_all_of.hpp b/tests/test_all_of.hpp index b6f0d8bc..cc5edeea 100644 --- a/tests/test_all_of.hpp +++ b/tests/test_all_of.hpp @@ -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::less_than<130>>>; + using Age = + rfl::Validator, 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})"); } diff --git a/tests/test_any_of.hpp b/tests/test_any_of.hpp new file mode 100644 index 00000000..893aad4b --- /dev/null +++ b/tests/test_any_of.hpp @@ -0,0 +1,28 @@ +#include +#include +#include +#include +#include + +#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::LessThan<10.>>, + rfl::AllOf, 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})"); +} diff --git a/tests/tests.cpp b/tests/tests.cpp index 904eda9c..9ebbd979 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -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; }