changed from uuid to constr, tested out different things, still not working properly.

This commit is contained in:
Jeremy Perez
2023-10-07 23:09:14 +02:00
parent 5c63a18771
commit 934c5d0045
3 changed files with 95 additions and 79 deletions
-75
View File
@@ -1,75 +0,0 @@
#ifndef RFL_UUID_HPP_
#define RFL_UUID_HPP_
#include <ctime>
#include <iterator>
#include <stdexcept>
#include <string>
#include <regex>
#include "internal/StringLiteral.hpp"
#include "rfl/Literal.hpp"
#include "rfl/Result.hpp"
namespace rfl {
template <size_t N>
consteval const auto _get_pattern(const internal::StringLiteral<N> _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 <internal::StringLiteral _version>
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<internal::StringLiteral{"1"}>;
using UUIDv2 = UUID<internal::StringLiteral{"2"}>;
using UUIDv3 = UUID<internal::StringLiteral{"3"}>;
using UUIDv4 = UUID<internal::StringLiteral{"3"}>;
}
#endif
+90
View File
@@ -0,0 +1,90 @@
#ifndef RFL_UUID_HPP_
#define RFL_UUID_HPP_
#include <ctime>
#include <iterator>
#include <stdexcept>
#include <string>
#include <regex>
#include "internal/StringLiteral.hpp"
#include "rfl/Literal.hpp"
#include "rfl/Result.hpp"
namespace rfl {
//template <size_t N>
//consteval const auto _get_pattern(const internal::StringLiteral<N> _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 <internal::StringLiteral _pattern>
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<ConStr> 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<ConStr> 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<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<"^[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
@@ -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\"}");
}