Skip to content

Commit 1ba0c1b

Browse files
committed
tree schema FEATURE emit paths suitable for key search
1 parent a64821a commit 1ba0c1b

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

src/tree_schema.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ lysc_path_until(const struct lysc_node *node, const struct lysc_node *parent, LY
695695
buffer[0] = '\0';
696696
}
697697

698-
if ((pathtype == LYSC_PATH_DATA) || (pathtype == LYSC_PATH_DATA_PATTERN)) {
698+
if ((pathtype == LYSC_PATH_DATA) || (pathtype == LYSC_PATH_DATA_PATTERN) || (pathtype == LYSC_PATH_KEY_PATTERN)) {
699699
/* skip schema-only nodes */
700700
skip_schema = 1;
701701
} else {
@@ -711,11 +711,11 @@ lysc_path_until(const struct lysc_node *node, const struct lysc_node *parent, LY
711711
continue;
712712
}
713713

714-
if ((pathtype == LYSC_PATH_DATA_PATTERN) && (iter->nodetype == LYS_LIST)) {
714+
if (((pathtype == LYSC_PATH_DATA_PATTERN) || (pathtype == LYSC_PATH_KEY_PATTERN)) && (iter->nodetype == LYS_LIST)) {
715715
char *predicates = NULL;
716716

717717
key = NULL;
718-
while ((key = lys_getnext(key, iter, NULL, 0)) && lysc_is_key(key)) {
718+
while ((key = lys_getnext(key, iter, NULL, 0)) && lysc_is_key(key) && ((pathtype != LYSC_PATH_KEY_PATTERN) || (key != node))) {
719719
s = predicates;
720720

721721
/* print key predicate */

src/tree_schema.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2114,8 +2114,10 @@ LIBYANG_API_DECL const struct lysc_node *lys_find_path(const struct ly_ctx *ctx,
21142114
typedef enum {
21152115
LYSC_PATH_LOG, /**< Descriptive path format used in log messages */
21162116
LYSC_PATH_DATA, /**< Similar to ::LYSC_PATH_LOG except that schema-only nodes (choice, case) are skipped */
2117-
LYSC_PATH_DATA_PATTERN /**< Similar to ::LYSC_PATH_DATA but there are predicates for all list keys added with
2118-
"%s" where their values should be so that they can be printed there */
2117+
LYSC_PATH_DATA_PATTERN, /**< Similar to ::LYSC_PATH_DATA but there are predicates for all list keys added with
2118+
"%s" where their values should be so that they can be printed there */
2119+
LYSC_PATH_KEY_PATTERN /**< Similar to ::LYSC_PATH_DATA_PATTERN but if target node is a list key predicates
2120+
for this and subsequent keys are not emitted */
21192121
} LYSC_PATH_TYPE;
21202122

21212123
/**

tests/utests/schema/test_schema.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1896,6 +1896,14 @@ test_lysc_path(void **state)
18961896
path = lysc_path(node, LYSC_PATH_DATA_PATTERN, NULL, 0);
18971897
assert_string_equal(path, "/b:a/l[k='%s'][l='%s'][m='%s']/n");
18981898
free(path);
1899+
/* node is the same as in previous test */
1900+
path = lysc_path(node, LYSC_PATH_KEY_PATTERN, NULL, 0);
1901+
assert_string_equal(path, "/b:a/l[k='%s'][l='%s'][m='%s']/n");
1902+
free(path);
1903+
node = lys_find_path(UTEST_LYCTS, NULL, "/b:a/l/l", 0);
1904+
path = lysc_path(node, LYSC_PATH_KEY_PATTERN, NULL, 0);
1905+
assert_string_equal(path, "/b:a/l[k='%s']/l");
1906+
free(path);
18991907
}
19001908

19011909
/* TEST */

0 commit comments

Comments
 (0)