This is an automated email from the ASF dual-hosted git repository.

pnoltes pushed a commit to branch feature/674-improve-properties
in repository https://gitbox.apache.org/repos/asf/celix.git

commit ccd4b3580ad271f44109b346d7a5e88c91c9cf86
Author: Pepijn Noltes <[email protected]>
AuthorDate: Mon Jan 22 18:42:17 2024 +0100

    Add getString / getAsString to celix properties
---
 libs/utils/gtest/src/CxxPropertiesTestSuite.cc |  8 +--
 libs/utils/gtest/src/PropertiesTestSuite.cc    |  9 +--
 libs/utils/include/celix/Properties.h          | 41 ++++++++++++--
 libs/utils/include/celix_properties.h          | 78 ++++++++++++++++++++++++--
 libs/utils/src/properties.c                    | 54 ++++++++++++++----
 5 files changed, 160 insertions(+), 30 deletions(-)

diff --git a/libs/utils/gtest/src/CxxPropertiesTestSuite.cc 
b/libs/utils/gtest/src/CxxPropertiesTestSuite.cc
index d41342a1..2370853e 100644
--- a/libs/utils/gtest/src/CxxPropertiesTestSuite.cc
+++ b/libs/utils/gtest/src/CxxPropertiesTestSuite.cc
@@ -44,6 +44,7 @@ TEST_F(CxxPropertiesTestSuite, FillAndLoopTest) {
     EXPECT_EQ(5, props.size());
 
     EXPECT_EQ(props.get("key1"), "value1");
+    EXPECT_EQ(props.getAsString("key1"), "value1");
     EXPECT_EQ(props.get("key2"), "value2");
     EXPECT_EQ(props.getAsDouble("key3", 0), 3.3);
     EXPECT_EQ(props.get("key4"), "4");
@@ -204,7 +205,7 @@ TEST_F(CxxPropertiesTestSuite, GetTest) {
     EXPECT_EQ(props.getAsVersion("key5", celix::Version{1, 2, 4}), 
checkVersion);
 
     //Test get with valid key
-    EXPECT_EQ(props.get("key1"), "value1");
+    EXPECT_EQ(props.getString("key1"), "value1");
     EXPECT_EQ(props.getLong("key2", -1), 2);
     EXPECT_EQ(props.getDouble("key3", -1), 3.3);
     EXPECT_EQ(props.getBool("key4", false), true);
@@ -219,7 +220,7 @@ TEST_F(CxxPropertiesTestSuite, GetTest) {
     EXPECT_EQ(props.getType("non-existing"), 
celix::Properties::ValueType::Unset);
 
     // Test get with invalid key and default value
-    EXPECT_EQ(props.get("non_existent_key", "default_value"), "default_value");
+    EXPECT_EQ(props.getString("non_existent_key", "default_value"), 
"default_value");
     EXPECT_EQ(props.getLong("non_existent_key", 1), 1);
     EXPECT_EQ(props.getDouble("non_existent_key", 1.1), 1.1);
     EXPECT_EQ(props.getBool("non_existent_key", true), true);
@@ -227,8 +228,7 @@ TEST_F(CxxPropertiesTestSuite, GetTest) {
     EXPECT_EQ(props.getVersion("non_existent_key", checkVersion2), 
checkVersion2);
 
     // Test get with an existing key, but invalid type and default value
-    EXPECT_EQ(props.get("key5", "default_value"), "1.2.3"); // Note get always 
returns the string value or string
-                                                            // representation 
of the value (in this case the version)
+    EXPECT_EQ(props.getString("key5", "default_value"), "default_value"); 
//key5 is a version
     EXPECT_EQ(props.getLong("key1", 1), 1); //key1 is a string
     EXPECT_EQ(props.getDouble("key1", 1.1), 1.1); //key1 is a string
     EXPECT_EQ(props.getBool("key1", true), true); //key1 is a string
diff --git a/libs/utils/gtest/src/PropertiesTestSuite.cc 
b/libs/utils/gtest/src/PropertiesTestSuite.cc
index 1b02bc15..c610a5ca 100644
--- a/libs/utils/gtest/src/PropertiesTestSuite.cc
+++ b/libs/utils/gtest/src/PropertiesTestSuite.cc
@@ -601,7 +601,7 @@ TEST_F(PropertiesTestSuite, HasKeyTest) {
 TEST_F(PropertiesTestSuite, SetEntryTest) {
     auto* props1 = celix_properties_create();
     auto* props2 = celix_properties_create();
-    celix_properties_set(props1, "key1", "value1");
+    celix_properties_setString(props1, "key1", "value1");
     celix_properties_setLong(props1, "key2", 123);
     celix_properties_setBool(props1, "key3", true);
     celix_properties_setDouble(props1, "key4", 3.14);
@@ -614,7 +614,7 @@ TEST_F(PropertiesTestSuite, SetEntryTest) {
     }
 
     EXPECT_EQ(5, celix_properties_size(props2));
-    EXPECT_STREQ("value1", celix_properties_get(props2, "key1", nullptr));
+    EXPECT_STREQ("value1", celix_properties_getAsString(props2, "key1", 
nullptr));
     EXPECT_EQ(123, celix_properties_getAsLong(props2, "key2", -1L));
     EXPECT_EQ(true, celix_properties_getAsBool(props2, "key3", false));
     EXPECT_EQ(3.14, celix_properties_getAsDouble(props2, "key4", -1.0));
@@ -756,20 +756,21 @@ TEST_F(PropertiesTestSuite, 
GetLongDoubleBoolVersionAndStringTest) {
     celix_properties_assignVersion(props, "version", version);
 
     // check if the values are correctly returned
-    EXPECT_STREQ("value", celix_properties_get(props, "str", nullptr));
+    EXPECT_STREQ("value", celix_properties_getString(props, "str", nullptr));
     EXPECT_EQ(42, celix_properties_getLong(props, "long", -1L));
     EXPECT_DOUBLE_EQ(3.14, celix_properties_getDouble(props, "double", -1.0));
     EXPECT_EQ(true, celix_properties_getBool(props, "bool", false));
     EXPECT_EQ(version, celix_properties_getVersion(props, "version", nullptr));
 
     // check if the values are correctly returned if value is not found
-    EXPECT_EQ(nullptr, celix_properties_get(props, "non-existing", nullptr));
+    EXPECT_EQ(nullptr, celix_properties_getString(props, "non-existing", 
nullptr));
     EXPECT_EQ(-1L, celix_properties_getLong(props, "non-existing", -1L));
     EXPECT_DOUBLE_EQ(-1.0, celix_properties_getDouble(props, "non-existing", 
-1.0));
     EXPECT_EQ(false, celix_properties_getBool(props, "non-existing", false));
     EXPECT_EQ(nullptr, celix_properties_getVersion(props, "non-existing", 
nullptr));
 
     // check if the values are correctly returned if the found value is not of 
the correct type
+    EXPECT_EQ(nullptr, celix_properties_getString(props, "long", nullptr));
     EXPECT_EQ(-1L, celix_properties_getLong(props, "str", -1L));
     EXPECT_DOUBLE_EQ(-1.0, celix_properties_getDouble(props, "str", -1.0));
     EXPECT_EQ(false, celix_properties_getBool(props, "str", false));
diff --git a/libs/utils/include/celix/Properties.h 
b/libs/utils/include/celix/Properties.h
index 96eb8b32..e287f32e 100644
--- a/libs/utils/include/celix/Properties.h
+++ b/libs/utils/include/celix/Properties.h
@@ -290,10 +290,41 @@ namespace celix {
         }
 
         /**
-         * @brief Get the value for a property key or return the defaultValue 
if the key does not exists.
+         * @brief Get the string value or string representation of a property.
+         *
+         * @note identical to celix::Properties::getAsString
+         *
+         * @param[in] key The key of the property to get.
+         * @param[in] defaultValue The value to return if the property is not 
set.
+         * @return The value of the property, or the default value if the 
property is not set.
          */
         std::string get(const std::string& key, const std::string& 
defaultValue = {}) const {
-            const char* found = celix_properties_get(cProps.get(), 
key.c_str(), nullptr);
+            return getAsString(key, defaultValue);
+        }
+
+        /**
+         * @brief Get the string value or string representation of a property.
+         *
+         * @note identical to celix::Properties::get
+         *
+         * @param[in] key The key of the property to get.
+         * @param[in] defaultValue The value to return if the property is not 
set.
+         * @return The value of the property, or the default value if the 
property is not set.
+         */
+        std::string getAsString(const std::string& key, const std::string& 
defaultValue = {}) const {
+            const char* found = celix_properties_getAsString(cProps.get(), 
key.c_str(), nullptr);
+            return found == nullptr ? std::string{defaultValue} : 
std::string{found};
+        }
+
+        /**
+         * @brief Get the value of a property, if the property is set and the 
underlying type is a string.
+         * @param[in] key The key of the property to get.
+         * @param[in] defaultValue The value to return if the property is not 
set or the value is not a string.
+         * @return The value of the property, or the default value if the 
property is not set or the value is not of the
+         * requested type.
+         */
+        std::string getString(const std::string& key, const std::string& 
defaultValue = {}) const {
+            const char* found = celix_properties_getString(cProps.get(), 
key.c_str(), nullptr);
             return found == nullptr ? std::string{defaultValue} : 
std::string{found};
         }
 
@@ -310,7 +341,7 @@ namespace celix {
         }
 
         /**
-         * @Brief Get the value of a property, if the property is set and the 
underlying type is a long.
+         * @brief Get the value of a property, if the property is set and the 
underlying type is a long.
          * @param[in] key The key of the property to get.
          * @param[in] defaultValue The value to return if the property is not 
set or the value is not a long.
          * @return The value of the property, or the default value if the 
property is not set or the value is not of the
@@ -333,7 +364,7 @@ namespace celix {
         }
 
         /**
-         * @Brief Get the value of a property, if the property is set and the 
underlying type is a double.
+         * @brief Get the value of a property, if the property is set and the 
underlying type is a double.
          * @param[in] properties The property set to search.
          * @param[in] key The key of the property to get.
          * @param[in] defaultValue The value to return if the property is not 
set or the value is not a double.
@@ -357,7 +388,7 @@ namespace celix {
         }
 
         /**
-         * @Brief Get the value of a property, if the property is set and the 
underlying type is a boolean.
+         * @brief Get the value of a property, if the property is set and the 
underlying type is a boolean.
          * @param[in] key The key of the property to get.
          * @param[in] defaultValue The value to return if the property is not 
set or the value is not a boolean.
          * @return The value of the property, or the default value if the 
property is not set or the value is not of the
diff --git a/libs/utils/include/celix_properties.h 
b/libs/utils/include/celix_properties.h
index 5bd06ce2..6b86b7d2 100644
--- a/libs/utils/include/celix_properties.h
+++ b/libs/utils/include/celix_properties.h
@@ -193,6 +193,8 @@ CELIX_UTILS_EXPORT const celix_properties_entry_t* 
celix_properties_getEntry(con
 /**
  * @brief Get the string value or string representation of a property.
  *
+ * @note identical to celix_properties_getAsString
+ *
  * @param[in] properties The property set to search.
  * @param[in] key The key of the property to get.
  * @param[in] defaultValue The value to return if the property is not set.
@@ -212,7 +214,7 @@ CELIX_UTILS_EXPORT celix_properties_value_type_e 
celix_properties_getType(const
                                                                           
const char* key);
 
 /**
- * @Brief Check if the properties set has the provided key.
+ * @brief Check if the properties set has the provided key.
  * @param[in] properties The property set to search.
  * @param[in] key The key to search for.
  * @return True if the property set has the provided key, false otherwise.
@@ -222,6 +224,8 @@ CELIX_UTILS_EXPORT bool celix_properties_hasKey(const 
celix_properties_t* proper
 /**
  * @brief Set the string value of a property.
  *
+ * @note Identical to celix_properties_setString.
+ *
  * The set property type will be CELIX_PROPERTIES_VALUE_TYPE_STRING.
  *
  * If the return status is an error, an error message is logged to celix_err.
@@ -257,7 +261,69 @@ CELIX_UTILS_EXPORT celix_status_t 
celix_properties_assign(celix_properties_t* pr
                                                                   char* value);
 
 /**
- * @Brief Get the value of a property, if the property is set and the 
underlying type is a long.
+ * @brief Get the value of a property, if the property is set and the 
underlying type is a string.
+ * @param[in] properties The property set to search.
+ * @param[in] key The key of the property to get.
+ * @param[in] defaultValue The value to return if the property is not set or 
the value is not a string.
+ * @return The value of the property, or the default value if the property is 
not set or the value is not of the
+ * requested type.
+ */
+CELIX_UTILS_EXPORT const char*
+celix_properties_getString(const celix_properties_t* properties, const char* 
key, const char* defaultValue);
+
+/**
+ * @brief Get the string value or string representation of a property.
+ *
+ * @note identical to celix_properties_get
+ *
+ * @param[in] properties The property set to search.
+ * @param[in] key The key of the property to get.
+ * @param[in] defaultValue The value to return if the property is not set.
+ * @return The value of the property, or the default value if the property is 
not set.
+ */
+CELIX_UTILS_EXPORT const char*
+celix_properties_getAsString(const celix_properties_t* properties, const char* 
key, const char* defaultValue);
+
+/**
+ * @brief Set the string value of a property.
+ *
+ * @note Identical to celix_properties_set.
+ *
+ * The set property type will be CELIX_PROPERTIES_VALUE_TYPE_STRING.
+ *
+ * If the return status is an error, an error message is logged to celix_err.
+ *
+ * @param[in] properties The property set to modify.
+ * @param[in] key The key of the property to set.
+ * @param[in] value The value to set the property to.
+ * @return CELIX_SUCCESS if the operation was successful, CELIX_ENOMEM if 
there was not enough memory to set the entry
+ *         and CELIX_ILLEGAL_ARGUMENT if the provided key is NULL.
+ */
+CELIX_UTILS_EXPORT celix_status_t 
celix_properties_setString(celix_properties_t* properties,
+                                                             const char* key,
+                                                             const char* 
value);
+
+/**
+ * @brief Assign the value of a property with the provided string pointer.
+ *
+ * The set property type will be CELIX_PROPERTIES_VALUE_TYPE_STRING.
+ *
+ * This function take ownership of the provided string.
+ * If the return status is an error, an error message is logged to celix_err.
+ *
+ * @param[in] properties The property set to modify.
+ * @param[in] key The key of the property to set.
+ * @param[in] value The value to assign. The function take ownership of the 
provided version. Cannot be NULL.
+ * @return CELIX_SUCCESS if the operation was successful, CELIX_ENOMEM if 
there was not enough memory to set the entry
+ *         and CELIX_ILLEGAL_ARGUMENT if the provided key is NULL. When an 
error status is returned,
+ *         the string will be free by this function.
+ */
+CELIX_UTILS_EXPORT celix_status_t 
celix_properties_assignString(celix_properties_t* properties,
+                                                                const char* 
key,
+                                                                char* value);
+
+/**
+ * @brief Get the value of a property, if the property is set and the 
underlying type is a long.
  * @param[in] properties The property set to search.
  * @param[in] key The key of the property to get.
  * @param[in] defaultValue The value to return if the property is not set or 
the value is not a long.
@@ -297,7 +363,7 @@ celix_properties_getAsLong(const celix_properties_t* 
properties, const char* key
 CELIX_UTILS_EXPORT celix_status_t celix_properties_setLong(celix_properties_t* 
properties, const char* key, long value);
 
 /**
- * @Brief Get the value of a property, if the property is set and the 
underlying type is a boolean.
+ * @brief Get the value of a property, if the property is set and the 
underlying type is a boolean.
  * @param[in] properties The property set to search.
  * @param[in] key The key of the property to get.
  * @param[in] defaultValue The value to return if the property is not set or 
the value is not a boolean.
@@ -354,7 +420,7 @@ CELIX_UTILS_EXPORT celix_status_t 
celix_properties_setDouble(celix_properties_t*
                                                              double val);
 
 /**
- * @Brief Get the value of a property, if the property is set and the 
underlying type is a double.
+ * @brief Get the value of a property, if the property is set and the 
underlying type is a double.
  * @param[in] properties The property set to search.
  * @param[in] key The key of the property to get.
  * @param[in] defaultValue The value to return if the property is not set or 
the value is not a double.
@@ -410,7 +476,7 @@ CELIX_UTILS_EXPORT celix_status_t 
celix_properties_setVersion(celix_properties_t
  * @param[in] key The key of the property to set.
  * @param[in] version The value to assign. The function will store a reference 
to this object in the property set and
  *                    takes ownership of the provided version. Cannot be NULL.
- @return CELIX_SUCCESS if the operation was successful, CELIX_ENOMEM if there 
was not enough memory to set the entry
+ * @return CELIX_SUCCESS if the operation was successful, CELIX_ENOMEM if 
there was not enough memory to set the entry
  *         and CELIX_ILLEGAL_ARGUMENT if the provided key is NULL. When an 
error status is returned,
  *         the version will be destroy with celix_version_destroy by this 
function.
  */
@@ -446,7 +512,7 @@ celix_properties_getVersion(const celix_properties_t* 
properties, const char* ke
  * @param[in] properties The property set to search.
  * @param[in] key The key of the property to get.
  * @param[in] defaultValue The value to return if the property is not set or 
if the value is not a Celix version.
- * @param[out] list A copy of the found version, a new parsed version, or a 
copy of the default value if the
+ * @param[out] version A copy of the found version, a new parsed version, or a 
copy of the default value if the
  *                 property is not set, its value is not an version or its 
value cannot be converted to an version.
  * @return CELIX_SUCCESS if the operation was successful, CELIX_ENOMEM if 
there was not enough memory to create the
  *        version. Note if the key is not found, the return status is still 
CELIX_SUCCESS.
diff --git a/libs/utils/src/properties.c b/libs/utils/src/properties.c
index 897e93c0..38055b2c 100644
--- a/libs/utils/src/properties.c
+++ b/libs/utils/src/properties.c
@@ -174,8 +174,7 @@ static celix_status_t 
celix_properties_fillEntry(celix_properties_t* properties,
         entry->value = 
celix_utils_versionArrayListToString(entry->typed.arrayValue);
     } else /*string value*/ {
         assert(entry->valueType == CELIX_PROPERTIES_VALUE_TYPE_STRING);
-        entry->value = celix_properties_createString(properties, 
entry->typed.strValue);
-        entry->typed.strValue = entry->value;
+        entry->value = entry->typed.strValue;
     }
 
     if (entry->value == NULL) {
@@ -644,11 +643,7 @@ bool celix_properties_hasKey(const celix_properties_t* 
properties, const char* k
 }
 
 const char* celix_properties_get(const celix_properties_t* properties, const 
char* key, const char* defaultValue) {
-    const celix_properties_entry_t* entry = 
celix_properties_getEntry(properties, key);
-    if (entry != NULL) {
-        return entry->value;
-    }
-    return defaultValue;
+    return celix_properties_getAsString(properties, key, defaultValue);
 }
 
 const celix_properties_entry_t* celix_properties_getEntry(const 
celix_properties_t* properties, const char* key) {
@@ -660,10 +655,7 @@ const celix_properties_entry_t* 
celix_properties_getEntry(const celix_properties
 }
 
 celix_status_t celix_properties_set(celix_properties_t* properties, const 
char* key, const char* value) {
-    celix_properties_entry_t prototype = {0};
-    prototype.valueType = CELIX_PROPERTIES_VALUE_TYPE_STRING;
-    prototype.typed.strValue = value;
-    return celix_properties_createAndSetEntry(properties, key, &prototype);
+    return celix_properties_setString(properties, key, value);
 }
 
 celix_status_t celix_properties_assign(celix_properties_t* properties, char* 
key, char* value) {
@@ -741,6 +733,46 @@ void celix_properties_unset(celix_properties_t* 
properties, const char* key) {
     }
 }
 
+const char* celix_properties_getString(const celix_properties_t* properties,
+                                                          const char* key,
+                                                          const char* 
defaultValue) {
+    const celix_properties_entry_t* entry = 
celix_properties_getEntry(properties, key);
+    if (entry != NULL && entry->valueType == 
CELIX_PROPERTIES_VALUE_TYPE_STRING) {
+        return entry->typed.strValue;
+    }
+    return defaultValue;
+}
+
+const char* celix_properties_getAsString(const celix_properties_t* properties,
+                                                            const char* key,
+                                                            const char* 
defaultValue) {
+    const celix_properties_entry_t* entry = 
celix_properties_getEntry(properties, key);
+    if (entry != NULL) {
+        return entry->value;
+    }
+    return defaultValue;
+}
+
+celix_status_t celix_properties_setString(celix_properties_t* properties,
+                                                             const char* key,
+                                                             const char* 
value) {
+    char* copy = properties ? celix_properties_createString(properties, value) 
: NULL;
+    celix_properties_entry_t prototype = {0};
+    prototype.valueType = CELIX_PROPERTIES_VALUE_TYPE_STRING;
+    prototype.typed.strValue = copy;
+    return celix_properties_createAndSetEntry(properties, key, &prototype);
+}
+
+celix_status_t celix_properties_assignString(celix_properties_t* properties,
+                                                                const char* 
key,
+                                                                char* value) {
+    assert(value != NULL);
+    celix_properties_entry_t prototype = {0};
+    prototype.valueType = CELIX_PROPERTIES_VALUE_TYPE_STRING;
+    prototype.typed.strValue = value;
+    return celix_properties_createAndSetEntry(properties, key, &prototype);
+}
+
 long celix_properties_getLong(const celix_properties_t* properties, const 
char* key, long defaultValue) {
     const celix_properties_entry_t* entry = 
celix_properties_getEntry(properties, key);
     if (entry != NULL && entry->valueType == CELIX_PROPERTIES_VALUE_TYPE_LONG) 
{

Reply via email to