[ox] Make UUID serializable, and make serialize as a string in OC

This commit is contained in:
Gary Talent 2023-02-16 01:36:20 -06:00
parent b53e8626d7
commit 762804905a
6 changed files with 42 additions and 3 deletions

View File

@ -290,6 +290,12 @@ Error OrganicClawReader::fieldCString(const char *key, char **val, std::size_t b
return err; return err;
} }
Error OrganicClawReader::field(const char *key, UUID *val) noexcept {
UUIDStr str;
oxReturnError(field(key, &str));
return UUID::fromString(str).moveTo(val);
}
Result<std::size_t> OrganicClawReader::arrayLength(const char *key, bool) noexcept { Result<std::size_t> OrganicClawReader::arrayLength(const char *key, bool) noexcept {
const auto &jv = value(key); const auto &jv = value(key);
if (jv.empty()) { if (jv.empty()) {

View File

@ -21,6 +21,7 @@
#include <ox/std/memops.hpp> #include <ox/std/memops.hpp>
#include <ox/std/memory.hpp> #include <ox/std/memory.hpp>
#include <ox/std/string.hpp> #include <ox/std/string.hpp>
#include <ox/std/uuid.hpp>
namespace ox { namespace ox {
@ -79,6 +80,8 @@ class OrganicClawReader {
Error fieldCString(const char *key, char **val, std::size_t buffLen) noexcept; Error fieldCString(const char *key, char **val, std::size_t buffLen) noexcept;
Error field(const char *key, UUID *val) noexcept;
/** /**
* Reads an array length from the current location in the buffer. * Reads an array length from the current location in the buffer.
* @param pass indicates that the parsing should iterate past the array length * @param pass indicates that the parsing should iterate past the array length

View File

@ -46,6 +46,15 @@ Error OrganicClawWriter::fieldCString(const char *key, char **val) noexcept {
return fieldCString(key, const_cast<const char**>(val), {}); return fieldCString(key, const_cast<const char**>(val), {});
} }
Error OrganicClawWriter::field(const char *key, const UUID *uuid) noexcept {
const auto uuidStr = uuid->toString();
if (targetValid() && uuidStr.len()) {
value(key) = uuidStr.c_str();
}
++m_fieldIt;
return {};
}
Json::Value &OrganicClawWriter::value(const char *key) noexcept { Json::Value &OrganicClawWriter::value(const char *key) noexcept {
if (m_json.isArray()) { if (m_json.isArray()) {
return m_json[m_fieldIt]; return m_json[m_fieldIt];

View File

@ -18,6 +18,7 @@
#include <ox/std/buffer.hpp> #include <ox/std/buffer.hpp>
#include <ox/std/hashmap.hpp> #include <ox/std/hashmap.hpp>
#include <ox/std/string.hpp> #include <ox/std/string.hpp>
#include <ox/std/uuid.hpp>
namespace ox { namespace ox {
@ -105,7 +106,7 @@ class OrganicClawWriter {
value(key) = *val; value(key) = *val;
} }
++m_fieldIt; ++m_fieldIt;
return OxError(0); return {};
} }
template<typename T> template<typename T>
@ -130,7 +131,7 @@ class OrganicClawWriter {
value(key) = w.m_json; value(key) = w.m_json;
} }
++m_fieldIt; ++m_fieldIt;
return OxError(0); return {};
} }
template<typename T> template<typename T>
@ -172,6 +173,8 @@ class OrganicClawWriter {
Error fieldCString(const char *name, char **val) noexcept; Error fieldCString(const char *name, char **val) noexcept;
Error field(const char *key, const UUID *uuid) noexcept;
template<typename T> template<typename T>
Error field(const char*, T *val) noexcept; Error field(const char*, T *val) noexcept;

View File

@ -64,6 +64,10 @@ class BString {
constexpr Error append(const char *str, std::size_t strLen) noexcept; constexpr Error append(const char *str, std::size_t strLen) noexcept;
[[nodiscard]]
constexpr const char *data() const noexcept;
[[nodiscard]]
constexpr char *data() noexcept; constexpr char *data() noexcept;
[[nodiscard]] [[nodiscard]]
@ -228,6 +232,11 @@ constexpr Error BString<buffLen>::append(const char *str, std::size_t strLen) no
return err; return err;
} }
template<std::size_t buffLen>
constexpr const char *BString<buffLen>::data() const noexcept {
return static_cast<const char*>(m_buff);
}
template<std::size_t buffLen> template<std::size_t buffLen>
constexpr char *BString<buffLen>::data() noexcept { constexpr char *BString<buffLen>::data() noexcept {
return static_cast<char*>(m_buff); return static_cast<char*>(m_buff);

View File

@ -90,7 +90,9 @@ constexpr ox::BString<2> toHex(uint8_t v) noexcept {
} }
class UUID { class UUID {
private: template<typename T>
friend constexpr Error model(T *io, ox::CommonPtrWith<UUID> auto *obj) noexcept;
protected:
static bool s_seeded; static bool s_seeded;
static Random s_rand; static Random s_rand;
ox::Array<uint8_t, 16> m_value; ox::Array<uint8_t, 16> m_value;
@ -158,4 +160,11 @@ class UUID {
} }
}; };
template<typename T>
constexpr Error model(T *io, ox::CommonPtrWith<UUID> auto *obj) noexcept {
io->template setTypeInfo<UUID>();
oxReturnError(io->field("value", &obj->m_value));
return {};
}
} }