1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2025-09-16 09:06:18 +02:00

Improve KeyValue object.

Add kvGetList() to get a value as a list (of 1) even if it is a scalar.
This commit is contained in:
David Steele
2018-01-23 12:40:02 -05:00
parent 2afb73da1c
commit 41bd5e8148
3 changed files with 25 additions and 1 deletions

View File

@@ -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
***********************************************************************************************************************************/

View File

@@ -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

View File

@@ -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");