summaryrefslogtreecommitdiffstats
path: root/lib/yang.c
diff options
context:
space:
mode:
authorChristian Hopps <chopps@labn.net>2023-10-20 14:20:44 +0200
committerChristian Hopps <chopps@labn.net>2023-12-28 18:52:57 +0100
commitdb0211d48a78730cd93b0610888f35120e0c7afe (patch)
tree46c970e126a411a063a81ffbd66d063232fb5122 /lib/yang.c
parentlib: darr: add new access and str functions (diff)
downloadfrr-db0211d48a78730cd93b0610888f35120e0c7afe.tar.xz
frr-db0211d48a78730cd93b0610888f35120e0c7afe.zip
lib: yang: add new functions
- yang_get_key_preds - yang_lyd_new_list A function like new_list was added recently to libyang, this is a compat version. Signed-off-by: Christian Hopps <chopps@labn.net>
Diffstat (limited to 'lib/yang.c')
-rw-r--r--lib/yang.c75
1 files changed, 75 insertions, 0 deletions
diff --git a/lib/yang.c b/lib/yang.c
index b0a869830..a707469a7 100644
--- a/lib/yang.c
+++ b/lib/yang.c
@@ -949,3 +949,78 @@ uint32_t yang_get_list_elements_count(const struct lyd_node *node)
} while (node);
return count;
}
+
+int yang_get_key_preds(char *s, const struct lysc_node *snode,
+ struct yang_list_keys *keys, ssize_t space)
+{
+ const struct lysc_node_leaf *skey;
+ ssize_t len2, len = 0;
+ ssize_t i = 0;
+
+ LY_FOR_KEYS (snode, skey) {
+ assert(i < keys->num);
+ len2 = snprintf(s + len, space - len, "[%s='%s']", skey->name,
+ keys->key[i]);
+ if (len2 > space - len)
+ len = space;
+ else
+ len += len2;
+ i++;
+ }
+
+ assert(i == keys->num);
+ return i;
+}
+
+LY_ERR yang_lyd_new_list(struct lyd_node_inner *parent,
+ const struct lysc_node *snode,
+ const struct yang_list_keys *list_keys,
+ struct lyd_node_inner **node)
+{
+ struct lyd_node *pnode = &parent->node;
+ struct lyd_node **nodepp = (struct lyd_node **)node;
+ const char(*keys)[LIST_MAXKEYLEN] = list_keys->key;
+
+ /*
+ * When
+ * https://github.com/CESNET/libyang/commit/2c1e327c7c2dd3ba12d466a4ebcf62c1c44116c4
+ * is released in libyang we should add a configure.ac check for the
+ * lyd_new_list3 function and use it here.
+ */
+ switch (list_keys->num) {
+ case 0:
+ return lyd_new_list(pnode, snode->module, snode->name, false,
+ nodepp);
+ case 1:
+ return lyd_new_list(pnode, snode->module, snode->name, false,
+ nodepp, keys[0]);
+ case 2:
+ return lyd_new_list(pnode, snode->module, snode->name, false,
+ nodepp, keys[0], keys[1]);
+ case 3:
+ return lyd_new_list(pnode, snode->module, snode->name, false,
+ nodepp, keys[0], keys[1], keys[2]);
+ case 4:
+ return lyd_new_list(pnode, snode->module, snode->name, false,
+ nodepp, keys[0], keys[1], keys[2], keys[3]);
+ case 5:
+ return lyd_new_list(pnode, snode->module, snode->name, false,
+ nodepp, keys[0], keys[1], keys[2], keys[3],
+ keys[4]);
+ case 6:
+ return lyd_new_list(pnode, snode->module, snode->name, false,
+ nodepp, keys[0], keys[1], keys[2], keys[3],
+ keys[4], keys[5]);
+ case 7:
+ return lyd_new_list(pnode, snode->module, snode->name, false,
+ nodepp, keys[0], keys[1], keys[2], keys[3],
+ keys[4], keys[5], keys[6]);
+ case 8:
+ return lyd_new_list(pnode, snode->module, snode->name, false,
+ nodepp, keys[0], keys[1], keys[2], keys[3],
+ keys[4], keys[5], keys[6], keys[7]);
+ }
+ _Static_assert(LIST_MAXKEYS == 8, "max key mismatch in switch unroll");
+ /*NOTREACHED*/
+ return LY_EINVAL;
+}