1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2024-12-12 10:04:14 +02:00

Add kvGetDefault() to KeyValue object.

Return the default only if the key is not found, not when the value is NULL.
This commit is contained in:
David Steele 2019-08-08 11:41:49 -04:00
parent 289b47902b
commit 1f93e147fa
3 changed files with 33 additions and 1 deletions

View File

@ -331,6 +331,35 @@ kvGet(const KeyValue *this, const Variant *key)
FUNCTION_TEST_RETURN(result);
}
/***********************************************************************************************************************************
Get a value using the key and return a default if not found
***********************************************************************************************************************************/
const Variant *
kvGetDefault(const KeyValue *this, const Variant *key, const Variant *defaultValue)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(KEY_VALUE, this);
FUNCTION_TEST_PARAM(VARIANT, key);
FUNCTION_TEST_PARAM(VARIANT, defaultValue);
FUNCTION_TEST_END();
ASSERT(this != NULL);
ASSERT(key != NULL);
const Variant *result = NULL;
// Find the key
unsigned int listIdx = kvGetIdx(this, key);
// If key not found then return default, else return the value
if (listIdx == KEY_NOT_FOUND)
result = defaultValue;
else
result = ((KeyValuePair *)lstGet(this->list, listIdx))->value;
FUNCTION_TEST_RETURN(result);
}
/***********************************************************************************************************************************
Get a value as a list (even if there is only one value) using the key
***********************************************************************************************************************************/

View File

@ -25,6 +25,7 @@ KeyValue *kvMove(KeyValue *this, MemContext *parentNew);
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);
const Variant *kvGetDefault(const KeyValue *this, const Variant *key, const Variant *defaultValue);
bool kvKeyExists(const KeyValue *this, const Variant *key);
VariantList *kvGetList(const KeyValue *this, const Variant *key);
void kvFree(KeyValue *this);

View File

@ -24,7 +24,7 @@ testRun(void)
}
// -----------------------------------------------------------------------------------------------------------------------------
if (testBegin("kvPut(), kvAdd(), kvKeyExists(), kvKeyList(), kvGet(), kvGetList(), and kvDup()"))
if (testBegin("kvPut(), kvAdd(), kvKeyExists(), kvKeyList(), kvGet(), kvGetDefault(), kvGetList(), and kvDup()"))
{
KeyValue *store = NULL;
@ -50,7 +50,9 @@ testRun(void)
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");
TEST_RESULT_PTR(kvGetDefault(store, varNewInt(78), varNewInt(999)), NULL, "get int/null (default ignored)");
TEST_RESULT_PTR(kvGet(store, varNewInt(777)), NULL, "get missing key");
TEST_RESULT_INT(varInt(kvGetDefault(store, varNewInt(777), varNewInt(888))), 888, "get missing key with default");
// Check key exists
// -------------------------------------------------------------------------------------------------------------------------