diff --git a/CHANGELOG.md b/CHANGELOG.md index 03710a3d17afebc1636ed47bd870742db3be9e04..d5debc0941463bd9811e253a7742f9fd2ea4bcc4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - New functions getEnumNameFromValue() and getEnumValueFromName(). +- Values with empty lists are implemented now. ### Changed diff --git a/include/caosdb/value.h b/include/caosdb/value.h index 989f780ed25a535fb34a97281f55e29c005a6c53..d5e6b1fa8e2c2da9cbdbf95acf64f7983019e4bc 100644 --- a/include/caosdb/value.h +++ b/include/caosdb/value.h @@ -34,6 +34,10 @@ for (const auto &value : values) { \ this->wrapped->mutable_list_values()->add_values()->SETTER(value); \ } \ + if (values.empty()) { \ + this->wrapped->mutable_list_values()->add_values()->set_special_value \ + (ProtoSpecialValue::SPECIAL_VALUE_UNSPECIFIED); \ + } \ } namespace caosdb::entity { @@ -177,8 +181,11 @@ public: [[nodiscard]] inline auto IsList() const noexcept -> bool { return this->wrapped->value_case() == ValueCase::kListValues; } - [[nodiscard]] inline auto AsList() const noexcept -> const std::vector<ScalarValue> & { - if (!IsList()) { + [[nodiscard]] inline auto AsList() const noexcept -> const std::vector<ScalarValue> & + { + if (!IsList() || (this->wrapped->list_values().values(0).has_special_value() + && this->wrapped->list_values().values(0).special_value() + == ProtoSpecialValue::SPECIAL_VALUE_UNSPECIFIED)) { // create empty list this->list_values = std::make_unique<std::vector<ScalarValue>>(); } diff --git a/test/test_value.cpp b/test/test_value.cpp index 74d30f04b621a0e118bc7591a07aab052786c790..adc830d784901e35f2ccbfbafae176b59d0b63a1 100644 --- a/test/test_value.cpp +++ b/test/test_value.cpp @@ -27,6 +27,7 @@ #include <gtest/gtest-message.h> // for Message #include <gtest/gtest-test-part.h> // for TestPartResult, SuiteApi... #include <gtest/gtest_pred_impl.h> // for AssertionResult, Test +#include <gtest/gtest-spi.h> #include <initializer_list> // for initializer_list #include <string> // for string, basic_string #include <vector> // for vector @@ -134,5 +135,13 @@ TEST(test_value, test_list) { EXPECT_EQ(item.IsString(), true); EXPECT_EQ(item.AsString(), "id" + std::to_string(counter++)); } + + // Test empty lists + auto empty_content = std::vector<int>(); + Value value_0(empty_content); + EXPECT_TRUE(value_0.IsList()); + auto list_value_0 = value_0.AsList(); + EXPECT_EQ(list_value_0.size(), 0); + EXPECT_TRUE(list_value_0.empty()); } } // namespace caosdb::entity