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

ENH: Convenient list constructor for DataType.

parent 3ce3fae9
Branches
Tags
2 merge requests!14ENH: New functions getEnumNameFromValue() and getEnumValueFromName(),!12F consolidation
Pipeline #12259 failed
...@@ -155,12 +155,26 @@ public: ...@@ -155,12 +155,26 @@ public:
DataType(ProtoDataType *wrapped) DataType(ProtoDataType *wrapped)
: ProtoMessageWrapper<ProtoDataType>(wrapped) {} : ProtoMessageWrapper<ProtoDataType>(wrapped) {}
DataType() : ProtoMessageWrapper<ProtoDataType>() {} DataType() : ProtoMessageWrapper<ProtoDataType>() {}
DataType(AtomicDataType data_type) : DataType() { /**
this->wrapped->set_atomic_data_type( * Create an AtomicDataType typed DataType. For references, use the std::string constructor.
static_cast<ProtoAtomicDataType>(data_type)); */
DataType(AtomicDataType data_type, bool list_type = false) : DataType() {
if (list_type) {
this->wrapped->mutable_list_data_type()->set_atomic_data_type
(static_cast<ProtoAtomicDataType>(data_type));
} else {
this->wrapped->set_atomic_data_type(static_cast<ProtoAtomicDataType>(data_type));
}
} }
DataType(const std::string &data_type) : DataType() { /**
this->wrapped->mutable_reference_data_type()->set_name(data_type); * Create a reference typed DataType.
*/
DataType(const std::string &data_type, bool list_type = false) : DataType() {
if (list_type) {
this->wrapped->mutable_list_data_type()->mutable_reference_data_type()->set_name(data_type);
} else {
this->wrapped->mutable_reference_data_type()->set_name(data_type);
}
} }
[[nodiscard]] inline auto IsAtomic() const noexcept -> bool { [[nodiscard]] inline auto IsAtomic() const noexcept -> bool {
return this->wrapped->data_type_case() == DataTypeCase::kAtomicDataType; return this->wrapped->data_type_case() == DataTypeCase::kAtomicDataType;
......
...@@ -22,9 +22,7 @@ ...@@ -22,9 +22,7 @@
#include "caosdb/data_type.h" // for DataType, AtomicDataType #include "caosdb/data_type.h" // for DataType, AtomicDataType
#include "caosdb/entity.h" // for Entity #include "caosdb/entity.h" // for Entity
#include "caosdb/entity/v1alpha1/main.pb.h" // for AtomicDataType, DataType
#include "caosdb/logging.h" // for CAOSDB_LOG_DEBUG #include "caosdb/logging.h" // for CAOSDB_LOG_DEBUG
#include "caosdb/protobuf_helper.h" // for CAOSDB_DEBUG_...
#include <boost/log/core/record.hpp> // for record #include <boost/log/core/record.hpp> // for record
#include <boost/log/detail/attachable_sstream_buf.hpp> // for basic_ostring... #include <boost/log/detail/attachable_sstream_buf.hpp> // for basic_ostring...
#include <boost/log/sources/record_ostream.hpp> // for operator<< #include <boost/log/sources/record_ostream.hpp> // for operator<<
...@@ -45,15 +43,15 @@ using ProtoAtomicDataType = caosdb::entity::v1alpha1::AtomicDataType; ...@@ -45,15 +43,15 @@ using ProtoAtomicDataType = caosdb::entity::v1alpha1::AtomicDataType;
TEST(test_data_type, test_atomic) { TEST(test_data_type, test_atomic) {
ProtoDataType proto_data_type; ProtoDataType proto_data_type;
for (int i = 1; i < 6; i++) { for (auto map_el : atomicdatatype_names) {
Entity entity; Entity entity;
entity.SetRole(Role::PROPERTY); entity.SetRole(Role::PROPERTY);
// the different AtomicDataType are associated with integers // the different AtomicDataType are associated with integers
entity.SetDataType(static_cast<AtomicDataType>(i)); entity.SetDataType(map_el.first);
EXPECT_TRUE(entity.GetDataType().IsAtomic()); EXPECT_TRUE(entity.GetDataType().IsAtomic());
EXPECT_EQ(entity.GetDataType().AsAtomic(), static_cast<AtomicDataType>(i)); EXPECT_EQ(entity.GetDataType().AsAtomic(), map_el.first);
proto_data_type.set_atomic_data_type(static_cast<ProtoAtomicDataType>(i)); proto_data_type.set_atomic_data_type(static_cast<ProtoAtomicDataType>(map_el.first));
DataType data_type(&proto_data_type); DataType data_type(&proto_data_type);
entity.SetDataType(data_type); entity.SetDataType(data_type);
...@@ -61,7 +59,7 @@ TEST(test_data_type, test_atomic) { ...@@ -61,7 +59,7 @@ TEST(test_data_type, test_atomic) {
EXPECT_EQ(data_type.AsReference().GetName(), std::basic_string<char>("")); EXPECT_EQ(data_type.AsReference().GetName(), std::basic_string<char>(""));
EXPECT_FALSE(data_type.IsList()); EXPECT_FALSE(data_type.IsList());
EXPECT_TRUE(data_type.IsAtomic()); EXPECT_TRUE(data_type.IsAtomic());
EXPECT_EQ(data_type.AsAtomic(), static_cast<AtomicDataType>(i)); EXPECT_EQ(data_type.AsAtomic(), map_el.first);
} }
} }
...@@ -85,13 +83,8 @@ TEST(test_data_type, test_reference) { ...@@ -85,13 +83,8 @@ TEST(test_data_type, test_reference) {
} }
TEST(test_data_type, test_list_of_atomic) { TEST(test_data_type, test_list_of_atomic) {
ProtoDataType proto_data_type; for (auto map_el : atomicdatatype_names) {
auto *proto_list_data_type = proto_data_type.mutable_list_data_type(); DataType data_type(map_el.first, true);
for (int i = 1; i < 6; i++) {
proto_list_data_type->set_atomic_data_type(
static_cast<ProtoAtomicDataType>(i));
DataType data_type(&proto_data_type);
EXPECT_FALSE(data_type.IsReference()); EXPECT_FALSE(data_type.IsReference());
EXPECT_FALSE(data_type.IsAtomic()); EXPECT_FALSE(data_type.IsAtomic());
...@@ -101,17 +94,12 @@ TEST(test_data_type, test_list_of_atomic) { ...@@ -101,17 +94,12 @@ TEST(test_data_type, test_list_of_atomic) {
std::basic_string<char>("")); std::basic_string<char>(""));
EXPECT_TRUE(list_data_type.IsListOfAtomic()); EXPECT_TRUE(list_data_type.IsListOfAtomic());
EXPECT_FALSE(list_data_type.IsListOfReference()); EXPECT_FALSE(list_data_type.IsListOfReference());
EXPECT_EQ(list_data_type.GetAtomicDataType(), EXPECT_EQ(list_data_type.GetAtomicDataType(), map_el.first);
static_cast<AtomicDataType>(i));
} }
} }
TEST(test_data_type, test_list_of_reference) { TEST(test_data_type, test_list_of_reference) {
ProtoDataType proto_data_type; auto data_type = DataType("person", true);
auto *proto_list_data_type = proto_data_type.mutable_list_data_type();
proto_list_data_type->mutable_reference_data_type()->set_name("person");
DataType data_type(&proto_data_type);
EXPECT_FALSE(data_type.IsReference()); EXPECT_FALSE(data_type.IsReference());
EXPECT_FALSE(data_type.IsAtomic()); EXPECT_FALSE(data_type.IsAtomic());
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment