From 49dc812fcd816b17dd0a5c0180833ad6e04c48a1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Henrik=20tom=20W=C3=B6rden?= <h.tomwoerden@indiscale.com>
Date: Thu, 26 Aug 2021 16:17:00 +0200
Subject: [PATCH] ENH: add to_string for extern C

---
 include/ccaosdb.h     |  3 +++
 src/ccaosdb.cpp       | 30 ++++++++++++++++++++++++++++++
 test/test_ccaosdb.cpp | 11 +++++++++++
 3 files changed, 44 insertions(+)

diff --git a/include/ccaosdb.h b/include/ccaosdb.h
index cfe5cf4..263dd25 100644
--- a/include/ccaosdb.h
+++ b/include/ccaosdb.h
@@ -333,6 +333,7 @@ typedef struct {
 } caosdb_entity_message;
 
 // GETTERS FOR EVERYTHING
+int caosdb_entity_entity_to_string(caosdb_entity_entity *entity, char **out);
 int caosdb_entity_entity_get_id(caosdb_entity_entity *entity, char **out);
 int caosdb_entity_entity_get_role(caosdb_entity_entity *entity, char **out);
 int caosdb_entity_entity_get_name(caosdb_entity_entity *entity, char **out);
@@ -376,6 +377,7 @@ int caosdb_entity_entity_get_parents_size(caosdb_entity_entity *entity, int *out
 int caosdb_entity_entity_get_parent(caosdb_entity_entity *entity, caosdb_entity_parent *out,
                                     int index);
 
+int caosdb_entity_property_to_string(caosdb_entity_property *entity, char **out);
 int caosdb_entity_property_get_id(caosdb_entity_property *property, char **out);
 int caosdb_entity_property_get_name(caosdb_entity_property *property, char **out);
 int caosdb_entity_property_get_description(caosdb_entity_property *property, char **out);
@@ -401,6 +403,7 @@ int caosdb_entity_property_get_string_list_value_at(caosdb_entity_property *prop
                                                     const int index);
 int caosdb_entity_property_get_value_list_length(caosdb_entity_property *property, int *out);
 
+int caosdb_entity_parent_to_string(caosdb_entity_parent *entity, char **out);
 int caosdb_entity_parent_get_id(caosdb_entity_parent *parent, char **out);
 int caosdb_entity_parent_get_name(caosdb_entity_parent *parent, char **out);
 int caosdb_entity_parent_get_description(caosdb_entity_parent *parent, char **out);
diff --git a/src/ccaosdb.cpp b/src/ccaosdb.cpp
index b4c7ea1..b5f7921 100644
--- a/src/ccaosdb.cpp
+++ b/src/ccaosdb.cpp
@@ -584,6 +584,16 @@ ERROR_RETURN_CODE(GENERIC_ERROR, int caosdb_entity_delete_parent(caosdb_entity_p
 })
 
 CAOSDB_ENTITY_GET(id, GetId())
+ERROR_RETURN_CODE(GENERIC_ERROR,
+                  int caosdb_entity_entity_to_string(caosdb_entity_entity *entity, char **out), {
+                    auto *wrapped_entity = WRAPPED_ENTITY_CAST(entity);
+                    std::string role_str = wrapped_entity->ToString();
+                    auto *tmp = (char *)malloc(sizeof(char) * role_str.length() + 1);
+                    strcpy(tmp, role_str.c_str());
+                    delete[] * out;
+                    *out = tmp;
+                    return 0;
+                  })
 ERROR_RETURN_CODE(GENERIC_ERROR,
                   int caosdb_entity_entity_get_role(caosdb_entity_entity *entity, char **out), {
                     auto *wrapped_entity = WRAPPED_ENTITY_CAST(entity);
@@ -839,10 +849,30 @@ ERROR_RETURN_CODE(GENERIC_ERROR,
                   })
 
 CAOSDB_PARENT_GET(id, GetId())
+ERROR_RETURN_CODE(GENERIC_ERROR,
+                  int caosdb_entity_parent_to_string(caosdb_entity_parent *entity, char **out), {
+                    auto *wrapped_entity = WRAPPED_PARENT_CAST(entity);
+                    std::string role_str = wrapped_entity->ToString();
+                    auto *tmp = (char *)malloc(sizeof(char) * role_str.length() + 1);
+                    strcpy(tmp, role_str.c_str());
+                    delete[] * out;
+                    *out = tmp;
+                    return 0;
+                  })
 CAOSDB_PARENT_GET(name, GetName())
 CAOSDB_PARENT_GET(description, GetDescription())
 
 CAOSDB_PROPERTY_GET(id, GetId())
+ERROR_RETURN_CODE(GENERIC_ERROR,
+                  int caosdb_entity_property_to_string(caosdb_entity_property *entity, char **out), {
+                    auto *wrapped_entity = WRAPPED_PROPERTY_CAST(entity);
+                    std::string role_str = wrapped_entity->ToString();
+                    auto *tmp = (char *)malloc(sizeof(char) * role_str.length() + 1);
+                    strcpy(tmp, role_str.c_str());
+                    delete[] * out;
+                    *out = tmp;
+                    return 0;
+                  })
 CAOSDB_PROPERTY_GET(name, GetName())
 CAOSDB_PROPERTY_GET(description, GetDescription())
 
diff --git a/test/test_ccaosdb.cpp b/test/test_ccaosdb.cpp
index 475a5d1..a97ffa2 100644
--- a/test/test_ccaosdb.cpp
+++ b/test/test_ccaosdb.cpp
@@ -152,6 +152,9 @@ TEST_F(test_ccaosdb, test_entity) {
   EXPECT_EQ(return_code, 0);
   EXPECT_EQ(strcmp(out, "length"), 0);
 
+  return_code = caosdb_entity_entity_to_string(&entity, &out);
+  EXPECT_EQ(return_code, 0);
+
   // test call without validation of result
   return_code = caosdb_entity_entity_set_role(&entity, "FILE");
   EXPECT_EQ(return_code, 0);
@@ -170,6 +173,7 @@ TEST_F(test_ccaosdb, test_entity) {
   caosdb_entity_entity_get_role(&entity, &out);
   EXPECT_EQ(strcmp(out, "PROPERTY"), 0);
 
+
   caosdb_entity_entity_set_description(&entity, "The length of an object");
   caosdb_entity_entity_get_description(&entity, &out);
   EXPECT_EQ(strcmp(out, "The length of an object"), 0);
@@ -241,6 +245,9 @@ TEST_F(test_ccaosdb, test_property) {
   caosdb_entity_property_get_id(&property, &out);
   EXPECT_EQ(strcmp(out, "some_id"), 0);
 
+  return_code = caosdb_entity_property_to_string(&property, &out);
+  EXPECT_EQ(return_code, 0);
+
   caosdb_entity_property_get_name(&property, &out);
   EXPECT_EQ(strcmp(out, "some_name"), 0);
 
@@ -341,6 +348,7 @@ TEST_F(test_ccaosdb, test_entity_with_parent_and_property) {
   char *in = nullptr;  // NOLINT
   char *out = nullptr; // NOLINT
 
+
   // cannot assign an already assigned property
   return_code = caosdb_entity_entity_get_property(&entity, &input_property, 0);
   EXPECT_EQ(return_code, caosdb::StatusCode::EXTERN_C_ASSIGNMENT_ERROR);
@@ -382,6 +390,9 @@ TEST_F(test_ccaosdb, test_entity_with_parent_and_property) {
   caosdb_entity_parent_get_name(&output_parent, &out);
   EXPECT_EQ(strcmp(in, out), 0);
 
+  return_code = caosdb_entity_parent_to_string(&input_parent, &out);
+  EXPECT_EQ(return_code, 0);
+
   // Delete everything
   return_code = caosdb_entity_delete_parent(&input_parent);
   EXPECT_EQ(return_code, 0);
-- 
GitLab