From 1f93c243ca820bae633c0d2c98233e00fd2dc316 Mon Sep 17 00:00:00 2001 From: Jeremy Perez Date: Fri, 6 Oct 2023 21:15:11 +0200 Subject: [PATCH 1/9] first draft for UUID implementation. --- include/rfl/UUID.hpp | 62 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 include/rfl/UUID.hpp diff --git a/include/rfl/UUID.hpp b/include/rfl/UUID.hpp new file mode 100644 index 00000000..1b4c2868 --- /dev/null +++ b/include/rfl/UUID.hpp @@ -0,0 +1,62 @@ +#ifndef RFL_TIMESTAMP_HPP_ +#define RFL_TIMESTAMP_HPP_ + +#include +#include +#include +#include +#include + +#include "internal/StringLiteral.hpp" +#include "rfl/Literal.hpp" +#include "rfl/Result.hpp" + + +namespace rfl { + + +template +class UUID { + using ReflectionType = std::string; + + constexpr static const internal::StringLiteral version_ = _version; + static constexpr const char* version_pattern = [] { + if constexpr (version_ == "1") { + return "^[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}$"; + } else if constexpr (version_ == "2") { + return "^[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}$"; + } else if constexpr (version_ == "3") { + return "^[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}$"; + } + else if constexpr (version_ == "3") { + return "^[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}$"; + } else { + static_assert("UUID Version must be one the following: 1, 2, 3, 4."); + } + }(); + + public: + UUID(const char* _str) { + std::regex uuid_regex(version_pattern); + if (!std::regex_match(uuid_regex, std::string(_str))) { + throw std::runtime_error( + "String '" + std::string(_str) + "' did not match format the UUIDv" + std::string(version_) + " format"); + } + uuid = _str; + } + + UUID(const std::string& _str) : UUID(_str.c_str()) {}; + + ReflectionType reflection() const { + return std::string(uuid); + } + + //std::string str() const {return reflection; } + + private: + //fixed size char instead of string + // + std::string uuid; + //char uuid[36]; +}; +} From 5c63a1877128c595c9a194336f8e0d3bb2d35421 Mon Sep 17 00:00:00 2001 From: Jeremy Perez Date: Sat, 7 Oct 2023 00:01:00 +0200 Subject: [PATCH 2/9] trying to make the constexpr function prototype work. --- include/rfl.hpp | 1 + include/rfl/UUID.hpp | 53 +++++++++++++++++++++++++++----------------- tests/test_uuid.hpp | 18 +++++++++++++++ 3 files changed, 52 insertions(+), 20 deletions(-) create mode 100644 tests/test_uuid.hpp diff --git a/include/rfl.hpp b/include/rfl.hpp index e38806c0..37407577 100644 --- a/include/rfl.hpp +++ b/include/rfl.hpp @@ -28,5 +28,6 @@ #include "rfl/replace.hpp" #include "rfl/to_named_tuple.hpp" #include "rfl/visit.hpp" +#include "rfl/UUID.hpp" #endif diff --git a/include/rfl/UUID.hpp b/include/rfl/UUID.hpp index 1b4c2868..28080f5a 100644 --- a/include/rfl/UUID.hpp +++ b/include/rfl/UUID.hpp @@ -1,5 +1,5 @@ -#ifndef RFL_TIMESTAMP_HPP_ -#define RFL_TIMESTAMP_HPP_ +#ifndef RFL_UUID_HPP_ +#define RFL_UUID_HPP_ #include #include @@ -14,26 +14,29 @@ namespace rfl { +template +consteval const auto _get_pattern(const internal::StringLiteral _v) { + if constexpr (_v == internal::StringLiteral{"1"}) { + return "^[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}$"; + } else if constexpr (_v == internal::StringLiteral{"2"}) { + return "^[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}$"; + } else if constexpr (_v == internal::StringLiteral{"3"}) { + return "^[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}$"; + } + else if constexpr (_v == internal::StringLiteral{"4"}) { + return "^[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}$"; + } else { + static_assert("UUID Version must be one the following: 1, 2, 3, 4."); + } +} template class UUID { using ReflectionType = std::string; constexpr static const internal::StringLiteral version_ = _version; - static constexpr const char* version_pattern = [] { - if constexpr (version_ == "1") { - return "^[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}$"; - } else if constexpr (version_ == "2") { - return "^[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}$"; - } else if constexpr (version_ == "3") { - return "^[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}$"; - } - else if constexpr (version_ == "3") { - return "^[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}$"; - } else { - static_assert("UUID Version must be one the following: 1, 2, 3, 4."); - } - }(); + + constexpr static const char* version_pattern = _get_pattern(version_); public: UUID(const char* _str) { @@ -42,21 +45,31 @@ class UUID { throw std::runtime_error( "String '" + std::string(_str) + "' did not match format the UUIDv" + std::string(version_) + " format"); } - uuid = _str; + uuid_ = _str; } UUID(const std::string& _str) : UUID(_str.c_str()) {}; + ~UUID() = default; ReflectionType reflection() const { - return std::string(uuid); + return std::string(uuid_); } - //std::string str() const {return reflection; } + /// Expresses the underlying timestamp as a string. + std::string str() const { return reflection; } private: //fixed size char instead of string // - std::string uuid; + std::string uuid_; //char uuid[36]; }; + +using UUIDv1 = UUID; +using UUIDv2 = UUID; +using UUIDv3 = UUID; +using UUIDv4 = UUID; } + + +#endif diff --git a/tests/test_uuid.hpp b/tests/test_uuid.hpp new file mode 100644 index 00000000..84c329ce --- /dev/null +++ b/tests/test_uuid.hpp @@ -0,0 +1,18 @@ +#include +#include "write_and_read.hpp" +void test_uuid() { + std::cout << "test_uuid" << std::endl; + + + struct Person { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", std::string> last_name = "Simpson"; + rfl::Field<"id", rfl::UUIDv1> id; + }; + + const auto bart = Person{.first_name = "Bart", .id = "12345678-1234-5678-1234-567812345678"}; + + //write_and_read(bart, + // "{\"firstName\":\"Bart\",\"lastName\":\"Simpson\"," + // "\"birthday\":\"1987-04-19\"}"); +} From 934c5d0045d2d6a9d6340a9fe3dc56659262ce3b Mon Sep 17 00:00:00 2001 From: Jeremy Perez Date: Sat, 7 Oct 2023 23:09:14 +0200 Subject: [PATCH 3/9] changed from uuid to constr, tested out different things, still not working properly. --- include/rfl/UUID.hpp | 75 -------------------- include/rfl/constr.hpp | 90 ++++++++++++++++++++++++ tests/{test_uuid.hpp => test_constr.hpp} | 9 +-- 3 files changed, 95 insertions(+), 79 deletions(-) delete mode 100644 include/rfl/UUID.hpp create mode 100644 include/rfl/constr.hpp rename tests/{test_uuid.hpp => test_constr.hpp} (52%) diff --git a/include/rfl/UUID.hpp b/include/rfl/UUID.hpp deleted file mode 100644 index 28080f5a..00000000 --- a/include/rfl/UUID.hpp +++ /dev/null @@ -1,75 +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 -consteval const auto _get_pattern(const internal::StringLiteral _v) { - if constexpr (_v == internal::StringLiteral{"1"}) { - return "^[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}$"; - } else if constexpr (_v == internal::StringLiteral{"2"}) { - return "^[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}$"; - } else if constexpr (_v == internal::StringLiteral{"3"}) { - return "^[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}$"; - } - else if constexpr (_v == internal::StringLiteral{"4"}) { - return "^[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}$"; - } else { - static_assert("UUID Version must be one the following: 1, 2, 3, 4."); - } -} - -template -class UUID { - using ReflectionType = std::string; - - constexpr static const internal::StringLiteral version_ = _version; - - constexpr static const char* version_pattern = _get_pattern(version_); - - public: - UUID(const char* _str) { - std::regex uuid_regex(version_pattern); - if (!std::regex_match(uuid_regex, std::string(_str))) { - throw std::runtime_error( - "String '" + std::string(_str) + "' did not match format the UUIDv" + std::string(version_) + " format"); - } - uuid_ = _str; - } - - UUID(const std::string& _str) : UUID(_str.c_str()) {}; - ~UUID() = default; - - ReflectionType reflection() const { - return std::string(uuid_); - } - - /// Expresses the underlying timestamp as a string. - std::string str() const { return reflection; } - - private: - //fixed size char instead of string - // - std::string uuid_; - //char uuid[36]; -}; - -using UUIDv1 = UUID; -using UUIDv2 = UUID; -using UUIDv3 = UUID; -using UUIDv4 = UUID; -} - - -#endif diff --git a/include/rfl/constr.hpp b/include/rfl/constr.hpp new file mode 100644 index 00000000..a3906fb3 --- /dev/null +++ b/include/rfl/constr.hpp @@ -0,0 +1,90 @@ +#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 +//consteval const auto _get_pattern(const internal::StringLiteral _v) { +// if constexpr (_v == internal::StringLiteral{"1"}) { +// return "^[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}$"; +// } else if constexpr (_v == internal::StringLiteral{"2"}) { +// return "^[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}$"; +// } else if constexpr (_v == internal::StringLiteral{"3"}) { +// return "^[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}$"; +// } +// else if constexpr (_v == internal::StringLiteral{"4"}) { +// return "^[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}$"; +// } else { +// static_assert("UUID Version must be one the following: 1, 2, 3, 4."); +// } +//} + +template +class ConStr { + using ReflectionType = std::string; + + constexpr static const internal::StringLiteral pattern_ = _pattern; + + //constexpr static const char* version_pattern = _get_pattern(version_); + + public: + ConStr(const char* _str) { + std::regex regex_pattern(pattern_.str()); + if (!std::regex_match(std::string(_str), regex_pattern)) { + throw std::runtime_error( + "String '" + std::string(_str) + "' did not match format " + pattern_.str() + " format"); + } + 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 timestamp 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 timestamp when successful or an Error + /// otherwise. + static Result from_string(const std::string& _str) { + return from_string(_str.c_str()); + } + /// Expresses the underlying timestamp as a string. + std::string str() const { return reflection; } + +}; +/// UUID +using UUIDv1 = ConStr; +//using UUIDv2 = ConStr<"^[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<"^[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<"^[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}$">; + +/// email +//using email = ConStr<"^[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}$">; +} + + +#endif diff --git a/tests/test_uuid.hpp b/tests/test_constr.hpp similarity index 52% rename from tests/test_uuid.hpp rename to tests/test_constr.hpp index 84c329ce..ca2c8231 100644 --- a/tests/test_uuid.hpp +++ b/tests/test_constr.hpp @@ -10,9 +10,10 @@ void test_uuid() { rfl::Field<"id", rfl::UUIDv1> id; }; - const auto bart = Person{.first_name = "Bart", .id = "12345678-1234-5678-1234-567812345678"}; + const auto bart = Person{.first_name = "Bart", .id = "de76b905-5b32-4127-96d1-48e5321e11a6"}; + - //write_and_read(bart, - // "{\"firstName\":\"Bart\",\"lastName\":\"Simpson\"," - // "\"birthday\":\"1987-04-19\"}"); + write_and_read(bart, + "{\"firstName\":\"Bart\",\"lastName\":\"Simpson\"," + "\"birthday\":\"12345678-1234-5678-1234-567812345678\"}"); } From fdfec40f92677433166ce5b84363af0a4f031e81 Mon Sep 17 00:00:00 2001 From: Jeremy Perez Date: Mon, 9 Oct 2023 13:44:45 +0200 Subject: [PATCH 4/9] added constr and some samples. --- include/rfl.hpp | 2 +- include/rfl/ConStr.hpp | 92 ++++++++++++++++++++++++++++++++++++++++++ include/rfl/constr.hpp | 90 ----------------------------------------- tests/test_constr.hpp | 43 ++++++++++++++------ tests/tests.cpp | 2 + 5 files changed, 125 insertions(+), 104 deletions(-) create mode 100644 include/rfl/ConStr.hpp delete mode 100644 include/rfl/constr.hpp diff --git a/include/rfl.hpp b/include/rfl.hpp index 37407577..671884bf 100644 --- a/include/rfl.hpp +++ b/include/rfl.hpp @@ -28,6 +28,6 @@ #include "rfl/replace.hpp" #include "rfl/to_named_tuple.hpp" #include "rfl/visit.hpp" -#include "rfl/UUID.hpp" +#include "rfl/ConStr.hpp" #endif diff --git a/include/rfl/ConStr.hpp b/include/rfl/ConStr.hpp new file mode 100644 index 00000000..a11bc8dd --- /dev/null +++ b/include/rfl/ConStr.hpp @@ -0,0 +1,92 @@ +#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 shorter 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/constr.hpp b/include/rfl/constr.hpp deleted file mode 100644 index a3906fb3..00000000 --- a/include/rfl/constr.hpp +++ /dev/null @@ -1,90 +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 -//consteval const auto _get_pattern(const internal::StringLiteral _v) { -// if constexpr (_v == internal::StringLiteral{"1"}) { -// return "^[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}$"; -// } else if constexpr (_v == internal::StringLiteral{"2"}) { -// return "^[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}$"; -// } else if constexpr (_v == internal::StringLiteral{"3"}) { -// return "^[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}$"; -// } -// else if constexpr (_v == internal::StringLiteral{"4"}) { -// return "^[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}$"; -// } else { -// static_assert("UUID Version must be one the following: 1, 2, 3, 4."); -// } -//} - -template -class ConStr { - using ReflectionType = std::string; - - constexpr static const internal::StringLiteral pattern_ = _pattern; - - //constexpr static const char* version_pattern = _get_pattern(version_); - - public: - ConStr(const char* _str) { - std::regex regex_pattern(pattern_.str()); - if (!std::regex_match(std::string(_str), regex_pattern)) { - throw std::runtime_error( - "String '" + std::string(_str) + "' did not match format " + pattern_.str() + " format"); - } - 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 timestamp 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 timestamp when successful or an Error - /// otherwise. - static Result from_string(const std::string& _str) { - return from_string(_str.c_str()); - } - /// Expresses the underlying timestamp as a string. - std::string str() const { return reflection; } - -}; -/// UUID -using UUIDv1 = ConStr; -//using UUIDv2 = ConStr<"^[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<"^[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<"^[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}$">; - -/// email -//using email = ConStr<"^[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}$">; -} - - -#endif diff --git a/tests/test_constr.hpp b/tests/test_constr.hpp index ca2c8231..81a04077 100644 --- a/tests/test_constr.hpp +++ b/tests/test_constr.hpp @@ -1,19 +1,36 @@ -#include #include "write_and_read.hpp" -void test_uuid() { - std::cout << "test_uuid" << std::endl; +#include +void test_constr() { + std::cout << "test_constr" << std::endl; - struct Person { - rfl::Field<"firstName", std::string> first_name; - rfl::Field<"lastName", std::string> last_name = "Simpson"; - rfl::Field<"id", rfl::UUIDv1> id; - }; + // Custom validation for string + // Masked credit cards + using MaskedCreditCardType = rfl::ConStr; + // Nick name between 5 and 10 characters long without regex use + using NickNameType = rfl::ConStr<"", 5, 10>; - const auto bart = Person{.first_name = "Bart", .id = "de76b905-5b32-4127-96d1-48e5321e11a6"}; - + struct Person { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", std::string> last_name = "Simpson"; + rfl::Field<"id", rfl::UUIDv4> id; + rfl::Field<"payload", rfl::Base64Encoded> payload; + rfl::Field<"maskedCreditCard", MaskedCreditCardType> masked_credit_card; + rfl::Field<"nickName", NickNameType> nick_name; + }; - write_and_read(bart, - "{\"firstName\":\"Bart\",\"lastName\":\"Simpson\"," - "\"birthday\":\"12345678-1234-5678-1234-567812345678\"}"); + const auto bart = Person{ + .first_name = "Bart", + .id = "de76b905-5b32-4127-96d1-48e5321e11a6", + .payload = "aGVsbG93b3JsZAo=", + .masked_credit_card = "XXXX-XXXX-XXXX-4440", + .nick_name = "Cosmo", + + }; + + write_and_read(bart, "{\"firstName\":\"Bart\",\"lastName\":\"Simpson\"," + "\"id\":\"de76b905-5b32-4127-96d1-48e5321e11a6\"," + "\"payload\":\"aGVsbG93b3JsZAo=\"," + "\"maskedCreditCard\":\"XXXX-XXXX-XXXX-4440\"," + "\"nickName\":\"Cosmo\"}"); } diff --git a/tests/tests.cpp b/tests/tests.cpp index f7e1e8ce..39ff4bf8 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -10,6 +10,7 @@ #include "test_ref.hpp" #include "test_tagged_union.hpp" #include "test_timestamp.hpp" +#include "test_constr.hpp" #include "test_variant.hpp" int main() { @@ -26,6 +27,7 @@ int main() { test_custom_class3(); test_array(); test_timestamp(); + test_constr(); return 0; } From fbd1d95e7e0431dd43c67d2697f72a3043fc475e Mon Sep 17 00:00:00 2001 From: Jeremy Perez Date: Mon, 9 Oct 2023 13:46:07 +0200 Subject: [PATCH 5/9] fixed typo in error msg. --- include/rfl/ConStr.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/rfl/ConStr.hpp b/include/rfl/ConStr.hpp index a11bc8dd..030fd824 100644 --- a/include/rfl/ConStr.hpp +++ b/include/rfl/ConStr.hpp @@ -41,7 +41,7 @@ public: if constexpr (_max >= 0) { if (unvalidated_str.length() > _max) { throw std::runtime_error("String '" + unvalidated_str + - "' is shorter than max length" + + "' is longer than max length" + std::to_string(_max)); } } From 15b12a34b366b5edb719e9792105312e3cb8fd78 Mon Sep 17 00:00:00 2001 From: Jeremy Perez Date: Fri, 20 Oct 2023 15:07:43 +0200 Subject: [PATCH 6/9] redesign of validation via Annotation. --- .clang-format | 1 + include/rfl.hpp | 1 + include/rfl/Validator.hpp | 113 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 115 insertions(+) create mode 100644 .clang-format create mode 100644 include/rfl/Validator.hpp diff --git a/.clang-format b/.clang-format new file mode 100644 index 00000000..90cfebd6 --- /dev/null +++ b/.clang-format @@ -0,0 +1 @@ +BasedOnStyle : Google diff --git a/include/rfl.hpp b/include/rfl.hpp index 671884bf..6066a156 100644 --- a/include/rfl.hpp +++ b/include/rfl.hpp @@ -29,5 +29,6 @@ #include "rfl/to_named_tuple.hpp" #include "rfl/visit.hpp" #include "rfl/ConStr.hpp" +#include "rfl/Validator.hpp" #endif diff --git a/include/rfl/Validator.hpp b/include/rfl/Validator.hpp new file mode 100644 index 00000000..cf1df9e4 --- /dev/null +++ b/include/rfl/Validator.hpp @@ -0,0 +1,113 @@ +#ifndef RFL_VALIDATOR_HPP_ +#define RFL_VALIDATOR_HPP_ + +#include +#include +#include +#include +#include + +#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 { + public: + using ReflectionType = _Type; + ReflectionType value; + + // template + // Annotate(T _value) : value(static_cast<_Type>(_value)) { + // std::optional validate_check = Class::validate(value); + // if (!validate_check.has_value()) { + // throw std::runtime_error(validate_check.value().what()); + // } + // }; + // + 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()); + } + } +}; + +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; +}; + +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; + } +}; + +Annotate> a("hello"); +Annotate> b("hello"); +Annotate> c(2); +Annotate> d(2); +Annotate, Length<1,2>> e({1,2}); + +} // namespace rfl +// +#endif From a41f35ce84b4168d22dccfa51c260006367a10fb Mon Sep 17 00:00:00 2001 From: Jeremy Perez Date: Fri, 27 Oct 2023 12:46:17 +0200 Subject: [PATCH 7/9] adapted test. --- include/rfl/Validator.hpp | 37 +++++++++++++++------------ tests/test_constr.hpp | 54 +++++++++++++++++++++++++++------------ 2 files changed, 59 insertions(+), 32 deletions(-) diff --git a/include/rfl/Validator.hpp b/include/rfl/Validator.hpp index cf1df9e4..0a656b0e 100644 --- a/include/rfl/Validator.hpp +++ b/include/rfl/Validator.hpp @@ -30,20 +30,31 @@ struct Annotate { using ReflectionType = _Type; ReflectionType value; - // template - // Annotate(T _value) : value(static_cast<_Type>(_value)) { - // std::optional validate_check = Class::validate(value); - // if (!validate_check.has_value()) { - // throw std::runtime_error(validate_check.value().what()); - // } - // }; - // - Annotate(ReflectionType _value) : value(_value) { + template , bool> = true > + Annotate(const T& _value) : value(_value) { std::optional validate_check = Class::validate(value); - if (!validate_check.has_value()) { + if (validate_check.has_value()) { + throw std::runtime_error(validate_check.value().what()); + } + }; + + 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()); } } + + 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()); + } + } + }; template @@ -102,12 +113,6 @@ struct Pattern { } }; -Annotate> a("hello"); -Annotate> b("hello"); -Annotate> c(2); -Annotate> d(2); -Annotate, Length<1,2>> e({1,2}); - } // namespace rfl // #endif diff --git a/tests/test_constr.hpp b/tests/test_constr.hpp index 81a04077..0fd41da4 100644 --- a/tests/test_constr.hpp +++ b/tests/test_constr.hpp @@ -1,36 +1,58 @@ -#include "write_and_read.hpp" +#include #include -void test_constr() { +#include "write_and_read.hpp" +void test_constr() { std::cout << "test_constr" << std::endl; // Custom validation for string // Masked credit cards - using MaskedCreditCardType = rfl::ConStr; - // Nick name between 5 and 10 characters long without regex use - using NickNameType = rfl::ConStr<"", 5, 10>; + using MaskedCreditCardType = rfl::Annotate>; + //// Nick name between 5 and 10 characters long without regex use + using NickNameType = rfl::Annotate>; struct Person { rfl::Field<"firstName", std::string> first_name; rfl::Field<"lastName", std::string> last_name = "Simpson"; - rfl::Field<"id", rfl::UUIDv4> id; - rfl::Field<"payload", rfl::Base64Encoded> payload; + //rfl::Field<"id", rfl::UUIDv4> id; + //rfl::Field<"payload", rfl::Base64Encoded> payload; rfl::Field<"maskedCreditCard", MaskedCreditCardType> masked_credit_card; rfl::Field<"nickName", NickNameType> nick_name; }; - const auto bart = Person{ + try { + const auto wrong_bart = Person{ + .first_name = "Bart", + //.id = "de76b905-5b32-4127-96d1-48e5321e11a6", + //.payload = "aGVsbG93b3JsZAo=", + .masked_credit_card = "XXXX-XXXX-XXXX-444A", + .nick_name = "Cosmo", + }; + } catch (const std::exception &e) { + std::cout << e.what() << std::endl; + //assert( + // e.what() == + // std::string( + // R"(String 'XXXX-XXXX-XXXX-444A' did not match format 'XXXX-XXXX-XXXX-\d{4}')")); + } + const auto correct_bart = Person{ .first_name = "Bart", - .id = "de76b905-5b32-4127-96d1-48e5321e11a6", - .payload = "aGVsbG93b3JsZAo=", + //.id = "de76b905-5b32-4127-96d1-48e5321e11a6", + //.payload = "aGVsbG93b3JsZAo=", .masked_credit_card = "XXXX-XXXX-XXXX-4440", .nick_name = "Cosmo", - }; - write_and_read(bart, "{\"firstName\":\"Bart\",\"lastName\":\"Simpson\"," - "\"id\":\"de76b905-5b32-4127-96d1-48e5321e11a6\"," - "\"payload\":\"aGVsbG93b3JsZAo=\"," - "\"maskedCreditCard\":\"XXXX-XXXX-XXXX-4440\"," - "\"nickName\":\"Cosmo\"}"); + write_and_read(correct_bart, + "{\"firstName\":\"Bart\",\"lastName\":\"Simpson\"," + "\"id\":\"de76b905-5b32-4127-96d1-48e5321e11a6\"," + "\"payload\":\"aGVsbG93b3JsZAo=\"," + "\"maskedCreditCard\":\"XXXX-XXXX-XXXX-4440\"," + "\"nickName\":\"Cosmo\"}"); + + //struct Blub { + // rfl::Field<"firstName", + // rfl::Validate>; + //}; + // } From b9a4542ea09ffce2a9119f267a5e93b73be0c552 Mon Sep 17 00:00:00 2001 From: Jeremy Perez Date: Fri, 27 Oct 2023 12:46:17 +0200 Subject: [PATCH 8/9] adapted test. --- include/rfl/Validator.hpp | 118 ++++++++++++++++++++++++++++++++++++++ tests/test_constr.hpp | 58 +++++++++++++++++++ 2 files changed, 176 insertions(+) create mode 100644 include/rfl/Validator.hpp create mode 100644 tests/test_constr.hpp diff --git a/include/rfl/Validator.hpp b/include/rfl/Validator.hpp new file mode 100644 index 00000000..0a656b0e --- /dev/null +++ b/include/rfl/Validator.hpp @@ -0,0 +1,118 @@ +#ifndef RFL_VALIDATOR_HPP_ +#define RFL_VALIDATOR_HPP_ + +#include +#include +#include +#include +#include + +#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 { + public: + using ReflectionType = _Type; + ReflectionType value; + + 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()); + } + }; + + 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()); + } + } + + 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()); + } + } + +}; + +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; +}; + +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 +// +#endif diff --git a/tests/test_constr.hpp b/tests/test_constr.hpp new file mode 100644 index 00000000..0fd41da4 --- /dev/null +++ b/tests/test_constr.hpp @@ -0,0 +1,58 @@ +#include +#include + +#include "write_and_read.hpp" +void test_constr() { + std::cout << "test_constr" << std::endl; + + // Custom validation for string + // Masked credit cards + using MaskedCreditCardType = rfl::Annotate>; + //// Nick name between 5 and 10 characters long without regex use + using NickNameType = rfl::Annotate>; + + struct Person { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", std::string> last_name = "Simpson"; + //rfl::Field<"id", rfl::UUIDv4> id; + //rfl::Field<"payload", rfl::Base64Encoded> payload; + rfl::Field<"maskedCreditCard", MaskedCreditCardType> masked_credit_card; + rfl::Field<"nickName", NickNameType> nick_name; + }; + + try { + const auto wrong_bart = Person{ + .first_name = "Bart", + //.id = "de76b905-5b32-4127-96d1-48e5321e11a6", + //.payload = "aGVsbG93b3JsZAo=", + .masked_credit_card = "XXXX-XXXX-XXXX-444A", + .nick_name = "Cosmo", + }; + } catch (const std::exception &e) { + std::cout << e.what() << std::endl; + //assert( + // e.what() == + // std::string( + // R"(String 'XXXX-XXXX-XXXX-444A' did not match format 'XXXX-XXXX-XXXX-\d{4}')")); + } + const auto correct_bart = Person{ + .first_name = "Bart", + //.id = "de76b905-5b32-4127-96d1-48e5321e11a6", + //.payload = "aGVsbG93b3JsZAo=", + .masked_credit_card = "XXXX-XXXX-XXXX-4440", + .nick_name = "Cosmo", + }; + + write_and_read(correct_bart, + "{\"firstName\":\"Bart\",\"lastName\":\"Simpson\"," + "\"id\":\"de76b905-5b32-4127-96d1-48e5321e11a6\"," + "\"payload\":\"aGVsbG93b3JsZAo=\"," + "\"maskedCreditCard\":\"XXXX-XXXX-XXXX-4440\"," + "\"nickName\":\"Cosmo\"}"); + + //struct Blub { + // rfl::Field<"firstName", + // rfl::Validate>; + //}; + // +} From 62f60b1cf390cadb8d30e72b04bf290f64dc4857 Mon Sep 17 00:00:00 2001 From: Jeremy Perez Date: Fri, 27 Oct 2023 13:13:08 +0200 Subject: [PATCH 9/9] add missing include headers. --- tests/tests.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/tests.cpp b/tests/tests.cpp index b79fd5a5..10e03bdb 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -32,6 +32,10 @@ #include "test_unique_ptr.hpp" #include "test_unique_ptr2.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_variant.hpp" int main() {