conf: new function: ConfNodeHasChildren

Test if a configuration node has any children, indicating
that it is a non-empty map or sequence.
pull/3205/head
Jason Ish 7 years ago
parent fe9cac5870
commit 472cc8ea61

@ -782,6 +782,25 @@ void ConfDump(void)
ConfNodeDump(root, NULL);
}
/**
* \brief Check if a node has any children.
*
* Checks if the provided node has any children. Any node that is a
* YAML map or array will have children.
*
* \param node The node to check.
*
* \retval true if node has children
* \retval false if node does not have children
*/
bool ConfNodeHasChildren(const ConfNode *node)
{
if (TAILQ_EMPTY(&node->head)) {
return false;
}
return true;
}
/**
* \brief Lookup a child configuration node by name.
*
@ -1419,6 +1438,29 @@ static int ConfSetFromStringTest(void)
PASS;
}
static int ConfNodeHasChildrenTest(void)
{
ConfCreateContextBackup();
ConfInit();
/* Set a plain key with value. */
ConfSet("no-children", "value");
ConfNode *n = ConfGetNode("no-children");
FAIL_IF_NULL(n);
FAIL_IF(ConfNodeHasChildren(n));
/* Set a key with a sub key to a value. This makes the first key a
* map. */
ConfSet("parent.child", "value");
n = ConfGetNode("parent");
FAIL_IF_NULL(n);
FAIL_IF(!ConfNodeHasChildren(n));
ConfDeInit();
ConfRestoreContextBackup();
PASS;
}
void ConfRegisterTests(void)
{
UtRegisterTest("ConfTestGetNonExistant", ConfTestGetNonExistant);
@ -1442,6 +1484,7 @@ void ConfRegisterTests(void)
UtRegisterTest("ConfNodePruneTest", ConfNodePruneTest);
UtRegisterTest("ConfNodeIsSequenceTest", ConfNodeIsSequenceTest);
UtRegisterTest("ConfSetFromStringTest", ConfSetFromStringTest);
UtRegisterTest("ConfNodeHasChildrenTest", ConfNodeHasChildrenTest);
}
#endif /* UNITTESTS */

@ -81,6 +81,7 @@ int ConfValIsTrue(const char *val);
int ConfValIsFalse(const char *val);
void ConfNodePrune(ConfNode *node);
int ConfRemove(const char *name);
bool ConfNodeHasChildren(const ConfNode *node);
ConfNode *ConfGetChildWithDefault(const ConfNode *base, const ConfNode *dflt, const char *name);
ConfNode *ConfNodeLookupKeyValue(const ConfNode *base, const char *key, const char *value);

Loading…
Cancel
Save