summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoman Smrž <roman.smrz@seznam.cz>2023-04-08 18:35:46 +0200
committerRoman Smrž <roman.smrz@seznam.cz>2023-04-08 18:35:46 +0200
commita9a5308cf03fcd25492317b844a59019eacfac13 (patch)
treef7f6d8e1d42cc8280bff98511d55a234371903fa
parent182dd4271c96305c69e0a950a4cdada38d39eac8 (diff)
Empty record item type
-rw-r--r--include/erebos/storage.h4
-rw-r--r--src/storage.cpp19
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<uint8_t>,
@@ -253,6 +256,7 @@ public:
operator bool() const;
+ std::optional<Empty> asEmpty() const;
std::optional<int> asInteger() const;
std::optional<std::string> asText() const;
std::optional<std::vector<uint8_t>> asBinary() const;
diff --git a/src/storage.cpp b/src/storage.cpp
index 2e948b0..b5688bb 100644
--- a/src/storage.cpp
+++ b/src/storage.cpp
@@ -1079,6 +1079,14 @@ RecordT<S>::Item::operator bool() const
}
template<class S>
+optional<typename RecordT<S>::Item::Empty> RecordT<S>::Item::asEmpty() const
+{
+ if (holds_alternative<RecordT<S>::Item::Empty>(value))
+ return std::get<RecordT<S>::Item::Empty>(value);
+ return nullopt;
+}
+
+template<class S>
optional<int> RecordT<S>::Item::asInteger() const
{
if (holds_alternative<int>(value))
@@ -1179,7 +1187,11 @@ optional<RecordT<S>> RecordT<S>::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<uint8_t> RecordT<S>::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()) {