From 49ea1cf7dd950c7da3f112f44f422488850ff861 Mon Sep 17 00:00:00 2001 From: Daniel <d.hornung@indiscale.com> Date: Tue, 24 Aug 2021 16:06:50 +0200 Subject: [PATCH] STY: Mostly formatting. --- include/caosdb/entity.h | 3 +- src/caosdb/entity.cpp | 71 +++++++++++++++++------------------------ 2 files changed, 30 insertions(+), 44 deletions(-) diff --git a/include/caosdb/entity.h b/include/caosdb/entity.h index f93c349..08a7be6 100644 --- a/include/caosdb/entity.h +++ b/include/caosdb/entity.h @@ -454,7 +454,7 @@ class Property { public: explicit inline Property(ProtoProperty *other) : value(Value(other->mutable_value())), data_type(DataType(other->mutable_data_type())), - wrapped(other){ + wrapped(other) { FixValue(); }; Property(); @@ -553,7 +553,6 @@ public: friend class RepeatedPtrFieldWrapper<Property, ProtoProperty>; private: - /** * Workaround until non-string values are supported by the server. * diff --git a/src/caosdb/entity.cpp b/src/caosdb/entity.cpp index e927f68..d7d957d 100644 --- a/src/caosdb/entity.cpp +++ b/src/caosdb/entity.cpp @@ -21,10 +21,10 @@ */ #include "caosdb/entity.h" #include "caosdb/exceptions.h" -#include "caosdb/data_type.h" // for DataType -#include "caosdb/entity/v1alpha1/main.pb.h" // for Messages -#include "caosdb/protobuf_helper.h" // for get_arena -#include "caosdb/value.h" // for Value +#include "caosdb/data_type.h" // for DataType +#include "caosdb/entity/v1alpha1/main.pb.h" // for Messages +#include "caosdb/protobuf_helper.h" // for get_arena +#include "caosdb/value.h" // for Value #include <boost/algorithm/string.hpp> #include <google/protobuf/arena.h> // for Arena #include <google/protobuf/generated_message_util.h> // for Arena::Create... @@ -46,8 +46,7 @@ Messages::~Messages() = default; // Forward declarations /////////////////////////////////////////////////////// -template<typename E> -auto FixValueImpl(E* ent) -> void; +template <typename E> auto FixValueImpl(E *ent) -> void; // Parent ///////////////////////////////////////////////////////////////////// @@ -75,9 +74,7 @@ auto Parent::SetId(const std::string &id) -> void { this->wrapped->set_id(id); } return this->wrapped->description(); } -Property::Property() : Property(Property::CreateProtoProperty()) { - FixValue(); -} +Property::Property() : Property(Property::CreateProtoProperty()) { FixValue(); } auto Property::CreateProtoProperty() -> ProtoProperty * { return Arena::CreateMessage<ProtoProperty>(get_arena()); @@ -163,9 +160,7 @@ auto Property::SetDataType(const std::string &new_data_type, bool list_type) -> return SetDataType(DataType(new_data_type, list_type)); } -auto Property::FixValue() -> void { - FixValueImpl(this); -} +auto Property::FixValue() -> void { FixValueImpl(this); } // Entity ///////////////////////////////////////////////////////////////////// [[nodiscard]] auto Entity::GetParents() const -> const Parents & { return parents; } @@ -193,9 +188,7 @@ Entity::Entity(IdResponse *id_response) : Entity() { FixValue(); } -Entity::Entity() : Entity(Entity::CreateProtoEntity()) { - FixValue(); -} +Entity::Entity() : Entity(Entity::CreateProtoEntity()) { FixValue(); } auto Entity::CreateMessagesField() -> RepeatedPtrField<ProtoMessage> * { return Arena::CreateMessage<RepeatedPtrField<ProtoMessage>>(get_arena()); @@ -275,56 +268,52 @@ auto Entity::SetFilePath(const std::string &path) -> void { this->wrapped->mutable_file_descriptor()->set_path(path); } -auto Entity::FixValue() -> void { - FixValueImpl(this); -} +auto Entity::FixValue() -> void { FixValueImpl(this); } // Utility functions ////////////////////////////////////////////////////////// -template<typename E> -auto FixValueImpl(E* ent) -> void { +template <typename E> auto FixValueImpl(E *ent) -> void { const auto &dtype = ent->GetDataType(); const auto &value = ent->GetValue(); 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; } if (value.IsList()) { // Also don't treat empty or non-string lists. const auto &list = value.AsList(); - if (list.empty() || ! list[0].IsString()) { + if (list.empty() || !list[0].IsString()) { return; } } auto atype = AtomicDataType::UNSPECIFIED; - if (dtype.IsList()) { // List Datatype + if (dtype.IsList()) { // List Datatype if (!value.IsList()) { throw caosdb::exceptions::Exception(StatusCode::OTHER_CLIENT_ERROR, "DataType is list, but Value is scalar."); } auto &list_type = dtype.AsList(); atype = list_type.GetAtomicDataType(); - if (!list_type.IsListOfAtomic() // References, strings etc. need no treatment. - || atype == AtomicDataType::UNSPECIFIED - || atype == AtomicDataType::TEXT - || atype == AtomicDataType::DATETIME) { + if (!list_type.IsListOfAtomic() // References, strings etc. need no treatment. + || atype == AtomicDataType::UNSPECIFIED || atype == AtomicDataType::TEXT || + atype == AtomicDataType::DATETIME) { return; } if (atype == AtomicDataType::DOUBLE) { std::vector<double> data; - for (auto &d: value.AsList()) { + for (auto &d : value.AsList()) { data.push_back(std::stod(d.AsString())); } - new_value = Value(data) ; + new_value = Value(data); } else if (atype == AtomicDataType::INTEGER) { - std::vector<long> data; - for (auto &d: value.AsList()) { + std::vector<int64_t> data; + for (auto &d : value.AsList()) { data.push_back(std::stol(d.AsString())); } - new_value = Value(data) ; + new_value = Value(data); } else if (atype == AtomicDataType::BOOLEAN) { std::vector<bool> data; - for (auto &d: value.AsList()) { - auto bool_value = d.AsString(); + for (auto &d : value.AsList()) { + const auto &bool_value = d.AsString(); if (boost::to_upper_copy(bool_value) == "TRUE") { data.push_back(true); } else if (boost::to_upper_copy(bool_value) == "FALSE") { @@ -334,21 +323,20 @@ auto FixValueImpl(E* ent) -> void { "Boolean value is neither true nor false."); } } - new_value = Value(data) ; + new_value = Value(data); } else { std::cout << "Unhandled datatype: " << ent->ToString() << std::endl; throw std::logic_error("Unhandled datatype"); } - } else { // Scalar Datatype + } else { // Scalar Datatype if (value.IsList()) { throw caosdb::exceptions::Exception(StatusCode::OTHER_CLIENT_ERROR, "Value is list, but DataType is scalar."); } atype = dtype.AsAtomic(); - if (!dtype.IsAtomic() // References, strings etc. need no treatment. - || atype == AtomicDataType::UNSPECIFIED - || atype == AtomicDataType::TEXT - || atype == AtomicDataType::DATETIME) { + if (!dtype.IsAtomic() // References, strings etc. need no treatment. + || atype == AtomicDataType::UNSPECIFIED || atype == AtomicDataType::TEXT || + atype == AtomicDataType::DATETIME) { return; } if (atype == AtomicDataType::DOUBLE) { @@ -356,7 +344,7 @@ auto FixValueImpl(E* ent) -> void { } else if (atype == AtomicDataType::INTEGER) { new_value = Value(std::stol(value.AsString())); } else if (atype == AtomicDataType::BOOLEAN) { - auto bool_value = value.AsString(); + const auto &bool_value = value.AsString(); if (boost::to_upper_copy(bool_value) == "TRUE") { new_value = Value(true); } else if (boost::to_upper_copy(bool_value) == "FALSE") { @@ -374,5 +362,4 @@ auto FixValueImpl(E* ent) -> void { ent->SetValue(new_value); } - } // namespace caosdb::entity -- GitLab