From ba997272c7e7ae1905be6b7491bdf9f04924626f Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Thu, 2 Nov 2023 20:02:34 +0100 Subject: [PATCH] Added OneOf --- include/rfl.hpp | 1 + include/rfl/OneOf.hpp | 63 +++++++++++++++++++++++++++++++++++++++++++ tests/test_one_of.hpp | 28 +++++++++++++++++++ tests/tests.cpp | 2 ++ 4 files changed, 94 insertions(+) create mode 100644 include/rfl/OneOf.hpp create mode 100644 tests/test_one_of.hpp diff --git a/include/rfl.hpp b/include/rfl.hpp index 2e121aa7..e4e98e69 100644 --- a/include/rfl.hpp +++ b/include/rfl.hpp @@ -9,6 +9,7 @@ #include "rfl/Flatten.hpp" #include "rfl/Literal.hpp" #include "rfl/NamedTuple.hpp" +#include "rfl/OneOf.hpp" #include "rfl/Ref.hpp" #include "rfl/TaggedUnion.hpp" #include "rfl/Timestamp.hpp" diff --git a/include/rfl/OneOf.hpp b/include/rfl/OneOf.hpp new file mode 100644 index 00000000..9bfdcd82 --- /dev/null +++ b/include/rfl/OneOf.hpp @@ -0,0 +1,63 @@ +#ifndef RFL_ONEOF_HPP_ +#define RFL_ONEOF_HPP_ + +#include +#include +#include + +#include "rfl/Result.hpp" + +namespace rfl { + +/// Requires that all of the contraints C and Cs be true. +template +struct OneOf { + 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 exactly 1 out of " + + std::to_string(sizeof...(Cs) + 1) + + " validations to pass, but " + + std::to_string(sizeof...(Cs) + 1 - _errors.size()) + + " of them did. The following errors were generated: "; + 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 push_back = [&](Error&& _err) -> rfl::Result { + _errors.emplace_back(std::forward(_err)); + return _err; + }; + + const auto next_validation = [&](rfl::Result&& _r) -> rfl::Result { + _r.or_else(push_back); + + if constexpr (sizeof...(Tail) == 0) { + if (_errors.size() == sizeof...(Cs)) { + return _value; + } + return make_error_message(_errors); + } else { + return validate_impl( + _value, std::forward>(_errors)); + } + }; + + return Head::validate(_value) + .and_then(next_validation) + .or_else(next_validation); + } +}; + +} // namespace rfl + +#endif diff --git a/tests/test_one_of.hpp b/tests/test_one_of.hpp new file mode 100644 index 00000000..c57564a3 --- /dev/null +++ b/tests/test_one_of.hpp @@ -0,0 +1,28 @@ +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +void test_one_of() { + std::cout << "test_one_of" << std::endl; + + using Age = rfl::Validator< + unsigned int, + rfl::OneOf, rfl::Maximum<10.>>, + rfl::AllOf, rfl::Maximum<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 9ebbd979..7914c2b1 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -34,6 +34,7 @@ #include "test_unique_ptr.hpp" #include "test_unique_ptr2.hpp" //#include "test_constr.hpp" +#include "test_one_of.hpp" #include "test_unordered_map.hpp" #include "test_unordered_multimap.hpp" #include "test_unordered_multiset.hpp" @@ -85,6 +86,7 @@ int main() { test_all_of(); test_any_of(); + test_one_of(); // test_constr(); return 0;