Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xpath: add support for indexed xpath #112

Merged
merged 1 commit into from
Aug 2, 2024

Conversation

jeanseb6wind
Copy link
Contributor

This patch adds support for indexed xpath to xpath_set. In the case of a leaf-list, it is now possible to apply xpath_set in this way:

xpath_set(d, "/lstnum[5]", 33, after="4")

This will replace the 5th element of the leaf-list with the new value 33.

This feature is important because libyang can return this type of xpath when a callback is called with sr_module_change_subscribe from sysrepo.

The tests are updated accordingly.

@jeanseb6wind jeanseb6wind force-pushed the add_index_support_to_xpath_set branch 3 times, most recently from 527a545 to 89847e0 Compare March 19, 2024 15:59
libyang/xpath.py Outdated
break
j += 1
key_value = xpath[i:j]
keys.append((KEY_INDEX, key_value))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am a bit concerned that the keys list can now contain tuples that are not only strings. It would be less disruptive if the key name would be an empty string in this case.

You can check that below without much hassle.

libyang/xpath.py Outdated
@@ -134,6 +147,12 @@ def _list_find_key_index(keys: List[Tuple[str, str]], lst: List) -> int:
if py_to_yang(elem) == keys[0][1]:
return i

elif keys[0][0] == KEY_INDEX:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change to:

elif keys[0][0] == "":

@@ -101,7 +102,7 @@ def test_xpath_set(self):
{"name": "eth3", "mtu": 1000},
],
"lst2": ["a", "b", "c"],
"lstnum": [1, 10, 20, 30, 40, 100],
"lstnum": [1, 10, 20, 30, 33, 100],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add more unit tests. This is a rather risky change. Thanks.

This patch adds support for indexed xpath to xpath_set.
In the case of a leaf-list, it is now possible to apply xpath_set in this way:

xpath_set(d, "/lstnum[5]", 33, after="4")

This will replace the 5th element of the leaf-list with the new value 33.

This feature is important because libyang can return this type of xpath
when a callback is called with sr_module_change_subscribe from sysrepo.

The tests are updated accordingly.

Signed-off-by: Jean-Sébastien Bevilacqua <[email protected]>
@jeanseb6wind jeanseb6wind force-pushed the add_index_support_to_xpath_set branch from 89847e0 to 1347f5f Compare April 9, 2024 08:40
@jeanseb6wind
Copy link
Contributor Author

@rjarry I updated it

@rjarry rjarry merged commit 4a9b85e into CESNET:master Aug 2, 2024
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants