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

STY: Mostly formatting.

parent b2d07890
Branches
Tags
1 merge request!16Workaround to return non-string values, according to the specified DataType.
Pipeline #12505 failed
...@@ -454,7 +454,7 @@ class Property { ...@@ -454,7 +454,7 @@ class Property {
public: public:
explicit inline Property(ProtoProperty *other) explicit inline Property(ProtoProperty *other)
: value(Value(other->mutable_value())), data_type(DataType(other->mutable_data_type())), : value(Value(other->mutable_value())), data_type(DataType(other->mutable_data_type())),
wrapped(other){ wrapped(other) {
FixValue(); FixValue();
}; };
Property(); Property();
...@@ -553,7 +553,6 @@ public: ...@@ -553,7 +553,6 @@ public:
friend class RepeatedPtrFieldWrapper<Property, ProtoProperty>; friend class RepeatedPtrFieldWrapper<Property, ProtoProperty>;
private: private:
/** /**
* Workaround until non-string values are supported by the server. * Workaround until non-string values are supported by the server.
* *
......
...@@ -21,10 +21,10 @@ ...@@ -21,10 +21,10 @@
*/ */
#include "caosdb/entity.h" #include "caosdb/entity.h"
#include "caosdb/exceptions.h" #include "caosdb/exceptions.h"
#include "caosdb/data_type.h" // for DataType #include "caosdb/data_type.h" // for DataType
#include "caosdb/entity/v1alpha1/main.pb.h" // for Messages #include "caosdb/entity/v1alpha1/main.pb.h" // for Messages
#include "caosdb/protobuf_helper.h" // for get_arena #include "caosdb/protobuf_helper.h" // for get_arena
#include "caosdb/value.h" // for Value #include "caosdb/value.h" // for Value
#include <boost/algorithm/string.hpp> #include <boost/algorithm/string.hpp>
#include <google/protobuf/arena.h> // for Arena #include <google/protobuf/arena.h> // for Arena
#include <google/protobuf/generated_message_util.h> // for Arena::Create... #include <google/protobuf/generated_message_util.h> // for Arena::Create...
...@@ -46,8 +46,7 @@ Messages::~Messages() = default; ...@@ -46,8 +46,7 @@ Messages::~Messages() = default;
// Forward declarations /////////////////////////////////////////////////////// // Forward declarations ///////////////////////////////////////////////////////
template<typename E> template <typename E> auto FixValueImpl(E *ent) -> void;
auto FixValueImpl(E* ent) -> void;
// Parent ///////////////////////////////////////////////////////////////////// // Parent /////////////////////////////////////////////////////////////////////
...@@ -75,9 +74,7 @@ auto Parent::SetId(const std::string &id) -> void { this->wrapped->set_id(id); } ...@@ -75,9 +74,7 @@ auto Parent::SetId(const std::string &id) -> void { this->wrapped->set_id(id); }
return this->wrapped->description(); return this->wrapped->description();
} }
Property::Property() : Property(Property::CreateProtoProperty()) { Property::Property() : Property(Property::CreateProtoProperty()) { FixValue(); }
FixValue();
}
auto Property::CreateProtoProperty() -> ProtoProperty * { auto Property::CreateProtoProperty() -> ProtoProperty * {
return Arena::CreateMessage<ProtoProperty>(get_arena()); return Arena::CreateMessage<ProtoProperty>(get_arena());
...@@ -163,9 +160,7 @@ auto Property::SetDataType(const std::string &new_data_type, bool list_type) -> ...@@ -163,9 +160,7 @@ auto Property::SetDataType(const std::string &new_data_type, bool list_type) ->
return SetDataType(DataType(new_data_type, list_type)); return SetDataType(DataType(new_data_type, list_type));
} }
auto Property::FixValue() -> void { auto Property::FixValue() -> void { FixValueImpl(this); }
FixValueImpl(this);
}
// Entity ///////////////////////////////////////////////////////////////////// // Entity /////////////////////////////////////////////////////////////////////
[[nodiscard]] auto Entity::GetParents() const -> const Parents & { return parents; } [[nodiscard]] auto Entity::GetParents() const -> const Parents & { return parents; }
...@@ -193,9 +188,7 @@ Entity::Entity(IdResponse *id_response) : Entity() { ...@@ -193,9 +188,7 @@ Entity::Entity(IdResponse *id_response) : Entity() {
FixValue(); FixValue();
} }
Entity::Entity() : Entity(Entity::CreateProtoEntity()) { Entity::Entity() : Entity(Entity::CreateProtoEntity()) { FixValue(); }
FixValue();
}
auto Entity::CreateMessagesField() -> RepeatedPtrField<ProtoMessage> * { auto Entity::CreateMessagesField() -> RepeatedPtrField<ProtoMessage> * {
return Arena::CreateMessage<RepeatedPtrField<ProtoMessage>>(get_arena()); return Arena::CreateMessage<RepeatedPtrField<ProtoMessage>>(get_arena());
...@@ -275,56 +268,52 @@ auto Entity::SetFilePath(const std::string &path) -> void { ...@@ -275,56 +268,52 @@ auto Entity::SetFilePath(const std::string &path) -> void {
this->wrapped->mutable_file_descriptor()->set_path(path); this->wrapped->mutable_file_descriptor()->set_path(path);
} }
auto Entity::FixValue() -> void { auto Entity::FixValue() -> void { FixValueImpl(this); }
FixValueImpl(this);
}
// Utility functions ////////////////////////////////////////////////////////// // Utility functions //////////////////////////////////////////////////////////
template<typename E> template <typename E> auto FixValueImpl(E *ent) -> void {
auto FixValueImpl(E* ent) -> void {
const auto &dtype = ent->GetDataType(); const auto &dtype = ent->GetDataType();
const auto &value = ent->GetValue(); const auto &value = ent->GetValue();
auto new_value = Value(); auto new_value = Value();
if (value.IsNull() || ! value.IsString()){ // Don't treat NULL and non-string values. if (value.IsNull() || !value.IsString()) { // Don't treat NULL and non-string values.
return; return;
} }
if (value.IsList()) { // Also don't treat empty or non-string lists. if (value.IsList()) { // Also don't treat empty or non-string lists.
const auto &list = value.AsList(); const auto &list = value.AsList();
if (list.empty() || ! list[0].IsString()) { if (list.empty() || !list[0].IsString()) {
return; return;
} }
} }
auto atype = AtomicDataType::UNSPECIFIED; auto atype = AtomicDataType::UNSPECIFIED;
if (dtype.IsList()) { // List Datatype if (dtype.IsList()) { // List Datatype
if (!value.IsList()) { if (!value.IsList()) {
throw caosdb::exceptions::Exception(StatusCode::OTHER_CLIENT_ERROR, throw caosdb::exceptions::Exception(StatusCode::OTHER_CLIENT_ERROR,
"DataType is list, but Value is scalar."); "DataType is list, but Value is scalar.");
} }
auto &list_type = dtype.AsList(); auto &list_type = dtype.AsList();
atype = list_type.GetAtomicDataType(); atype = list_type.GetAtomicDataType();
if (!list_type.IsListOfAtomic() // References, strings etc. need no treatment. if (!list_type.IsListOfAtomic() // References, strings etc. need no treatment.
|| atype == AtomicDataType::UNSPECIFIED || atype == AtomicDataType::UNSPECIFIED || atype == AtomicDataType::TEXT ||
|| atype == AtomicDataType::TEXT atype == AtomicDataType::DATETIME) {
|| atype == AtomicDataType::DATETIME) {
return; return;
} }
if (atype == AtomicDataType::DOUBLE) { if (atype == AtomicDataType::DOUBLE) {
std::vector<double> data; std::vector<double> data;
for (auto &d: value.AsList()) { for (auto &d : value.AsList()) {
data.push_back(std::stod(d.AsString())); data.push_back(std::stod(d.AsString()));
} }
new_value = Value(data) ; new_value = Value(data);
} else if (atype == AtomicDataType::INTEGER) { } else if (atype == AtomicDataType::INTEGER) {
std::vector<long> data; std::vector<int64_t> data;
for (auto &d: value.AsList()) { for (auto &d : value.AsList()) {
data.push_back(std::stol(d.AsString())); data.push_back(std::stol(d.AsString()));
} }
new_value = Value(data) ; new_value = Value(data);
} else if (atype == AtomicDataType::BOOLEAN) { } else if (atype == AtomicDataType::BOOLEAN) {
std::vector<bool> data; std::vector<bool> data;
for (auto &d: value.AsList()) { for (auto &d : value.AsList()) {
auto bool_value = d.AsString(); const auto &bool_value = d.AsString();
if (boost::to_upper_copy(bool_value) == "TRUE") { if (boost::to_upper_copy(bool_value) == "TRUE") {
data.push_back(true); data.push_back(true);
} else if (boost::to_upper_copy(bool_value) == "FALSE") { } else if (boost::to_upper_copy(bool_value) == "FALSE") {
...@@ -334,21 +323,20 @@ auto FixValueImpl(E* ent) -> void { ...@@ -334,21 +323,20 @@ auto FixValueImpl(E* ent) -> void {
"Boolean value is neither true nor false."); "Boolean value is neither true nor false.");
} }
} }
new_value = Value(data) ; new_value = Value(data);
} else { } else {
std::cout << "Unhandled datatype: " << ent->ToString() << std::endl; std::cout << "Unhandled datatype: " << ent->ToString() << std::endl;
throw std::logic_error("Unhandled datatype"); throw std::logic_error("Unhandled datatype");
} }
} else { // Scalar Datatype } else { // Scalar Datatype
if (value.IsList()) { if (value.IsList()) {
throw caosdb::exceptions::Exception(StatusCode::OTHER_CLIENT_ERROR, throw caosdb::exceptions::Exception(StatusCode::OTHER_CLIENT_ERROR,
"Value is list, but DataType is scalar."); "Value is list, but DataType is scalar.");
} }
atype = dtype.AsAtomic(); atype = dtype.AsAtomic();
if (!dtype.IsAtomic() // References, strings etc. need no treatment. if (!dtype.IsAtomic() // References, strings etc. need no treatment.
|| atype == AtomicDataType::UNSPECIFIED || atype == AtomicDataType::UNSPECIFIED || atype == AtomicDataType::TEXT ||
|| atype == AtomicDataType::TEXT atype == AtomicDataType::DATETIME) {
|| atype == AtomicDataType::DATETIME) {
return; return;
} }
if (atype == AtomicDataType::DOUBLE) { if (atype == AtomicDataType::DOUBLE) {
...@@ -356,7 +344,7 @@ auto FixValueImpl(E* ent) -> void { ...@@ -356,7 +344,7 @@ auto FixValueImpl(E* ent) -> void {
} else if (atype == AtomicDataType::INTEGER) { } else if (atype == AtomicDataType::INTEGER) {
new_value = Value(std::stol(value.AsString())); new_value = Value(std::stol(value.AsString()));
} else if (atype == AtomicDataType::BOOLEAN) { } else if (atype == AtomicDataType::BOOLEAN) {
auto bool_value = value.AsString(); const auto &bool_value = value.AsString();
if (boost::to_upper_copy(bool_value) == "TRUE") { if (boost::to_upper_copy(bool_value) == "TRUE") {
new_value = Value(true); new_value = Value(true);
} else if (boost::to_upper_copy(bool_value) == "FALSE") { } else if (boost::to_upper_copy(bool_value) == "FALSE") {
...@@ -374,5 +362,4 @@ auto FixValueImpl(E* ent) -> void { ...@@ -374,5 +362,4 @@ auto FixValueImpl(E* ent) -> void {
ent->SetValue(new_value); ent->SetValue(new_value);
} }
} // namespace caosdb::entity } // namespace caosdb::entity
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment