From a9a5308cf03fcd25492317b844a59019eacfac13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Sat, 8 Apr 2023 18:35:46 +0200 Subject: Empty record item type --- include/erebos/storage.h | 4 ++++ src/storage.cpp | 19 +++++++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/include/erebos/storage.h b/include/erebos/storage.h index c506dfd..ed537eb 100644 --- a/include/erebos/storage.h +++ b/include/erebos/storage.h @@ -230,8 +230,11 @@ public: std::string value; }; + struct Empty {}; + typedef std::variant< std::monostate, + Empty, int, std::string, std::vector, @@ -253,6 +256,7 @@ public: operator bool() const; + std::optional asEmpty() const; std::optional asInteger() const; std::optional asText() const; std::optional> asBinary() const; diff --git a/src/storage.cpp b/src/storage.cpp index 2e948b0..b5688bb 100644 --- a/src/storage.cpp +++ b/src/storage.cpp @@ -1078,6 +1078,14 @@ RecordT::Item::operator bool() const return !holds_alternative(value); } +template +optional::Item::Empty> RecordT::Item::asEmpty() const +{ + if (holds_alternative::Item::Empty>(value)) + return std::get::Item::Empty>(value); + return nullopt; +} + template optional RecordT::Item::asInteger() const { @@ -1179,7 +1187,11 @@ optional> RecordT::decode(const S & st, begin = newline + 1; } - if (type == "i") + if (type == "e") { + if (value.size() != 0) + return nullopt; + items->emplace_back(name, typename Item::Empty {}); + } else if (type == "i") try { items->emplace_back(name, std::stoi(value)); } catch (invalid_argument &) { @@ -1271,7 +1283,10 @@ vector RecordT::encodeInner() const string type; string value; - if (auto x = item.asInteger()) { + if (item.asEmpty()) { + type = "e"; + value = ""; + } else if (auto x = item.asInteger()) { type = "i"; value = to_string(*x); } else if (auto x = item.asText()) { -- cgit v1.2.3