diff --git a/include/caosdb/data_type.h b/include/caosdb/data_type.h index 7afc694b6a3a723d126c21a45c52d9f4c9a7f710..f33ccaa88747815237a7ede49525a0b58946c08a 100644 --- a/include/caosdb/data_type.h +++ b/include/caosdb/data_type.h @@ -151,6 +151,22 @@ public: DataType(const std::string &data_type) : DataType() { this->wrapped->mutable_reference_data_type()->set_name(data_type); } + + inline static auto ListOf(const AtomicDataType &atomic_data_type) + -> DataType { + DataType result; + result.wrapped->mutable_list_data_type()->set_atomic_data_type( + static_cast<ProtoAtomicDataType>(atomic_data_type)); + return result; + } + inline static auto ListOf(const std::string reference_data_type) -> DataType { + DataType result; + result.wrapped->mutable_list_data_type() + ->mutable_reference_data_type() + ->set_name(reference_data_type); + return result; + } + [[nodiscard]] inline auto IsAtomic() const noexcept -> bool { return this->wrapped->data_type_case() == DataTypeCase::kAtomicDataType; } diff --git a/include/caosdb/entity.h b/include/caosdb/entity.h index b66a0d21e0cacb89df8652a0bdee45a11f2adbc2..25aae33ccfe1c388700f048e38fa671e0ff44abb 100644 --- a/include/caosdb/entity.h +++ b/include/caosdb/entity.h @@ -42,6 +42,7 @@ #include <boost/log/sources/record_ostream.hpp> // for basic_record_... #include <boost/preprocessor/seq/limits/enum_256.hpp> // for BOOST_PP_SEQ_... #include <boost/preprocessor/seq/limits/size_256.hpp> // for BOOST_PP_SEQ_... +#include <cstdint> // for int64_t #include <google/protobuf/message.h> // for RepeatedPtrField #include <google/protobuf/util/json_util.h> // for MessageToJson... #include <iosfwd> // for streamsize @@ -50,6 +51,7 @@ #include <random> // for mt19937, rand... #include <stdexcept> // for out_of_range #include <string> // for string, basic... +#include <vector> // for vector namespace caosdb::entity { using boost::filesystem::exists; @@ -514,9 +516,16 @@ public: */ auto SetValue(const Value &value) -> StatusCode; auto SetValue(const std::string &value) -> StatusCode; + auto SetValue(const char *value) -> StatusCode; auto SetValue(const double value) -> StatusCode; - // auto SetValue(const int64_t value) -> StatusCode; - // auto SetValue(const bool value) -> StatusCode; + auto SetValue(const std::vector<std::string> &values) -> StatusCode; + auto SetValue(const std::vector<char *> &values) -> StatusCode; + auto SetValue(const std::vector<int64_t> &values) -> StatusCode; + auto SetValue(const std::vector<double> &values) -> StatusCode; + auto SetValue(const std::vector<bool> &values) -> StatusCode; + auto SetValue(const int64_t value) -> StatusCode; + auto SetValue(const bool value) -> StatusCode; + /** * Set the unit of this property. */ @@ -688,10 +697,15 @@ public: auto SetValue(const Value &value) -> StatusCode; auto SetValue(const std::string &value) -> StatusCode; + auto SetValue(const char *value) -> StatusCode; auto SetValue(const double value) -> StatusCode; - // TODO(tf) - // auto SetValue(const int64_t value) -> StatusCode; - // auto SetValue(const bool value) -> StatusCode; + auto SetValue(const std::vector<std::string> &values) -> StatusCode; + auto SetValue(const std::vector<char *> &values) -> StatusCode; + auto SetValue(const std::vector<int64_t> &values) -> StatusCode; + auto SetValue(const std::vector<double> &values) -> StatusCode; + auto SetValue(const std::vector<bool> &values) -> StatusCode; + auto SetValue(const int64_t value) -> StatusCode; + auto SetValue(const bool value) -> StatusCode; auto SetUnit(const std::string &unit) -> void; diff --git a/include/caosdb/value.h b/include/caosdb/value.h index b11246181bb0c8bdd477f144e7ded06b8571aca8..30a2bec91de6dcec15d6f1f0cf2f78b77a500300 100644 --- a/include/caosdb/value.h +++ b/include/caosdb/value.h @@ -111,7 +111,9 @@ public: : ProtoMessageWrapper<ProtoValue>() { this->wrapped->mutable_scalar_value()->set_string_value(value); } - explicit inline Value(const char value[]) : Value(std::string(value)) {} + explicit inline Value(const char *value) : ProtoMessageWrapper<ProtoValue>() { + this->wrapped->mutable_scalar_value()->set_string_value(std::string(value)); + } explicit inline Value(double value) : ProtoMessageWrapper<ProtoValue>() { this->wrapped->mutable_scalar_value()->set_double_value(value); } @@ -123,16 +125,11 @@ public: this->wrapped->mutable_scalar_value()->set_boolean_value(value); } - explicit inline Value(const std::vector<std::string> &values) - : ProtoMessageWrapper<ProtoValue>() { - for (const auto &value : values) { - this->wrapped->mutable_list_values()->add_values()->set_string_value( - value); - } - } - + LIST_VALUE_CONSTRUCTOR(int, set_integer_value) LIST_VALUE_CONSTRUCTOR(int64_t, set_integer_value) LIST_VALUE_CONSTRUCTOR(double, set_double_value) + LIST_VALUE_CONSTRUCTOR(std::string, set_string_value) + LIST_VALUE_CONSTRUCTOR(char *, set_string_value) LIST_VALUE_CONSTRUCTOR(bool, set_boolean_value) [[nodiscard]] inline auto IsNull() -> bool { diff --git a/src/caosdb/entity.cpp b/src/caosdb/entity.cpp index e1fcc5f3c148414ca57718ecbd7201ee0b2323a4..dcf121683c390287d452a8ab3527cb77a8d16b86 100644 --- a/src/caosdb/entity.cpp +++ b/src/caosdb/entity.cpp @@ -130,10 +130,42 @@ auto Property::SetValue(const std::string &value) -> StatusCode { return SetValue(Value(value)); } +auto Property::SetValue(const char *value) -> StatusCode { + return SetValue(Value(value)); +} + auto Property::SetValue(double value) -> StatusCode { return SetValue(Value(value)); } +auto Property::SetValue(const std::vector<std::string> &values) -> StatusCode { + return SetValue(Value(values)); +} + +auto Property::SetValue(const std::vector<char *> &values) -> StatusCode { + return SetValue(Value(values)); +} + +auto Property::SetValue(const std::vector<int64_t> &values) -> StatusCode { + return SetValue(Value(values)); +} + +auto Property::SetValue(const std::vector<double> &values) -> StatusCode { + return SetValue(Value(values)); +} + +auto Property::SetValue(const std::vector<bool> &values) -> StatusCode { + return SetValue(Value(values)); +} + +auto Property::SetValue(const int64_t value) -> StatusCode { + return SetValue(Value(value)); +} + +auto Property::SetValue(const bool value) -> StatusCode { + return SetValue(Value(value)); +} + auto Property::SetUnit(const std::string &unit) -> void { this->wrapped->set_unit(unit); } @@ -223,10 +255,42 @@ auto Entity::SetValue(const std::string &value) -> StatusCode { return SetValue(Value(value)); } +auto Entity::SetValue(const char *value) -> StatusCode { + return SetValue(Value(value)); +} + auto Entity::SetValue(double value) -> StatusCode { return SetValue(Value(value)); } +auto Entity::SetValue(const std::vector<std::string> &values) -> StatusCode { + return SetValue(Value(values)); +} + +auto Entity::SetValue(const std::vector<char *> &values) -> StatusCode { + return SetValue(Value(values)); +} + +auto Entity::SetValue(const std::vector<int64_t> &values) -> StatusCode { + return SetValue(Value(values)); +} + +auto Entity::SetValue(const std::vector<double> &values) -> StatusCode { + return SetValue(Value(values)); +} + +auto Entity::SetValue(const std::vector<bool> &values) -> StatusCode { + return SetValue(Value(values)); +} + +auto Entity::SetValue(const int64_t value) -> StatusCode { + return SetValue(Value(value)); +} + +auto Entity::SetValue(const bool value) -> StatusCode { + return SetValue(Value(value)); +} + auto Entity::SetUnit(const std::string &unit) -> void { this->wrapped->set_unit(unit); } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 53c2f4878562f7817de5d1f649a35f51b492a6c3..63b696a2b602fcbdf128ff478073bc680ef170de 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -26,6 +26,7 @@ set(test_cases test_entity test_file_transmission test_info + test_list_properties test_protobuf test_transaction test_utility diff --git a/test/test_list_properties.cpp b/test/test_list_properties.cpp new file mode 100644 index 0000000000000000000000000000000000000000..34895dfa15eabc207c2a485f4a22fcb33428a791 --- /dev/null +++ b/test/test_list_properties.cpp @@ -0,0 +1,64 @@ +/* + * + * This file is a part of the CaosDB Project. + * + * Copyright (C) 2021 Timm Fitschen <t.fitschen@indiscale.com> + * Copyright (C) 2021 IndiScale GmbH <info@indiscale.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + * + */ + +#include "caosdb/data_type.h" // for DataType, AtomicDataType +#include "caosdb/entity.h" // for Entity +#include "caosdb/entity/v1alpha1/main.pb.h" // for AtomicDataType, DataType +#include "caosdb/value.h" // for Value +#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 <memory> // for allocator_traits<>::valu... +#include <string> // for string +#include <vector> // for vector + +namespace caosdb::entity { +using ProtoEntity = caosdb::entity::v1alpha1::Entity; +using ProtoParent = caosdb::entity::v1alpha1::Parent; +using ProtoDataType = caosdb::entity::v1alpha1::DataType; +using ProtoAtomicDataType = caosdb::entity::v1alpha1::AtomicDataType; + +TEST(test_list_property, test_list_of_text) { + Property list_property; + list_property.SetDataType(DataType::ListOf(AtomicDataType::TEXT)); + std::vector<std::string> in_value{"item1", "item2", "item3"}; + list_property.SetValue(in_value); + + Entity entity; + entity.SetRole(Role::RECORD_TYPE); + entity.SetName("TestRT"); + entity.AppendProperty(list_property); + + const auto &data_type = entity.GetProperties().at(0).GetDataType(); + const auto &value = entity.GetProperties().at(0).GetValue(); + + EXPECT_TRUE(data_type.IsList()); + EXPECT_TRUE(data_type.AsList().IsListOfAtomic()); + EXPECT_EQ(data_type.AsList().GetAtomicDataType(), AtomicDataType::TEXT); + + EXPECT_TRUE(value.IsList()); + EXPECT_EQ(value.AsList().size(), 3); + EXPECT_TRUE(value.AsList().at(1).IsString()); + EXPECT_EQ(value.AsList().at(1).AsString(), "item2"); +} + +} // namespace caosdb::entity diff --git a/test/test_value.cpp b/test/test_value.cpp index 39a6ac4a0b7f5eadb292dbcf7b3447061892b579..0bbc645397ede57fcbcecb9243085f42120cbebb 100644 --- a/test/test_value.cpp +++ b/test/test_value.cpp @@ -48,7 +48,7 @@ TEST(test_value, test_null) { } TEST(test_value, test_string) { - Value value("test"); + Value value(std::string("test")); EXPECT_FALSE(value.IsNull()); EXPECT_TRUE(value.IsString()); EXPECT_FALSE(value.IsDouble()); @@ -57,7 +57,7 @@ TEST(test_value, test_string) { EXPECT_EQ(value.AsString(), "test"); - Value empty_string(""); + Value empty_string(std::string("")); EXPECT_FALSE(empty_string.IsNull()); EXPECT_TRUE(empty_string.IsString()); EXPECT_FALSE(empty_string.IsDouble());