From 1ba0c1b10a1e5e2348dafcc5b36ef8feddf5b46d Mon Sep 17 00:00:00 2001 From: Alexandre Snarskii Date: Sun, 11 May 2025 18:29:43 +0300 Subject: [PATCH 1/2] tree schema FEATURE emit paths suitable for key search --- src/tree_schema.c | 6 +++--- src/tree_schema.h | 6 ++++-- tests/utests/schema/test_schema.c | 8 ++++++++ 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/tree_schema.c b/src/tree_schema.c index 916a834e4..8c757d701 100644 --- a/src/tree_schema.c +++ b/src/tree_schema.c @@ -695,7 +695,7 @@ lysc_path_until(const struct lysc_node *node, const struct lysc_node *parent, LY buffer[0] = '\0'; } - if ((pathtype == LYSC_PATH_DATA) || (pathtype == LYSC_PATH_DATA_PATTERN)) { + if ((pathtype == LYSC_PATH_DATA) || (pathtype == LYSC_PATH_DATA_PATTERN) || (pathtype == LYSC_PATH_KEY_PATTERN)) { /* skip schema-only nodes */ skip_schema = 1; } else { @@ -711,11 +711,11 @@ lysc_path_until(const struct lysc_node *node, const struct lysc_node *parent, LY continue; } - if ((pathtype == LYSC_PATH_DATA_PATTERN) && (iter->nodetype == LYS_LIST)) { + if (((pathtype == LYSC_PATH_DATA_PATTERN) || (pathtype == LYSC_PATH_KEY_PATTERN)) && (iter->nodetype == LYS_LIST)) { char *predicates = NULL; key = NULL; - while ((key = lys_getnext(key, iter, NULL, 0)) && lysc_is_key(key)) { + while ((key = lys_getnext(key, iter, NULL, 0)) && lysc_is_key(key) && ((pathtype != LYSC_PATH_KEY_PATTERN) || (key != node))) { s = predicates; /* print key predicate */ diff --git a/src/tree_schema.h b/src/tree_schema.h index 35da1a415..5aba160eb 100644 --- a/src/tree_schema.h +++ b/src/tree_schema.h @@ -2114,8 +2114,10 @@ LIBYANG_API_DECL const struct lysc_node *lys_find_path(const struct ly_ctx *ctx, typedef enum { LYSC_PATH_LOG, /**< Descriptive path format used in log messages */ LYSC_PATH_DATA, /**< Similar to ::LYSC_PATH_LOG except that schema-only nodes (choice, case) are skipped */ - LYSC_PATH_DATA_PATTERN /**< Similar to ::LYSC_PATH_DATA but there are predicates for all list keys added with - "%s" where their values should be so that they can be printed there */ + LYSC_PATH_DATA_PATTERN, /**< Similar to ::LYSC_PATH_DATA but there are predicates for all list keys added with + "%s" where their values should be so that they can be printed there */ + LYSC_PATH_KEY_PATTERN /**< Similar to ::LYSC_PATH_DATA_PATTERN but if target node is a list key predicates + for this and subsequent keys are not emitted */ } LYSC_PATH_TYPE; /** diff --git a/tests/utests/schema/test_schema.c b/tests/utests/schema/test_schema.c index 42a051e77..6e8951294 100644 --- a/tests/utests/schema/test_schema.c +++ b/tests/utests/schema/test_schema.c @@ -1896,6 +1896,14 @@ test_lysc_path(void **state) path = lysc_path(node, LYSC_PATH_DATA_PATTERN, NULL, 0); assert_string_equal(path, "/b:a/l[k='%s'][l='%s'][m='%s']/n"); free(path); + /* node is the same as in previous test */ + path = lysc_path(node, LYSC_PATH_KEY_PATTERN, NULL, 0); + assert_string_equal(path, "/b:a/l[k='%s'][l='%s'][m='%s']/n"); + free(path); + node = lys_find_path(UTEST_LYCTS, NULL, "/b:a/l/l", 0); + path = lysc_path(node, LYSC_PATH_KEY_PATTERN, NULL, 0); + assert_string_equal(path, "/b:a/l[k='%s']/l"); + free(path); } /* TEST */ From d9e4386f519040bf7b908247812dd499f987a7c5 Mon Sep 17 00:00:00 2001 From: Alexandre Snarskii Date: Sun, 11 May 2025 19:15:01 +0300 Subject: [PATCH 2/2] fix typo in test --- tests/utests/schema/test_schema.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/utests/schema/test_schema.c b/tests/utests/schema/test_schema.c index 6e8951294..33d946452 100644 --- a/tests/utests/schema/test_schema.c +++ b/tests/utests/schema/test_schema.c @@ -1900,7 +1900,7 @@ test_lysc_path(void **state) path = lysc_path(node, LYSC_PATH_KEY_PATTERN, NULL, 0); assert_string_equal(path, "/b:a/l[k='%s'][l='%s'][m='%s']/n"); free(path); - node = lys_find_path(UTEST_LYCTS, NULL, "/b:a/l/l", 0); + node = lys_find_path(UTEST_LYCTX, NULL, "/b:a/l/l", 0); path = lysc_path(node, LYSC_PATH_KEY_PATTERN, NULL, 0); assert_string_equal(path, "/b:a/l[k='%s']/l"); free(path);