From 495d2cb6b47b309070b31e0ef83fa5731a150a6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Mon, 27 Jan 2020 21:56:06 +0100 Subject: Storage: handle record items with unknown type --- include/erebos/storage.h | 10 +++++++++- src/storage.cpp | 14 +++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/include/erebos/storage.h b/include/erebos/storage.h index d855460..d23022f 100644 --- a/include/erebos/storage.h +++ b/include/erebos/storage.h @@ -160,12 +160,19 @@ class RecordT public: class Item { public: + struct UnknownType + { + std::string type; + std::string value; + }; + typedef std::variant< std::monostate, int, std::string, std::vector, - typename S::Ref> Variant; + typename S::Ref, + UnknownType> Variant; Item(const std::string & name): Item(name, std::monostate()) {} @@ -184,6 +191,7 @@ public: std::optional asText() const; std::optional> asBinary() const; std::optional asRef() const; + std::optional asUnknown() const; template std::optional> as() const; diff --git a/src/storage.cpp b/src/storage.cpp index 6b2e4f8..db9f629 100644 --- a/src/storage.cpp +++ b/src/storage.cpp @@ -574,6 +574,14 @@ optional RecordT::Item::asRef() const return nullopt; } +template +optional::Item::UnknownType> RecordT::Item::asUnknown() const +{ + if (holds_alternative(value)) + return std::get(value); + return nullopt; +} + template RecordT::RecordT(const vector & from): @@ -625,7 +633,8 @@ optional> RecordT::decode(const S & st, items->emplace_back(name, st.ref(Digest(value))); } } else - throw runtime_error("unknown record item type"); + items->emplace_back(name, + typename Item::UnknownType { type, value }); begin = newline + 1; } @@ -696,6 +705,9 @@ vector RecordT::encodeInner() const } else if (auto x = item.asRef()) { type = "r.b2"; value = string(x->digest()); + } else if (auto x = item.asUnknown()) { + type = x->type; + value = x->value; } else { throw runtime_error("unhandeled record item type"); } -- cgit v1.2.3