From 1f93e147fa8563028db8b58621db4a1a12387c87 Mon Sep 17 00:00:00 2001 From: David Steele Date: Thu, 8 Aug 2019 11:41:49 -0400 Subject: [PATCH] Add kvGetDefault() to KeyValue object. Return the default only if the key is not found, not when the value is NULL. --- src/common/type/keyValue.c | 29 +++++++++++++++++++++++ src/common/type/keyValue.h | 1 + test/src/module/common/typeKeyValueTest.c | 4 +++- 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/common/type/keyValue.c b/src/common/type/keyValue.c index bbf0bb8c0..f4bceb572 100644 --- a/src/common/type/keyValue.c +++ b/src/common/type/keyValue.c @@ -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 ***********************************************************************************************************************************/ diff --git a/src/common/type/keyValue.h b/src/common/type/keyValue.h index 22c59c473..b66e9c015 100644 --- a/src/common/type/keyValue.h +++ b/src/common/type/keyValue.h @@ -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); diff --git a/test/src/module/common/typeKeyValueTest.c b/test/src/module/common/typeKeyValueTest.c index d6796a2b9..0620d4336 100644 --- a/test/src/module/common/typeKeyValueTest.c +++ b/test/src/module/common/typeKeyValueTest.c @@ -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 // -------------------------------------------------------------------------------------------------------------------------