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
No related branches found
No related tags found
2 merge requests!14ENH: New functions getEnumNameFromValue() and getEnumValueFromName(),!12F consolidation
Pipeline #12259 failed
......@@ -155,12 +155,26 @@ public:
DataType(ProtoDataType *wrapped)
: ProtoMessageWrapper<ProtoDataType>(wrapped) {}
DataType() : ProtoMessageWrapper<ProtoDataType>() {}
DataType(AtomicDataType data_type) : DataType() {
this->wrapped->set_atomic_data_type(
static_cast<ProtoAtomicDataType>(data_type));
/**
* Create an AtomicDataType typed DataType. For references, use the std::string constructor.
*/
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 {
return this->wrapped->data_type_case() == DataTypeCase::kAtomicDataType;
......
......@@ -22,9 +22,7 @@
#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/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/detail/attachable_sstream_buf.hpp> // for basic_ostring...
#include <boost/log/sources/record_ostream.hpp> // for operator<<
......@@ -45,15 +43,15 @@ using ProtoAtomicDataType = caosdb::entity::v1alpha1::AtomicDataType;
TEST(test_data_type, test_atomic) {
ProtoDataType proto_data_type;
for (int i = 1; i < 6; i++) {
for (auto map_el : atomicdatatype_names) {
Entity entity;
entity.SetRole(Role::PROPERTY);
// the different AtomicDataType are associated with integers
entity.SetDataType(static_cast<AtomicDataType>(i));
entity.SetDataType(map_el.first);
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);
entity.SetDataType(data_type);
......@@ -61,7 +59,7 @@ TEST(test_data_type, test_atomic) {
EXPECT_EQ(data_type.AsReference().GetName(), std::basic_string<char>(""));
EXPECT_FALSE(data_type.IsList());
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) {
}
TEST(test_data_type, test_list_of_atomic) {
ProtoDataType proto_data_type;
auto *proto_list_data_type = proto_data_type.mutable_list_data_type();
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);
for (auto map_el : atomicdatatype_names) {
DataType data_type(map_el.first, true);
EXPECT_FALSE(data_type.IsReference());
EXPECT_FALSE(data_type.IsAtomic());
......@@ -101,17 +94,12 @@ TEST(test_data_type, test_list_of_atomic) {
std::basic_string<char>(""));
EXPECT_TRUE(list_data_type.IsListOfAtomic());
EXPECT_FALSE(list_data_type.IsListOfReference());
EXPECT_EQ(list_data_type.GetAtomicDataType(),
static_cast<AtomicDataType>(i));
EXPECT_EQ(list_data_type.GetAtomicDataType(), map_el.first);
}
}
TEST(test_data_type, test_list_of_reference) {
ProtoDataType proto_data_type;
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);
auto data_type = DataType("person", true);
EXPECT_FALSE(data_type.IsReference());
EXPECT_FALSE(data_type.IsAtomic());
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment