diff options
author | Roman Smrž <roman.smrz@seznam.cz> | 2020-04-16 21:42:45 +0200 |
---|---|---|
committer | Roman Smrž <roman.smrz@seznam.cz> | 2020-04-16 21:42:45 +0200 |
commit | 6cf5244bc514042fe419fffe1cd26a7f5e3c778f (patch) | |
tree | 4a72abf80980c3d10656bd82606a69329867d057 /src/identity.cpp | |
parent | a76a9ad65fa549d2c1650bb5a7d9a657186edc43 (diff) |
Remove optional from load result
Makes loading of data well-defined for arbitrary object contents.
Introduce zero reference and object to represent missing or mismatched
parts.
Diffstat (limited to 'src/identity.cpp')
-rw-r--r-- | src/identity.cpp | 45 |
1 files changed, 22 insertions, 23 deletions
diff --git a/src/identity.cpp b/src/identity.cpp index 3cba97c..2396b9f 100644 --- a/src/identity.cpp +++ b/src/identity.cpp @@ -24,12 +24,8 @@ optional<Identity> Identity::load(const vector<Ref> & refs) vector<Stored<Signed<IdentityData>>> data; data.reserve(refs.size()); - for (const auto & ref : refs) { - auto d = Stored<Signed<IdentityData>>::load(ref); - if (!d) - return nullopt; - data.push_back(*d); - } + for (const auto & ref : refs) + data.push_back(Stored<Signed<IdentityData>>::load(ref)); if (auto ptr = Priv::validate(data)) return Identity(ptr); @@ -124,27 +120,30 @@ void Identity::Builder::owner(const Identity & val) p->owner.emplace(val); } -optional<IdentityData> IdentityData::load(const Ref & ref) +IdentityData IdentityData::load(const Ref & ref) { - auto rec = ref->asRecord(); - if (!rec) - return nullopt; - - vector<Stored<Signed<IdentityData>>> prev; - for (auto p : rec->items("SPREV")) - if (const auto & x = p.as<Signed<IdentityData>>()) - prev.push_back(x.value()); + if (auto rec = ref->asRecord()) { + vector<Stored<Signed<IdentityData>>> prev; + for (auto p : rec->items("SPREV")) + if (const auto & x = p.as<Signed<IdentityData>>()) + prev.push_back(x.value()); - auto keyIdentity = rec->item("key-id").as<PublicKey>(); - if (!keyIdentity) - return nullopt; + if (auto keyIdentity = rec->item("key-id").as<PublicKey>()) + return IdentityData { + .prev = std::move(prev), + .name = rec->item("name").asText(), + .owner = rec->item("owner").as<Signed<IdentityData>>(), + .keyIdentity = keyIdentity.value(), + .keyMessage = rec->item("key-msg").as<PublicKey>(), + }; + } return IdentityData { - .prev = std::move(prev), - .name = rec->item("name").asText(), - .owner = rec->item("owner").as<Signed<IdentityData>>(), - .keyIdentity = keyIdentity.value(), - .keyMessage = rec->item("key-msg").as<PublicKey>(), + .prev = {}, + .name = nullopt, + .owner = nullopt, + .keyIdentity = Stored<PublicKey>::load(ref.storage().zref()), + .keyMessage = nullopt, }; } |