From ffb23674199d3571efbe7d4e3ab24a18a093ee97 Mon Sep 17 00:00:00 2001
From: florian <f.spreckelsen@inidscale.com>
Date: Tue, 10 Aug 2021 14:42:46 +0200
Subject: [PATCH] DRAFT: Begin setters and getters for entities

---
 src/ccaosdb.cpp       | 38 +++++++++++++++++++++++++++++++++++---
 test/test_ccaosdb.cpp | 15 ++++++++++++++-
 2 files changed, 49 insertions(+), 4 deletions(-)

diff --git a/src/ccaosdb.cpp b/src/ccaosdb.cpp
index 100b3d4..d18099c 100644
--- a/src/ccaosdb.cpp
+++ b/src/ccaosdb.cpp
@@ -50,6 +50,34 @@ extern "C" {
     }                                                                          \
   }
 
+/**
+ * Macro for entity getters
+ */
+#define CAOSDB_ENTITY_GET(element, body_part)                                  \
+  ERROR_RETURN_CODE(                                                           \
+    GENERIC_ERROR,                                                             \
+    int caosdb_entity_entity_get_##element(caosdb_entity_entity *entity,       \
+                                           char *out),                         \
+    {                                                                          \
+      auto *wrapped_entity =                                                   \
+        static_cast<caosdb::entity::Entity *>(entity->wrapped_entity);         \
+      body_part return 0;                                                      \
+    })
+
+/**
+ * Macro for entity setters
+ */
+#define CAOSDB_ENTITY_SET(element, value, body_part)                           \
+  ERROR_RETURN_CODE(                                                           \
+    GENERIC_ERROR,                                                             \
+    int caosdb_entity_entity_set_##element(caosdb_entity_entity *entity,       \
+                                           const char *value),                 \
+    {                                                                          \
+      auto *wrapped_entity =                                                   \
+        static_cast<caosdb::entity::Entity *>(entity->wrapped_entity);         \
+      body_part return 0;                                                      \
+    })
+
 int caosdb_constants_LIBCAOSDB_VERSION_MAJOR() {
   return caosdb::LIBCAOSDB_VERSION_MAJOR;
 }
@@ -334,7 +362,7 @@ ERROR_RETURN_CODE(GENERIC_ERROR,
                       static_cast<caosdb::transaction::Transaction *>(
                         transaction->wrapped_transaction);
                     wrapped_transaction->ExecuteAsynchronously();
-		    auto status = wrapped_transaction->WaitForIt();
+                    auto status = wrapped_transaction->WaitForIt();
                     return status.GetCode();
                   })
 
@@ -394,8 +422,7 @@ ERROR_RETURN_CODE(GENERIC_ERROR,
 
 ERROR_RETURN_CODE(GENERIC_ERROR,
                   int caosdb_entity_create_entity(caosdb_entity_entity *out), {
-                    auto entity = caosdb::entity::Entity();
-                    out->wrapped_entity = (void *)(&entity);
+                    out->wrapped_entity = new caosdb::entity::Entity();
                     return 0;
                   })
 
@@ -405,4 +432,9 @@ ERROR_RETURN_CODE(GENERIC_ERROR,
                       out->wrapped_entity);
                     return 0;
                   })
+
+CAOSDB_ENTITY_GET(id, strcpy(out, wrapped_entity->GetId().c_str());)
+CAOSDB_ENTITY_GET(name, strcpy(out, wrapped_entity->GetName().c_str());)
+
+CAOSDB_ENTITY_SET(name, name, wrapped_entity->SetName(std::string(name));)
 }
diff --git a/test/test_ccaosdb.cpp b/test/test_ccaosdb.cpp
index 1493525..6043261 100644
--- a/test/test_ccaosdb.cpp
+++ b/test/test_ccaosdb.cpp
@@ -24,10 +24,12 @@
 #include "caosdb/status_code.h"    // for StatusCode
 #include "caosdb_test_utility.h"   // for EXPECT_THROW_MESSAGE, TEST_DATA_DIR
 #include "ccaosdb.h"               // for caosdb_utility_get_env_var
+#include <cstring>                 // for strcmp
 #include <gtest/gtest-message.h>   // for Message
 #include <gtest/gtest-test-part.h> // for SuiteApiResolver, TestFactoryImpl
 #include <gtest/gtest_pred_impl.h> // for Test, TestInfo, EXPECT_EQ, TEST
-#include <string>                  // for allocator
+#include <iostream>
+#include <string> // for allocator
 
 class test_ccaosdb : public ::testing::Test {
 protected:
@@ -99,10 +101,12 @@ TEST_F(test_ccaosdb, test_execute_transaction) {
 }
 
 TEST_F(test_ccaosdb, test_multi_retrieve) {
+  std::cout << "Entering test_multi_retrieve ..." << std::endl;
   caosdb_connection_connection connection;
   caosdb_connection_connection_manager_get_connection(&connection,
                                                       "local-caosdb-admin");
 
+  std::cout << "Creating transaction" << std::endl;
   caosdb_transaction_transaction multi_transaction;
   caosdb_connection_connection_create_transaction(&connection,
                                                   &multi_transaction);
@@ -110,10 +114,12 @@ TEST_F(test_ccaosdb, test_multi_retrieve) {
   // We explicitely want to define a C-style array here, so we disable
   // linting
   const char *ids[] = {"id1", "id2", "id3"}; // NOLINT
+  std::cout << "Adding mutli retrieval ..." << std::endl;
   int return_code(
     caosdb_transaction_transaction_retrieve_by_ids(&multi_transaction, ids));
   EXPECT_EQ(return_code, caosdb::StatusCode::GO_ON);
 
+  std::cout << "Deleting transaction ..." << std::endl;
   return_code = caosdb_transaction_delete_transaction(&multi_transaction);
   EXPECT_EQ(return_code, 0);
 }
@@ -140,6 +146,13 @@ TEST_F(test_ccaosdb, test_entity) {
   int return_code(caosdb_entity_create_entity(&entity));
   EXPECT_EQ(return_code, 0);
 
+  return_code = caosdb_entity_entity_set_name(&entity, "some_name");
+  EXPECT_EQ(return_code, 0);
+  char out[255] = {"a"}; // NOLINT
+  return_code = caosdb_entity_entity_get_name(&entity, out);
+  EXPECT_EQ(return_code, 0);
+  EXPECT_EQ(strcmp(out, "some_name"), 0);
+
   return_code = caosdb_entity_delete_entity(&entity);
   EXPECT_EQ(return_code, 0);
 }
-- 
GitLab