Skip to content
Snippets Groups Projects
Commit 8acc01a2 authored by Daniel Hornung's avatar Daniel Hornung
Browse files

ENH: Empty listst are handled now.

parent f0fc164f
No related branches found
No related tags found
No related merge requests found
Pipeline #12334 failed
......@@ -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
......
......@@ -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>>();
}
......
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment