diff --git a/include/rfl.hpp b/include/rfl.hpp index c83a76c5..755270e6 100644 --- a/include/rfl.hpp +++ b/include/rfl.hpp @@ -12,6 +12,7 @@ #include "rfl/Pattern.hpp" #include "rfl/PatternValidator.hpp" #include "rfl/Ref.hpp" +#include "rfl/Size.hpp" #include "rfl/TaggedUnion.hpp" #include "rfl/Timestamp.hpp" #include "rfl/Validator.hpp" diff --git a/include/rfl/ConStr.hpp b/include/rfl/ConStr.hpp deleted file mode 100644 index 030fd824..00000000 --- a/include/rfl/ConStr.hpp +++ /dev/null @@ -1,92 +0,0 @@ -#ifndef RFL_UUID_HPP_ -#define RFL_UUID_HPP_ - -#include -#include -#include -#include -#include - -#include "internal/StringLiteral.hpp" -#include "rfl/Literal.hpp" -#include "rfl/Result.hpp" - -namespace rfl { - -template -class ConStr { - - constexpr static const internal::StringLiteral pattern_ = _pattern; - -public: - using ReflectionType = std::string; - ConStr(const char *_str) { - auto unvalidated_str = std::string(_str); - if constexpr (pattern_ != internal::StringLiteral{""}) { - std::regex regex_pattern(pattern_.str()); - if (!std::regex_match(unvalidated_str, regex_pattern)) { - throw std::runtime_error("String '" + unvalidated_str + - "' did not match format " + pattern_.str() + - " format"); - } - } - if constexpr (_min >= 0) { - if (unvalidated_str.length() < _min) { - throw std::runtime_error("String '" + unvalidated_str + - "' is shorter than min length" + - std::to_string(_min)); - } - } - if constexpr (_max >= 0) { - if (unvalidated_str.length() > _max) { - throw std::runtime_error("String '" + unvalidated_str + - "' is longer than max length" + - std::to_string(_max)); - } - } - value_ = _str; - } - - ConStr(const std::string &_str) : ConStr(_str.c_str()){}; - ~ConStr() = default; - - std::string value_; - - ReflectionType reflection() const { return value_; } - - /// Returns a result containing the ConStr when successful or an Error - /// otherwise. - static Result from_string(const char *_str) noexcept { - try { - return ConStr(_str); - } catch (std::exception &e) { - return Error(e.what()); - } - } - - /// Returns a result containing the ConStr when successful or an Error - /// otherwise. - static Result from_string(const std::string &_str) { - return from_string(_str.c_str()); - } -}; -/// UUID -using UUIDv1 = ConStr< - R"(^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-1[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$)">; -using UUIDv2 = ConStr< - R"(^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-2[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$)">; -using UUIDv3 = ConStr< - R"(^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-3[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$)">; -using UUIDv4 = ConStr< - R"(^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$)">; - -// This pattern will match valid Base64 encoded strings with or without padding. -using Base64Encoded = ConStr; - -/// email -using Email = ConStr; - -} // namespace rfl - -#endif diff --git a/include/rfl/Size.hpp b/include/rfl/Size.hpp new file mode 100644 index 00000000..72bb6cb7 --- /dev/null +++ b/include/rfl/Size.hpp @@ -0,0 +1,22 @@ +#ifndef RFL_SIZE_HPP_ +#define RFL_SIZE_HPP_ + +#include "rfl/Result.hpp" + +namespace rfl { + +template +struct Size { + template + static rfl::Result validate(const T& _t) { + const auto to_t = [&](const auto& _v) { return _t; }; + const auto embellish_error = [](const auto& _err) { + return Error("Size validation failed: " + _err.what()); + }; + return V::validate(_t.size()).transform(to_t).or_else(embellish_error); + } +}; + +} // namespace rfl + +#endif diff --git a/include/rfl/Validator.hpp b/include/rfl/Validator.hpp index d778bf9e..744ab511 100644 --- a/include/rfl/Validator.hpp +++ b/include/rfl/Validator.hpp @@ -19,6 +19,8 @@ struct Validator { public: using ReflectionType = T; + Validator() : value_(V::validate(T()).value()) {} + Validator(const T& _value) : value_(V::validate(_value).value()) {} template , @@ -45,55 +47,6 @@ struct Validator { /// The underlying value. T value_; }; -/* -template -concept HasSize = requires(T obj) { - { obj.size() } -> std::same_as; -}; - -template -concept HasIntLength = HasSize || std::is_integral_v; - -template -struct Length { - static constexpr const auto min_length = _min_length; - static constexpr const auto max_length = _max_length; - - template - static std::optional validate(T _value) { - uint16_t size; - if constexpr (HasSize) { - size = _value.size(); - } else if (std::is_integral_v) { - size = _value; - } - if (size < _min_length) { - return rfl::Error("'" + to_string(_value) + - "' is less than the min length requirement of '" + - std::to_string(_min_length) + "'"); - } - if (size > _max_length) { - return rfl::Error("'" + to_string(_value) + - "' exceeds max length requirement of '" + - std::to_string(_max_length) + "'"); - } - return std::nullopt; - } -}; - -template -struct Pattern { - static constexpr const auto pattern = _pattern; - - static std::optional validate(std::string _value) { - std::regex regex_pattern(_pattern.str()); - if (!std::regex_match(_value, regex_pattern)) { - return rfl::Error("String '" + _value + "' did not match format '" + - _pattern.str() + "'"); - } - return std::nullopt; - } -};*/ } // namespace rfl diff --git a/include/rfl/patterns.hpp b/include/rfl/patterns.hpp index ad463022..3dfccef2 100644 --- a/include/rfl/patterns.hpp +++ b/include/rfl/patterns.hpp @@ -6,10 +6,11 @@ namespace rfl { -// This pattern will match valid Base64 encoded strings with or without padding. +/// This pattern will match valid Base64 encoded strings with or without +/// padding. using Base64Encoded = Pattern; -/// email +/// This pattern will match email addresses. using Email = Pattern; diff --git a/tests/test_size.hpp b/tests/test_size.hpp new file mode 100644 index 00000000..c9cd2ca6 --- /dev/null +++ b/tests/test_size.hpp @@ -0,0 +1,51 @@ +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +// Makes sure that the example in the README works as expected. +// It would be embarrassing if it didn't... +void test_size() { + std::cout << "test_size" << std::endl; + + struct Person { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", std::string> last_name; + rfl::Field<"birthday", rfl::Timestamp<"%Y-%m-%d">> birthday; + rfl::Field<"children", + rfl::Validator< + std::vector, + rfl::Size, rfl::EqualTo<3.>>>>> + children; + }; + + const auto bart = Person{.first_name = "Bart", + .last_name = "Simpson", + .birthday = "1987-04-19", + .children = std::vector()}; + + const auto lisa = Person{ + .first_name = "Lisa", + .last_name = "Simpson", + .birthday = "1987-04-19", + .children = rfl::default_value // same as std::vector() + }; + + // Returns a deep copy of the original object, + // replacing first_name. + const auto maggie = + rfl::replace(lisa, rfl::make_field<"firstName">(std::string("Maggie"))); + + const auto homer = + Person{.first_name = "Homer", + .last_name = "Simpson", + .birthday = "1987-04-19", + .children = std::vector({bart, lisa, maggie})}; + + write_and_read( + homer, + R"({"firstName":"Homer","lastName":"Simpson","birthday":"1987-04-19","children":[{"firstName":"Bart","lastName":"Simpson","birthday":"1987-04-19","children":[]},{"firstName":"Lisa","lastName":"Simpson","birthday":"1987-04-19","children":[]},{"firstName":"Maggie","lastName":"Simpson","birthday":"1987-04-19","children":[]}]})"); +} diff --git a/tests/tests.cpp b/tests/tests.cpp index 9164901e..f11c094c 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -11,6 +11,7 @@ #include "test_custom_constructor.hpp" #include "test_default_values.hpp" #include "test_deque.hpp" +#include "test_email.hpp" #include "test_error_messages.hpp" #include "test_field_variant.hpp" #include "test_flatten.hpp" @@ -20,6 +21,7 @@ #include "test_map.hpp" #include "test_multimap.hpp" #include "test_multiset.hpp" +#include "test_one_of.hpp" #include "test_optional_fields.hpp" #include "test_readme_example.hpp" #include "test_ref.hpp" @@ -27,15 +29,13 @@ #include "test_replace_flatten.hpp" #include "test_replace_with_other_struct.hpp" #include "test_set.hpp" +#include "test_size.hpp" #include "test_string_map.hpp" #include "test_string_unordered_map.hpp" #include "test_tagged_union.hpp" #include "test_timestamp.hpp" #include "test_unique_ptr.hpp" #include "test_unique_ptr2.hpp" -//#include "test_constr.hpp" -#include "test_email.hpp" -#include "test_one_of.hpp" #include "test_unordered_map.hpp" #include "test_unordered_multimap.hpp" #include "test_unordered_multiset.hpp" @@ -89,6 +89,7 @@ int main() { test_any_of(); test_one_of(); test_email(); + test_size(); return 0; }