From 163860ce05ba1419c22dbb65bd1b0fae22a750c6 Mon Sep 17 00:00:00 2001
From: Timm Fitschen <t.fitschen@indiscale.com>
Date: Mon, 16 Aug 2021 15:28:13 +0200
Subject: [PATCH] WIP: consolidation

---
 include/caosdb/entity.h      | 16 ++++++++-----
 include/caosdb/transaction.h |  2 +-
 include/caosdb/value.h       |  7 ++++++
 src/caosdb/entity.cpp        |  7 ++++++
 src/caosdb/transaction.cpp   | 11 ++++++---
 test/test_entity.cpp         | 19 ++--------------
 test/test_protobuf.cpp       | 44 ++++++++++++++++++++++++++++++------
 7 files changed, 72 insertions(+), 34 deletions(-)

diff --git a/include/caosdb/entity.h b/include/caosdb/entity.h
index 7aa63fe..795087a 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 49a76ab..c6155ae 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 231fc84..63bb704 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 0bcde15..0c62531 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 2bb5b34..271f305 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 4db2b1b..25e192b 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 9dc957a..1fa8911 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
-- 
GitLab