diff --git a/src/conf.c b/src/conf.c index 4d7ad84be5..eae52161fd 100644 --- a/src/conf.c +++ b/src/conf.c @@ -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 */ diff --git a/src/conf.h b/src/conf.h index e44a45d5ce..daedbf9248 100644 --- a/src/conf.h +++ b/src/conf.h @@ -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);