From 5edbfac43ecedfe2c6872e80407e6ac3cb5e6a9f Mon Sep 17 00:00:00 2001
From: florian <f.spreckelsen@inidscale.com>
Date: Wed, 18 Aug 2021 17:26:27 +0200
Subject: [PATCH] DRAFT: Add set_value to entity

---
 include/caosdb/value.h |   2 +
 include/ccaosdb.h      |  24 ++---
 src/ccaosdb.cpp        | 230 +++++++++++++++++++++++++++++++++++++++--
 test/test_ccaosdb.cpp  |  48 +++++++--
 4 files changed, 275 insertions(+), 29 deletions(-)

diff --git a/include/caosdb/value.h b/include/caosdb/value.h
index 9cde6c6..b112461 100644
--- a/include/caosdb/value.h
+++ b/include/caosdb/value.h
@@ -132,6 +132,8 @@ public:
   }
 
   LIST_VALUE_CONSTRUCTOR(int64_t, set_integer_value)
+  LIST_VALUE_CONSTRUCTOR(double, set_double_value)
+  LIST_VALUE_CONSTRUCTOR(bool, set_boolean_value)
 
   [[nodiscard]] inline auto IsNull() -> bool {
     return this->wrapped->value_case() == ValueCase::VALUE_NOT_SET;
diff --git a/include/ccaosdb.h b/include/ccaosdb.h
index 002cbe2..9e21e8c 100644
--- a/include/ccaosdb.h
+++ b/include/ccaosdb.h
@@ -439,21 +439,21 @@ int caosdb_entity_entity_set_unit(caosdb_entity_entity *entity,
                                   const char *unit);
 // TODO(fspreck) replace by more specific setters
 int caosdb_entity_entity_set_int_value(caosdb_entity_entity *entity,
-                                       const long *value);
+                                       const long value);
 int caosdb_entity_entity_set_double_value(caosdb_entity_entity *entity,
-                                          const double *value);
+                                          const double value);
 int caosdb_entity_entity_set_boolean_value(caosdb_entity_entity *entity,
-                                           const bool *value);
+                                           const bool value);
 int caosdb_entity_entity_set_string_value(caosdb_entity_entity *entity,
                                           const char *value);
 int caosdb_entity_entity_set_int_list_value(caosdb_entity_entity *entity,
-                                            const long **value,
+                                            const long *value,
                                             const int length);
 int caosdb_entity_entity_set_double_list_value(caosdb_entity_entity *entity,
-                                               const double **value,
+                                               const double *value,
                                                const int length);
 int caosdb_entity_entity_set_boolean_list_value(caosdb_entity_entity *entity,
-                                                const bool **value,
+                                                const bool *value,
                                                 const int length);
 int caosdb_entity_entity_set_string_list_value(caosdb_entity_entity *entity,
                                                const char **value,
@@ -479,20 +479,20 @@ int caosdb_entity_property_set_unit(caosdb_entity_property *property,
                                     const char *unit);
 
 int caosdb_entity_property_set_int_value(caosdb_entity_property *property,
-                                         const long *value);
+                                         const long value);
 int caosdb_entity_property_set_double_value(caosdb_entity_property *property,
-                                            const double *value);
+                                            const double value);
 int caosdb_entity_property_set_boolean_value(caosdb_entity_property *property,
-                                             const bool *value);
+                                             const bool value);
 int caosdb_entity_property_set_string_value(caosdb_entity_property *property,
                                             const char *value);
 int caosdb_entity_property_set_int_list_value(caosdb_entity_property *property,
-                                              const long **value,
+                                              const long *value,
                                               const int length);
 int caosdb_entity_property_set_double_list_value(
-  caosdb_entity_property *property, const double **value, const int length);
+  caosdb_entity_property *property, const double *value, const int length);
 int caosdb_entity_property_set_boolean_list_value(
-  caosdb_entity_property *property, const bool **value, const int length);
+  caosdb_entity_property *property, const bool *value, const int length);
 int caosdb_entity_property_set_string_list_value(
   caosdb_entity_property *property, const char **value, const int length);
 
