From 63423526c9f60ca7808f8d4833940677e51ff61d Mon Sep 17 00:00:00 2001
From: florian <f.spreckelsen@inidscale.com>
Date: Thu, 16 Sep 2021 11:51:28 +0200
Subject: [PATCH] DRAFT: Re-implement getting of values and datatypes

---
 include/ccaosdb.h     |   4 --
 src/ccaosdb.cpp       | 118 ++++++++++++++++--------------------------
 test/test_ccaosdb.cpp |  92 ++++++++++++++++++++++++--------
 3 files changed, 114 insertions(+), 100 deletions(-)

diff --git a/include/ccaosdb.h b/include/ccaosdb.h
index 21ba884..38ed90e 100644
--- a/include/ccaosdb.h
+++ b/include/ccaosdb.h
@@ -377,10 +377,8 @@ int caosdb_entity_entity_get_role(caosdb_entity_entity *entity, char **out);
 int caosdb_entity_entity_get_name(caosdb_entity_entity *entity, char **out);
 int caosdb_entity_entity_get_description(caosdb_entity_entity *entity, char **out);
 int caosdb_entity_entity_get_local_path(caosdb_entity_entity *entity, char **out);
-// TODO(fspreck) implementation
 int caosdb_entity_entity_get_datatype(caosdb_entity_entity *entity, caosdb_entity_datatype *out);
 int caosdb_entity_entity_get_unit(caosdb_entity_entity *entity, char **out);
-// TODO(fspreck) implementation
 int caosdb_entity_entity_get_value(caosdb_entity_entity *entity, caosdb_entity_value *out);
 int caosdb_entity_entity_get_version_id(caosdb_entity_entity *entity, char **out);
 int caosdb_entity_entity_get_errors_size(caosdb_entity_entity *entity, int *out);
@@ -403,11 +401,9 @@ 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);
 int caosdb_entity_property_get_importance(caosdb_entity_property *property, char **out);
-// TODO(fspreck) implementation
 int caosdb_entity_property_get_datatype(caosdb_entity_property *property,
                                         caosdb_entity_datatype *out);
 int caosdb_entity_property_get_unit(caosdb_entity_property *property, char **out);
-// TODO(fspreck) implementation
 int caosdb_entity_property_get_value(caosdb_entity_property *property, caosdb_entity_value *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);
diff --git a/src/ccaosdb.cpp b/src/ccaosdb.cpp
index 476505e..e966ecd 100644
--- a/src/ccaosdb.cpp
+++ b/src/ccaosdb.cpp
@@ -789,41 +789,26 @@ ERROR_RETURN_CODE(GENERIC_ERROR,
                   })
 // CAOSDB_ENTITY_GET(file_path, GetFilePath()) TODO(henrik)
 CAOSDB_ENTITY_GET(description, GetDescription())
-// TODO(fspreck)
-// ERROR_RETURN_CODE(GENERIC_ERROR,
-//                   int caosdb_entity_entity_get_datatype(caosdb_entity_entity *entity, char
-//                   **name,
-//                                                         bool *is_ref, bool *is_list),
-//                   {
-//                     auto *wrapped_entity = WRAPPED_ENTITY_CAST(entity);
-//                     const auto &datatype = wrapped_entity->GetDataType();
-//                     *is_list = datatype.IsList();
-//                     std::string datatype_name;
-//                     if (*is_list) {
-//                       const auto &list_datatype = datatype.AsList();
-//                       *is_ref = list_datatype.IsListOfReference();
-//                       if (*is_ref) {
-//                         datatype_name = list_datatype.GetReferenceDataType().GetName();
-//                       } else {
-//                         datatype_name =
-//                           ENUM_NAME_FROM_VALUE(list_datatype.GetAtomicDataType(),
-//                           AtomicDataType);
-//                       }
-//                     } else {
-//                       *is_ref = datatype.IsReference();
-//                       if (*is_ref) {
-//                         datatype_name = datatype.AsReference().GetName();
-//                       } else {
-//                         datatype_name = ENUM_NAME_FROM_VALUE(datatype.AsAtomic(),
-//                         AtomicDataType);
-//                       }
-//                     }
-//                     char *tmp = (char *)malloc(sizeof(char) * datatype_name.length() + 1);
-//                     strcpy(tmp, datatype_name.c_str());
-//                     delete[] * name;
-//                     *name = tmp;
-//                     return 0;
-//                   })
+ERROR_RETURN_CODE(GENERIC_ERROR,
+                  int caosdb_entity_entity_get_datatype(caosdb_entity_entity *entity,
+                                                        caosdb_entity_datatype *out),
+                  {
+                    RETURN_ASSIGNEMENT_ERROR_IF_DELETABLE(out)
+                    auto *wrapped_entity = WRAPPED_ENTITY_CAST(entity);
+                    out->wrapped_datatype = (void *)(&(wrapped_entity->GetDataType()));
+                    out->_deletable = false;
+                    return 0;
+                  })
+ERROR_RETURN_CODE(GENERIC_ERROR,
+                  int caosdb_entity_entity_get_value(caosdb_entity_entity *entity,
+                                                     caosdb_entity_value *out),
+                  {
+                    RETURN_ASSIGNEMENT_ERROR_IF_DELETABLE(out)
+                    auto *wrapped_entity = WRAPPED_ENTITY_CAST(entity);
+                    out->wrapped_value = (void *)(&(wrapped_entity->GetValue()));
+                    out->_deletable = false;
+                    return 0;
+                  })
 
 CAOSDB_ENTITY_GET(unit, GetUnit())
 CAOSDB_ENTITY_GET(version_id, GetVersionId())
@@ -965,43 +950,26 @@ ERROR_RETURN_CODE(GENERIC_ERROR,
                     *out = tmp;
                     return 0;
                   })
-
-// TODO(fspreck)
-// ERROR_RETURN_CODE(GENERIC_ERROR,
-//                   int caosdb_entity_property_get_datatype(caosdb_entity_property *property,
-//                                                           char **name, bool *is_ref, bool
-//                                                           *is_list),
-//                   {
-//                     auto *wrapped_property = WRAPPED_PROPERTY_CAST(property);
-//                     const auto &datatype = wrapped_property->GetDataType();
-//                     *is_list = datatype.IsList();
-//                     std::string datatype_name;
-//                     if (*is_list) {
-//                       const auto &list_datatype = datatype.AsList();
-//                       *is_ref = list_datatype.IsListOfReference();
-//                       if (*is_ref) {
-//                         datatype_name = list_datatype.GetReferenceDataType().GetName();
-//                       } else {
-//                         datatype_name =
-//                           ENUM_NAME_FROM_VALUE(list_datatype.GetAtomicDataType(),
-//                           AtomicDataType);
-//                       }
-//                     } else {
-//                       *is_ref = datatype.IsReference();
-//                       if (*is_ref) {
-//                         datatype_name = datatype.AsReference().GetName();
-//                       } else {
-//                         datatype_name = ENUM_NAME_FROM_VALUE(datatype.AsAtomic(),
-//                         AtomicDataType);
-//                       }
-//                     }
-//                     char *tmp = (char *)malloc(sizeof(char) * datatype_name.length() + 1);
-//                     strcpy(tmp, datatype_name.c_str());
-//                     delete[] * name;
-//                     *name = tmp;
-//                     return 0;
-//                   })
-
+ERROR_RETURN_CODE(GENERIC_ERROR,
+                  int caosdb_entity_property_get_datatype(caosdb_entity_property *property,
+                                                          caosdb_entity_datatype *out),
+                  {
+                    RETURN_ASSIGNEMENT_ERROR_IF_DELETABLE(out)
+                    auto *wrapped_property = WRAPPED_PROPERTY_CAST(property);
+                    out->wrapped_datatype = (void *)(&(wrapped_property->GetDataType()));
+                    out->_deletable = false;
+                    return 0;
+                  })
+ERROR_RETURN_CODE(GENERIC_ERROR,
+                  int caosdb_entity_property_get_value(caosdb_entity_property *property,
+                                                       caosdb_entity_value *out),
+                  {
+                    RETURN_ASSIGNEMENT_ERROR_IF_DELETABLE(out)
+                    auto *wrapped_property = WRAPPED_PROPERTY_CAST(property);
+                    out->wrapped_value = (void *)(&(wrapped_property->GetValue()));
+                    out->_deletable = false;
+                    return 0;
+                  })
 CAOSDB_PROPERTY_GET(unit, GetUnit())
 
 ERROR_RETURN_CODE(GENERIC_ERROR,
@@ -1160,7 +1128,11 @@ ERROR_RETURN_CODE(GENERIC_ERROR,
                     auto *wrapped_entity = WRAPPED_ENTITY_CAST(entity);
                     auto *wrapped_value = WRAPPED_VALUE_CAST(value);
 
-                    return wrapped_entity->SetValue(*wrapped_value);
+                    std::cout << wrapped_entity->ToString() << std::endl;
+                    int return_value(wrapped_entity->SetValue(*wrapped_value));
+                    std::cout << wrapped_entity->ToString() << std::endl;
+
+                    return return_value;
                   })
 
 ERROR_RETURN_CODE(GENERIC_ERROR,
diff --git a/test/test_ccaosdb.cpp b/test/test_ccaosdb.cpp
index 48ad961..317cd8f 100644
--- a/test/test_ccaosdb.cpp
+++ b/test/test_ccaosdb.cpp
@@ -426,40 +426,86 @@ TEST_F(test_ccaosdb, test_entity) {
   caosdb_entity_datatype in_type;
   caosdb_entity_create_atomic_datatype(&in_type, "DOUBLE");
   caosdb_entity_entity_set_datatype(&entity, &in_type);
-  // TODO(fspreck)
-  // bool is_list[] = {false}; // NOLINT
-  // bool is_ref[] = {false};  // NOLINT
-  // caosdb_entity_entity_get_datatype(&entity, &out, is_ref, is_list);
-  // EXPECT_EQ(strcmp(out, "DOUBLE"), 0);
-  // EXPECT_FALSE(*is_list);
-  // EXPECT_FALSE(*is_ref);
+
+  // verify that this doesn't work ...
+  return_code = caosdb_entity_entity_get_datatype(&entity, &in_type);
+  EXPECT_EQ(return_code, caosdb::StatusCode::EXTERN_C_ASSIGNMENT_ERROR);
+  caosdb_entity_datatype out_type;
+  // ... but does with a clean property
+  return_code = caosdb_entity_entity_get_datatype(&entity, &out_type);
+  EXPECT_EQ(return_code, 0);
+  bool is_a(false);
+  return_code = caosdb_entity_datatype_is_atomic(&out_type, &is_a);
+  EXPECT_EQ(return_code, 0);
+  EXPECT_TRUE(is_a);
+  return_code = caosdb_entity_datatype_is_reference(&out_type, &is_a);
+  EXPECT_EQ(return_code, 0);
+  EXPECT_FALSE(is_a);
+  return_code = caosdb_entity_datatype_is_list_of_atomic(&out_type, &is_a);
+  EXPECT_EQ(return_code, 0);
+  EXPECT_FALSE(is_a);
+  return_code = caosdb_entity_datatype_is_list_of_refernce(&out_type, &is_a);
+  EXPECT_EQ(return_code, 0);
+  EXPECT_FALSE(is_a);
+
+  caosdb_entity_datatype_get_datatype_name(&out_type, &out);
+  EXPECT_STREQ(out, "DOUBLE");
+
+  caosdb_entity_value in_value;
+  return_code = caosdb_entity_create_double_value(&in_value, 5.0);
+  EXPECT_EQ(return_code, 0);
+  return_code = caosdb_entity_entity_set_value(&entity, &in_value);
+  EXPECT_EQ(return_code, 0);
+
+  caosdb_entity_value out_value;
+  return_code = caosdb_entity_entity_get_value(&entity, &out_value);
+  EXPECT_EQ(return_code, 0);
+
+  caosdb_entity_value_is_double(&out_value, &is_a);
+  EXPECT_TRUE(is_a);
+  caosdb_entity_value_is_null(&out_value, &is_a);
+  EXPECT_FALSE(is_a);
+  caosdb_entity_value_is_string(&out_value, &is_a);
+  EXPECT_FALSE(is_a);
+  caosdb_entity_value_is_bool(&out_value, &is_a);
+  EXPECT_FALSE(is_a);
+  caosdb_entity_value_is_integer(&out_value, &is_a);
+  EXPECT_FALSE(is_a);
+  caosdb_entity_value_is_vector(&out_value, &is_a);
+  EXPECT_FALSE(is_a);
+
+  double out_double(0);
+  caosdb_entity_value_get_as_double(&out_value, &out_double);
+  EXPECT_EQ(out_double, 5.0);
 
   // clear to re-use
   return_code = caosdb_entity_delete_datatype(&in_type);
   EXPECT_EQ(return_code, 0);
   caosdb_entity_create_reference_list_datatype(&in_type, "Person");
   caosdb_entity_entity_set_datatype(&entity, &in_type);
-  // TODO(fspreck)
-  // caosdb_entity_entity_get_datatype(&entity, &out, is_ref, is_list);
-  // EXPECT_EQ(strcmp(out, "Person"), 0);
-  // EXPECT_TRUE(*is_list);
-  // EXPECT_TRUE(*is_ref);
+
+  // works without clearing since datatype is managed by the owning entity
+  caosdb_entity_entity_get_datatype(&entity, &out_type);
+  return_code = caosdb_entity_datatype_is_atomic(&out_type, &is_a);
+  EXPECT_EQ(return_code, 0);
+  EXPECT_FALSE(is_a);
+  return_code = caosdb_entity_datatype_is_reference(&out_type, &is_a);
+  EXPECT_EQ(return_code, 0);
+  EXPECT_FALSE(is_a);
+  return_code = caosdb_entity_datatype_is_list_of_atomic(&out_type, &is_a);
+  EXPECT_EQ(return_code, 0);
+  EXPECT_FALSE(is_a);
+  return_code = caosdb_entity_datatype_is_list_of_refernce(&out_type, &is_a);
+  EXPECT_EQ(return_code, 0);
+  EXPECT_TRUE(is_a);
+
+  caosdb_entity_datatype_get_datatype_name(&out_type, &out);
+  EXPECT_STREQ(out, "Person");
 
   caosdb_entity_entity_set_unit(&entity, "m");
   caosdb_entity_entity_get_unit(&entity, &out);
   EXPECT_EQ(strcmp(out, "m"), 0);
 
-  caosdb_entity_value in_value;
-  return_code = caosdb_entity_create_double_value(&in_value, 5.0);
-  EXPECT_EQ(return_code, 0);
-  return_code = caosdb_entity_entity_set_value(&entity, &in_value);
-  EXPECT_EQ(return_code, 0);
-  // TODO(fspreck)
-  // double value[] = {0.0}; // NOLINT
-  // return_code = caosdb_entity_entity_get_double_value(&entity, value);
-  // EXPECT_EQ(return_code, 0);
-  // EXPECT_EQ(*value, 5.0);
-
   return_code = caosdb_entity_delete_entity(&entity);
   EXPECT_EQ(return_code, 0);
   return_code = caosdb_entity_delete_datatype(&in_type);
-- 
GitLab