From fdfec40f92677433166ce5b84363af0a4f031e81 Mon Sep 17 00:00:00 2001 From: Jeremy Perez Date: Mon, 9 Oct 2023 13:44:45 +0200 Subject: [PATCH] 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; }