diff --git a/src/ccaosdb.cpp b/src/ccaosdb.cpp
index 3765ddf..1af1701 100644
--- a/src/ccaosdb.cpp
+++ b/src/ccaosdb.cpp
@@ -783,9 +783,118 @@ CAOSDB_ENTITY_SET(description, description,
 // CAOSDB_ENTITY_SET(datatype, datatype,
 //                  wrapped_entity->SetDataType(std::string(datatype));)
 CAOSDB_ENTITY_SET(unit, unit, wrapped_entity->SetUnit(std::string(unit));)
-// TODO(fspreck)
-// CAOSDB_ENTITY_SET(value, value,
-// wrapped_entity->SetValue(std::string(value));)
+
+ERROR_RETURN_CODE(
+  GENERIC_ERROR,
+  int caosdb_entity_entity_set_int_value(caosdb_entity_entity *entity,
+                                           const long value),
+  {
+    auto *wrapped_entity =
+      static_cast<caosdb::entity::Entity *>(entity->wrapped_entity);
+    wrapped_entity->SetValue(value);
+    return 0;
+  })
+
+ERROR_RETURN_CODE(
+  GENERIC_ERROR,
+  int caosdb_entity_entity_set_double_value(caosdb_entity_entity *entity,
+                                              const double value),
+  {
+    auto *wrapped_entity =
+      static_cast<caosdb::entity::Entity *>(entity->wrapped_entity);
+    wrapped_entity->SetValue(value);
+    return 0;
+  })
+
+ERROR_RETURN_CODE(
+  GENERIC_ERROR,
+  int caosdb_entity_entity_set_boolean_value(caosdb_entity_entity *entity,
+                                               const bool value),
+  {
+    auto *wrapped_entity =
+      static_cast<caosdb::entity::Entity *>(entity->wrapped_entity);
+    wrapped_entity->SetValue(value);
+    return 0;
+  })
+
+ERROR_RETURN_CODE(
+  GENERIC_ERROR,
+  int caosdb_entity_entity_set_string_value(caosdb_entity_entity *entity,
+                                              const char *value),
+  {
+    auto *wrapped_entity =
+      static_cast<caosdb::entity::Entity *>(entity->wrapped_entity);
+    wrapped_entity->SetValue(std::string(value));
+    return 0;
+  })
+
+ERROR_RETURN_CODE(GENERIC_ERROR,
+                  int caosdb_entity_entity_set_int_list_value(
+                    caosdb_entity_entity *entity, const long *value,
+                    const int length),
+                  {
+                    auto *wrapped_entity =
+                      static_cast<caosdb::entity::Entity *>(
+                        entity->wrapped_entity);
+                    std::vector<long> value_list;
+                    for (int i = 0; i < length; i++) {
+                      value_list.push_back(value[i]);
+                    }
+                    caosdb::entity::Value to_be_set(value_list);
+                    wrapped_entity->SetValue(to_be_set);
+                    return 0;
+                  })
+
+ERROR_RETURN_CODE(GENERIC_ERROR,
+                  int caosdb_entity_entity_set_double_list_value(
+                    caosdb_entity_entity *entity, const double *value,
+                    const int length),
+                  {
+                    auto *wrapped_entity =
+                      static_cast<caosdb::entity::Entity *>(
+                        entity->wrapped_entity);
+                    std::vector<double> value_list;
+                    for (int i = 0; i < length; i++) {
+                      value_list.push_back(value[i]);
+                    }
+                    caosdb::entity::Value to_be_set(value_list);
+                    wrapped_entity->SetValue(to_be_set);
+                    return 0;
+                  })
+
+ERROR_RETURN_CODE(GENERIC_ERROR,
+                  int caosdb_entity_entity_set_boolean_list_value(
+                    caosdb_entity_entity *entity, const bool *value,
+                    const int length),
+                  {
+                    auto *wrapped_entity =
+                      static_cast<caosdb::entity::Entity *>(
+                        entity->wrapped_entity);
+                    std::vector<bool> value_list;
+                    for (int i = 0; i < length; i++) {
+                      value_list.push_back(value[i]);
+                    }
+                    caosdb::entity::Value to_be_set(value_list);
+                    wrapped_entity->SetValue(to_be_set);
+                    return 0;
+                  })
+
+ERROR_RETURN_CODE(GENERIC_ERROR,
+                  int caosdb_entity_entity_set_string_list_value(
+                    caosdb_entity_entity *entity, const char **value,
+                    const int length),
+                  {
+                    auto *wrapped_entity =
+                      static_cast<caosdb::entity::Entity *>(
+                        entity->wrapped_entity);
+                    std::vector<std::string> value_list;
+                    for (int i = 0; i < length; i++) {
+                      value_list.push_back(std::string(value[i]));
+                    }
+                    caosdb::entity::Value to_be_set(value_list);
+                    wrapped_entity->SetValue(to_be_set);
+                    return 0;
+                  })
 
 ERROR_RETURN_CODE(
   GENERIC_ERROR,
@@ -846,7 +955,116 @@ CAOSDB_PROPERTY_SET(id, id, wrapped_property->SetId(std::string(id));)
 // CAOSDB_PROPERTY_SET(importance, importance,
 //                    wrapped_property->SetImportance(std::string(importance));)
 CAOSDB_PROPERTY_SET(unit, unit, wrapped_property->SetUnit(std::string(unit));)
-// TODO(fspreck)
-// CAOSDB_PROPERTY_SET(value, value,
-//                    wrapped_property->SetValue(std::string(value));)
+
+ERROR_RETURN_CODE(
+  GENERIC_ERROR,
+  int caosdb_entity_property_set_int_value(caosdb_entity_property *property,
+                                           const long value),
+  {
+    auto *wrapped_property =
+      static_cast<caosdb::entity::Property *>(property->wrapped_property);
+    wrapped_property->SetValue(value);
+    return 0;
+  })
+
+ERROR_RETURN_CODE(
+  GENERIC_ERROR,
+  int caosdb_entity_property_set_double_value(caosdb_entity_property *property,
+                                              const double value),
+  {
+    auto *wrapped_property =
+      static_cast<caosdb::entity::Property *>(property->wrapped_property);
+    wrapped_property->SetValue(value);
+    return 0;
+  })
+
+ERROR_RETURN_CODE(
+  GENERIC_ERROR,
+  int caosdb_entity_property_set_boolean_value(caosdb_entity_property *property,
+                                               const bool value),
+  {
+    auto *wrapped_property =
+      static_cast<caosdb::entity::Property *>(property->wrapped_property);
+    wrapped_property->SetValue(value);
+    return 0;
+  })
+
+ERROR_RETURN_CODE(
+  GENERIC_ERROR,
+  int caosdb_entity_property_set_string_value(caosdb_entity_property *property,
+                                              const char *value),
+  {
+    auto *wrapped_property =
+      static_cast<caosdb::entity::Property *>(property->wrapped_property);
+    wrapped_property->SetValue(std::string(value));
+    return 0;
+  })
+
+ERROR_RETURN_CODE(GENERIC_ERROR,
+                  int caosdb_entity_property_set_int_list_value(
+                    caosdb_entity_property *property, const long *value,
+                    const int length),
+                  {
+                    auto *wrapped_property =
+                      static_cast<caosdb::entity::Property *>(
+                        property->wrapped_property);
+                    std::vector<long> value_list;
+                    for (int i = 0; i < length; i++) {
+                      value_list.push_back(value[i]);
+                    }
+                    caosdb::entity::Value to_be_set(value_list);
+                    wrapped_property->SetValue(to_be_set);
+                    return 0;
+                  })
+
+ERROR_RETURN_CODE(GENERIC_ERROR,
+                  int caosdb_entity_property_set_double_list_value(
+                    caosdb_entity_property *property, const double *value,
+                    const int length),
+                  {
+                    auto *wrapped_property =
+                      static_cast<caosdb::entity::Property *>(
+                        property->wrapped_property);
+                    std::vector<double> value_list;
+                    for (int i = 0; i < length; i++) {
+                      value_list.push_back(value[i]);
+                    }
+                    caosdb::entity::Value to_be_set(value_list);
+                    wrapped_property->SetValue(to_be_set);
+                    return 0;
+                  })
+
+ERROR_RETURN_CODE(GENERIC_ERROR,
+                  int caosdb_entity_property_set_boolean_list_value(
+                    caosdb_entity_property *property, const bool *value,
+                    const int length),
+                  {
+                    auto *wrapped_property =
+                      static_cast<caosdb::entity::Property *>(
+                        property->wrapped_property);
+                    std::vector<bool> value_list;
+                    for (int i = 0; i < length; i++) {
+                      value_list.push_back(value[i]);
+                    }
+                    caosdb::entity::Value to_be_set(value_list);
+                    wrapped_property->SetValue(to_be_set);
+                    return 0;
+                  })
+
+ERROR_RETURN_CODE(GENERIC_ERROR,
+                  int caosdb_entity_property_set_string_list_value(
+                    caosdb_entity_property *property, const char **value,
+                    const int length),
+                  {
+                    auto *wrapped_property =
+                      static_cast<caosdb::entity::Property *>(
+                        property->wrapped_property);
+                    std::vector<std::string> value_list;
+                    for (int i = 0; i < length; i++) {
+                      value_list.push_back(std::string(value[i]));
+                    }
+                    caosdb::entity::Value to_be_set(value_list);
+                    wrapped_property->SetValue(to_be_set);
+                    return 0;
+                  })
 }
diff --git a/test/test_ccaosdb.cpp b/test/test_ccaosdb.cpp
index 15d0ed6..a75d8b4 100644
--- a/test/test_ccaosdb.cpp
+++ b/test/test_ccaosdb.cpp
@@ -189,7 +189,7 @@ TEST_F(test_ccaosdb, test_entity) {
   EXPECT_EQ(strcmp(out, "m"), 0);
 
   // TODO(fspreck)
-  // caosdb_entity_entity_set_value(&entity, "5.0");
+  caosdb_entity_entity_set_double_value(&entity, 5.0);
   // caosdb_entity_entity_get_value(&entity, out);
   // EXPECT_EQ(strcmp(out, "5.0"), 0);
 
@@ -226,12 +226,11 @@ TEST_F(test_ccaosdb, test_property) {
   caosdb_entity_property_set_id(&property, "some_id");
   caosdb_entity_property_set_name(&property, "some_name");
   // TODO(fspreck)
-  // caosdb_entity_property_set_datatype(&property, "some_datatype");
+  // caosdb_entity_property_set_datatype(&property, "TEXT");
   // TODO(fspreck)
-  // caosdb_entity_property_set_importance(&property, "some_importance");
+  // caosdb_entity_property_set_importance(&property, "FIX");
   caosdb_entity_property_set_unit(&property, "some_unit");
-  // TODO(fspreck)
-  // caosdb_entity_property_set_value(&property, "some_value");
+  caosdb_entity_property_set_string_value(&property, "some_value");
 
   char out[255] = {"a"}; // NOLINT
   caosdb_entity_property_get_id(&property, out);
@@ -242,23 +241,51 @@ TEST_F(test_ccaosdb, test_property) {
 
   // TODO(fspreck)
   // caosdb_entity_property_get_datatype(&property, out);
-  // EXPECT_EQ(strcmp(out, "some_datatype"), 0);
+  // EXPECT_EQ(strcmp(out, "TEXT"), 0);
 
   // TODO(fspreck)
   // caosdb_entity_property_get_importance(&property, out);
-  // EXPECT_EQ(strcmp(out, "some_importance"), 0);
+  // EXPECT_EQ(strcmp(out, "FIX"), 0);
 
   caosdb_entity_property_get_unit(&property, out);
   EXPECT_EQ(strcmp(out, "some_unit"), 0);
 
   // TODO(fspreck)
-  // caosdb_entity_property_get_value(&property, out);
+  // caosdb_entity_property_get_string_value(&property, out);
   // EXPECT_EQ(strcmp(out, "some_value"), 0);
 
   return_code = caosdb_entity_delete_property(&property);
   EXPECT_EQ(return_code, 0);
 }
 
+// TODO(fspreck) Test a property with datatype LIST<Something> and a
+// list value.
+TEST_F(test_ccaosdb, test_list_property) {
+
+  caosdb_entity_property property;
+  int return_code(caosdb_entity_create_property(&property));
+  EXPECT_EQ(return_code, 0);
+
+  // TODO(fspreck)
+  // return_code = caosdb_entity_property_set_datatype(&property, "LIST<TEXT>");
+  // EXPECT_EQ(return_code, 0);
+
+  const *char[] value_list = {"val0", "val1", "val2"}; // NOLINT
+  return_code = caosdb_entity_property_set_string_list_value(&property, value_list, 3);
+  EXPECT_EQ(return_code, 0);
+
+  // TODO(fspreck)
+  // char out_type[255] = {"abc"}; // NOLINT
+  // return_code = caosdb_entity_property_get_datatype(&property, out_type);
+  // EXPECT_EQ(return_code, 0);
+  // EXPECT_EQ(strcmp(out_type, "LIST<TEXT>"));
+
+  // TODO(fspreck) get and compare values in list
+
+  return_code = caosdb_entity_delete_property(&property);
+  EXPECT_EQ(return_code, 0);
+}
+
 TEST_F(test_ccaosdb, test_entity_with_parent_and_property) {
   std::cout << "Creating objects ... " << std::endl;
   caosdb_entity_parent input_parent;
@@ -275,9 +302,8 @@ TEST_F(test_ccaosdb, test_entity_with_parent_and_property) {
   caosdb_entity_property_set_id(&input_property, "property_id");
   caosdb_entity_property_set_name(&input_property, "property_name");
   // TODO(fspreck)
-  // caosdb_entity_property_set_datatype(&input_property, "property_datatype");
-  // TODO(fspreck)
-  // caosdb_entity_property_set_value(&input_property, "property_value");
+  // caosdb_entity_property_set_datatype(&input_property, "TEXT");
+  caosdb_entity_property_set_string_value(&input_property, "property_value");
 
   caosdb_entity_entity entity;
   return_code = caosdb_entity_create_entity(&entity);
-- 
GitLab