diff --git a/include/caosdb/entity.h b/include/caosdb/entity.h index 7aa63fec21569c68660a4e1c0cad71882f2cff3f..795087a23584d7814367584e56804cbcd77933a3 100644 --- a/include/caosdb/entity.h +++ b/include/caosdb/entity.h @@ -304,10 +304,9 @@ private: */ class Property { public: - explicit inline Property(ProtoProperty *wrapped) : wrapped(wrapped), data_type(nullptr), value(nullptr) { - data_type.wrapped = this->wrapped->mutable_data_type(); - value.wrapped = this->wrapped->mutable_value(); - }; + explicit inline Property(ProtoProperty *wrapped) + : wrapped(wrapped), data_type(DataType(wrapped->mutable_data_type())), + value(Value(wrapped->mutable_value())){}; Property(); /** @@ -449,7 +448,9 @@ class Entity { public: Entity(); inline Entity(const Entity &original) - : wrapped(CreateProtoEntity()), data_type(nullptr), value(nullptr) { + : wrapped(CreateProtoEntity()), + data_type(DataType(wrapped->mutable_data_type())), + value(Value(wrapped->mutable_value())) { this->wrapped->CopyFrom(*original.wrapped); data_type.wrapped = this->wrapped->mutable_data_type(); value.wrapped = this->wrapped->mutable_value(); @@ -460,8 +461,11 @@ public: infos.wrapped = CreateMessagesField(); }; explicit Entity(IdResponse *id_response); - explicit Entity(ProtoEntity *wrapped) : wrapped(wrapped), data_type(nullptr) { + explicit Entity(ProtoEntity *wrapped) + : wrapped(wrapped), data_type(DataType(wrapped->mutable_data_type())), + value(Value(wrapped->mutable_value())) { data_type.wrapped = this->wrapped->mutable_data_type(); + value.wrapped = this->wrapped->mutable_value(); properties.wrapped = this->wrapped->mutable_properties(); parents.wrapped = this->wrapped->mutable_parents(); errors.wrapped = CreateMessagesField(); diff --git a/include/caosdb/transaction.h b/include/caosdb/transaction.h index 49a76ab784b3ccd207f78e1bd76d6479920cc081..c6155ae311da8d8c5b603d9b5ed37c359773db0d 100644 --- a/include/caosdb/transaction.h +++ b/include/caosdb/transaction.h @@ -34,8 +34,8 @@ #include <boost/log/sources/record_ostream.hpp> // for basic_record_o... #include <boost/preprocessor/seq/limits/enum_256.hpp> // for BOOST_PP_SEQ_E... #include <boost/preprocessor/seq/limits/size_256.hpp> // for BOOST_PP_SEQ_S... -#include <google/protobuf/generated_message_util.h> // for CreateMessage... #include <google/protobuf/arena.h> // for Arena +#include <google/protobuf/generated_message_util.h> // for CreateMessage... #include <google/protobuf/util/json_util.h> // for MessageToJsonS... #include <grpcpp/impl/codegen/completion_queue.h> // for CompletionQueue #include <iterator> // for iterator, next diff --git a/include/caosdb/value.h b/include/caosdb/value.h index 231fc84c6d6423e63f3f6e7989b9b5a683bd4667..63bb7045bf8f62c31634b75938a122f80c522ed0 100644 --- a/include/caosdb/value.h +++ b/include/caosdb/value.h @@ -23,6 +23,8 @@ #define CAOSDB_VALUE_H #include "caosdb/protobuf_helper.h" // for ProtoMessageWrapper #include "caosdb/entity/v1alpha1/main.pb.h" // for RepeatedPtrField, Message + +#include <google/protobuf/util/json_util.h> // for MessageToJson... #include <memory> // for unique_ptr #include <string> // for string #include <vector> // for vector @@ -210,6 +212,11 @@ public: other.wrapped->SerializeAsString(); } + inline auto ToString() const noexcept -> const std::string { + CAOSDB_DEBUG_MESSAGE_STRING(*wrapped, out) + return out; + } + friend class Entity; friend class Property; diff --git a/src/caosdb/entity.cpp b/src/caosdb/entity.cpp index 0bcde15dbdf6bd82c7ce8a52d45c2ecae8ff4a5a..0c6253177526e74f75a64ed82fd8ab2bdef93bd9 100644 --- a/src/caosdb/entity.cpp +++ b/src/caosdb/entity.cpp @@ -209,7 +209,14 @@ auto Entity::SetVersionId(const std::string &id) -> void { } auto Entity::CopyTo(ProtoEntity *target) -> void { + CAOSDB_DEBUG_MESSAGE_STRING(*(this->wrapped), out); + CAOSDB_LOG_DEBUG(logger_name) << "COPY ENTITY: " << out; + CAOSDB_DEBUG_MESSAGE_STRING(*target, out_2); + CAOSDB_LOG_DEBUG(logger_name) << "COPY ENTITY TO: " << out_2; + target->Clear(); target->CopyFrom(*(this->wrapped)); + CAOSDB_DEBUG_MESSAGE_STRING(*target, out_3); + CAOSDB_LOG_DEBUG(logger_name) << "COPY ENTITY FINAL: " << out_3; } auto Entity::SetRole(Role role) -> void { diff --git a/src/caosdb/transaction.cpp b/src/caosdb/transaction.cpp index 2bb5b34f8ad0ec8a79b62c730c3d68ab50bdf188..271f305e300b57292c46a1bd1a05dc8ec15dd495 100644 --- a/src/caosdb/transaction.cpp +++ b/src/caosdb/transaction.cpp @@ -24,7 +24,8 @@ #include "caosdb/file_transmission/file_reader.h" // for path #include "caosdb/file_transmission/register_file_upload_handler.h" #include "caosdb/file_transmission/upload_request_handler.h" // Upload... -#include "caosdb/logging.h" // for CAOSDB_LOG_FATAL +#include "caosdb/logging.h" // for CAOSDB_LOG_FATAL +#include "caosdb/protobuf_helper.h" #include "caosdb/status_code.h" // for StatusCode #include "caosdb/transaction_handler.h" #include <algorithm> // for max @@ -35,8 +36,10 @@ #include <boost/preprocessor/seq/limits/enum_256.hpp> // for BOOST_PP_SEQ_... #include <boost/preprocessor/seq/limits/size_256.hpp> // for BOOST_PP_SEQ_... // IWYU pragma: no_include <bits/exception.h> -#include <exception> // IWYU pragma: keep -#include <google/protobuf/arena.h> // for Arena +#include <exception> // IWYU pragma: keep +#include <google/protobuf/arena.h> // for Arena +#include <google/protobuf/generated_message_util.h> // for CreateMessage... +#include <google/protobuf/util/json_util.h> #include <grpc/impl/codegen/gpr_types.h> #include <grpcpp/impl/codegen/completion_queue.h> // for CompletionQueue #include <iosfwd> // for streamsize @@ -222,6 +225,8 @@ auto Transaction::InsertEntity(Entity *entity) noexcept -> StatusCode { // copy the original entity for the transaction entity->CopyTo(proto_entity); + CAOSDB_DEBUG_MESSAGE_STRING(*proto_entity, out); + CAOSDB_LOG_DEBUG(logger_name) << "INSERT ENTITY: " << out; if (entity->HasFile()) { auto *file_transmission_id = entity_request->mutable_upload_id(); entity->SetFileTransmissionId(file_transmission_id); diff --git a/test/test_entity.cpp b/test/test_entity.cpp index 4db2b1bdb90350a8b5acbd93790b3b5c047e03d8..25e192b548196e70a585d84c56d3cc2b9020c684 100644 --- a/test/test_entity.cpp +++ b/test/test_entity.cpp @@ -71,38 +71,22 @@ TEST(test_entity, test_append_parent) { } TEST(test_entity, test_property_setters) { - CAOSDB_LOG_DEBUG(logger_name) << "HERE 1"; auto prop = Property(); - CAOSDB_LOG_DEBUG(logger_name) << "HERE 2"; prop.SetName("prop_name"); - CAOSDB_LOG_DEBUG(logger_name) << "HERE 3"; prop.SetId("prop_id"); - CAOSDB_LOG_DEBUG(logger_name) << "HERE 4"; prop.SetImportance(Importance::OBLIGATORY); - CAOSDB_LOG_DEBUG(logger_name) << "HERE 5"; prop.SetValue("prop_value"); - CAOSDB_LOG_DEBUG(logger_name) << "HERE 6"; prop.SetUnit("prop_unit"); - CAOSDB_LOG_DEBUG(logger_name) << "HERE 7"; prop.SetDataType("prop_dtype"); - CAOSDB_LOG_DEBUG(logger_name) << "HERE 8"; EXPECT_EQ(prop.GetName(), "prop_name"); - CAOSDB_LOG_DEBUG(logger_name) << "HERE 1"; EXPECT_EQ(prop.GetId(), "prop_id"); - CAOSDB_LOG_DEBUG(logger_name) << "HERE 2"; EXPECT_EQ(prop.GetImportance(), Importance::OBLIGATORY); - CAOSDB_LOG_DEBUG(logger_name) << "HERE 3"; EXPECT_TRUE(prop.GetValue().IsString()); - CAOSDB_LOG_DEBUG(logger_name) << "HERE 4"; EXPECT_EQ(prop.GetValue().AsString(), "prop_value"); - CAOSDB_LOG_DEBUG(logger_name) << "HERE 5"; EXPECT_EQ(prop.GetUnit(), "prop_unit"); - CAOSDB_LOG_DEBUG(logger_name) << "HERE 6"; EXPECT_TRUE(prop.GetDataType().IsReference()); - CAOSDB_LOG_DEBUG(logger_name) << "HERE 7"; EXPECT_EQ(prop.GetDataType().AsReference().GetName(), "prop_dtype"); - CAOSDB_LOG_DEBUG(logger_name) << "HERE 8"; } TEST(test_entity, test_append_property) { @@ -238,7 +222,7 @@ TEST(test_entity, test_insert_with_property) { prop.SetName("prop_name"); prop.SetId("prop_id"); prop.SetImportance(Importance::FIX); - prop.SetValue("prop_value"); + prop.SetValue(Value("prop_value")); prop.SetUnit("prop_unit"); prop.SetDataType("prop_dtype"); @@ -253,6 +237,7 @@ TEST(test_entity, test_insert_with_property) { EXPECT_EQ(prop.GetName(), inserted_prop.GetName()); EXPECT_EQ(prop.GetId(), inserted_prop.GetId()); EXPECT_EQ(prop.GetImportance(), inserted_prop.GetImportance()); + EXPECT_EQ(prop.GetValue().ToString(), inserted_prop.GetValue().ToString()); EXPECT_EQ(prop.GetValue(), inserted_prop.GetValue()); EXPECT_EQ(prop.GetUnit(), inserted_prop.GetUnit()); EXPECT_EQ(prop.GetDataType(), inserted_prop.GetDataType()); diff --git a/test/test_protobuf.cpp b/test/test_protobuf.cpp index 9dc957a2f85cd95ce3ead5f59ab8c10868582003..1fa8911785dd96ace73f3f5f8813b42fa4d31829 100644 --- a/test/test_protobuf.cpp +++ b/test/test_protobuf.cpp @@ -20,13 +20,15 @@ * */ #include "caosdb/entity/v1alpha1/main.pb.h" // for RepeatedPtrField, Message -#include "gtest/gtest-message.h" // for Message -#include "gtest/gtest-test-part.h" // for SuiteApiResolver, TestPa... -#include "gtest/gtest_pred_impl.h" // for Test, TestInfo, TEST -#include <memory> // for allocator +#include "caosdb/entity.h" +#include "gtest/gtest-message.h" // for Message +#include "gtest/gtest-test-part.h" // for SuiteApiResolver, TestPa... +#include "gtest/gtest_pred_impl.h" // for Test, TestInfo, TEST +#include <memory> // for allocator namespace caosdb { -using caosdb::entity::v1alpha1::Entity; +using ProtoEntity = caosdb::entity::v1alpha1::Entity; +using caosdb::entity::Entity; using caosdb::entity::v1alpha1::Message; TEST(test_protobuf, test_swap_trivial) { @@ -50,12 +52,12 @@ TEST(test_protobuf, test_swap_trivial) { } TEST(test_protobuf, test_swap_nested) { - Entity entity_source; + ProtoEntity entity_source; entity_source.set_id("entity_id"); auto *version_source = entity_source.mutable_version(); version_source->set_id("version_id"); - Entity entity_destination; + ProtoEntity entity_destination; auto *version_destination = entity_destination.mutable_version(); EXPECT_EQ(entity_source.id(), "entity_id"); @@ -81,4 +83,32 @@ TEST(test_protobuf, test_swap_nested) { EXPECT_EQ(entity_destination.mutable_version(), version_source); } +TEST(test_protobuf, test_copy_nested) { + ProtoEntity entity_source; + auto *data_type_source = entity_source.mutable_data_type(); + data_type_source->mutable_reference_data_type()->set_name("src_per"); + + ProtoEntity entity_destination; + auto *data_type_destination = entity_destination.mutable_data_type(); + data_type_destination->mutable_reference_data_type()->set_name("dest_per"); + + EXPECT_EQ(entity_source.data_type().reference_data_type().name(), "src_per"); + EXPECT_EQ(entity_destination.data_type().reference_data_type().name(), + "dest_per"); + + entity_destination.CopyFrom(entity_source); + + EXPECT_EQ(entity_source.data_type().reference_data_type().name(), "src_per"); + EXPECT_EQ(entity_destination.data_type().reference_data_type().name(), + "src_per"); + + Entity entity(&entity_destination); + EXPECT_EQ(entity.GetDataType().AsReference().GetName(), "src_per"); + EXPECT_EQ(entity.ToString(), ""); + + Entity copy_entity(entity); + EXPECT_EQ(copy_entity.GetDataType().AsReference().GetName(), "src_per"); + EXPECT_EQ(copy_entity.ToString(), ""); +} + } // namespace caosdb