diff --git a/src/common/type/keyValue.c b/src/common/type/keyValue.c index 351abb530..ee3cdd650 100644 --- a/src/common/type/keyValue.c +++ b/src/common/type/keyValue.c @@ -245,6 +245,26 @@ kvGet(const KeyValue *this, const Variant *key) return result; } +/*********************************************************************************************************************************** +Get a value as a list (even if there is only one value) using the key +***********************************************************************************************************************************/ +VariantList * +kvGetList(const KeyValue *this, const Variant *key) +{ + VariantList *result = NULL; + + // Get the value + const Variant *value = kvGet(this, key); + + // Convert the value to a list if it is not already one + if (value != NULL && varType(value) == varTypeVariantList) + result = varLstDup(varVarLst(value)); + else + result = varLstAdd(varLstNew(), varDup(value)); + + return result; +} + /*********************************************************************************************************************************** Free the string ***********************************************************************************************************************************/ diff --git a/src/common/type/keyValue.h b/src/common/type/keyValue.h index 54f6826fd..1c3b2c508 100644 --- a/src/common/type/keyValue.h +++ b/src/common/type/keyValue.h @@ -21,6 +21,7 @@ const VariantList *kvKeyList(const KeyValue *this); KeyValue *kvPut(KeyValue *this, const Variant *key, const Variant *value); KeyValue *kvPutKv(KeyValue *this, const Variant *key); const Variant *kvGet(const KeyValue *this, const Variant *key); +VariantList *kvGetList(const KeyValue *this, const Variant *key); void kvFree(KeyValue *this); #endif diff --git a/test/src/module/common/typeKeyValueTest.c b/test/src/module/common/typeKeyValueTest.c index 238ae3719..2db4986c0 100644 --- a/test/src/module/common/typeKeyValueTest.c +++ b/test/src/module/common/typeKeyValueTest.c @@ -21,7 +21,7 @@ void testRun() } // ----------------------------------------------------------------------------------------------------------------------------- - if (testBegin("kvPut(), kvAdd(), kvKeyList(), kvGet(), and kvDup()")) + if (testBegin("kvPut(), kvAdd(), kvKeyList(), kvGet(), kvGetList(), and kvDup()")) { KeyValue *store = NULL; @@ -38,6 +38,7 @@ void testRun() // ------------------------------------------------------------------------------------------------------------------------- TEST_RESULT_STR(strPtr(varStr(kvGet(store, varNewStr(strNew("str-key"))))), "str-value", "get string/string"); TEST_RESULT_INT(varInt(kvGet(store, varNewInt(42))), 57, "get int/int"); + TEST_RESULT_INT(varInt(varLstGet(kvGetList(store, varNewInt(42)), 0)), 57, "get int/int"); TEST_RESULT_INT(varInt(kvGet(store, varNewStr(strNew("str-key-int")))), 99, "get string/int"); TEST_RESULT_PTR(kvGet(store, varNewInt(78)), NULL, "get int/null"); @@ -62,6 +63,8 @@ void testRun() TEST_RESULT_INT(varInt(varLstGet(varVarLst(kvGet(store, varNewInt(99))), 1)), 2, "get int/int"); TEST_RESULT_INT(varInt(varLstGet(varVarLst(kvGet(store, varNewInt(99))), 2)), 3, "get int/int"); + TEST_RESULT_INT(varInt(varLstGet(kvGetList(store, varNewInt(99)), 2)), 3, "get int/int"); + // Check item in key list // ------------------------------------------------------------------------------------------------------------------------- TEST_RESULT_INT(varInt(varLstGet(kvKeyList(store), 1)), 42, "key list");