From 934c5d0045d2d6a9d6340a9fe3dc56659262ce3b Mon Sep 17 00:00:00 2001 From: Jeremy Perez Date: Sat, 7 Oct 2023 23:09:14 +0200 Subject: [PATCH] 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\"}"); }