diff options
author | Roman Smrž <roman.smrz@seznam.cz> | 2020-02-20 21:22:03 +0100 |
---|---|---|
committer | Roman Smrž <roman.smrz@seznam.cz> | 2020-03-01 21:17:26 +0100 |
commit | 751cdc6c0235a0623ed980b24b71acb85e94dacf (patch) | |
tree | fb0b82046f802060c713cc707b5a2f44b284462f /src/storage.cpp | |
parent | 0e9e9c4d233a331e10dfb2db889fe437d0911ba2 (diff) |
UUID record item type
Diffstat (limited to 'src/storage.cpp')
-rw-r--r-- | src/storage.cpp | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/storage.cpp b/src/storage.cpp index 07fef51..e0819d4 100644 --- a/src/storage.cpp +++ b/src/storage.cpp @@ -536,6 +536,30 @@ const Storage & Ref::storage() const } +UUID::UUID(string str) +{ + if (uuid_parse(str.c_str(), uuid) != 0) + throw runtime_error("invalid UUID"); +} + +UUID::operator string() const +{ + string str(UUID_STR_LEN - 1, '\0'); + uuid_unparse_lower(uuid, str.data()); + return str; +} + +bool UUID::operator==(const UUID & other) const +{ + return std::equal(std::begin(uuid), std::end(uuid), std::begin(other.uuid)); +} + +bool UUID::operator!=(const UUID & other) const +{ + return !(*this == other); +} + + template<class S> RecordT<S>::Item::operator bool() const { @@ -567,6 +591,14 @@ optional<vector<uint8_t>> RecordT<S>::Item::asBinary() const } template<class S> +optional<UUID> RecordT<S>::Item::asUUID() const +{ + if (holds_alternative<UUID>(value)) + return std::get<UUID>(value); + return nullopt; +} + +template<class S> optional<typename S::Ref> RecordT<S>::Item::asRef() const { if (holds_alternative<typename S::Ref>(value)) @@ -623,6 +655,8 @@ optional<RecordT<S>> RecordT<S>::decode(const S & st, items->emplace_back(name, value); else if (type == "b") items->emplace_back(name, base64::decode(value)); + else if (type == "u") + items->emplace_back(name, UUID(value)); else if (type == "r.b2") { if constexpr (is_same_v<S, Storage>) { if (auto ref = st.ref(Digest(value))) @@ -702,6 +736,9 @@ vector<uint8_t> RecordT<S>::encodeInner() const } else if (auto x = item.asBinary()) { type = "b"; value = base64::encode(*x); + } else if (auto x = item.asUUID()) { + type = "u"; + value = string(*x); } else if (auto x = item.asRef()) { type = "r.b2"; value = string(x->digest()); |