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

Add the parameter output to find_path #29

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,17 +264,23 @@ pub trait Data<'a> {
}

/// Search in the given data for a single node matching the provided XPath.
/// Whether to search in RPC/action output nodes or in input nodes.
///
/// The expected format of the expression is JSON, meaning the first node in
/// every path must have its module name as prefix or be the special `*`
/// value for all the nodes.
fn find_path(&'a self, path: &str) -> Result<DataNodeRef<'a>> {
fn find_path(
&'a self,
path: &str,
output: bool,
) -> Result<DataNodeRef<'a>> {
let path = CString::new(path).unwrap();
let mut rnode = std::ptr::null_mut();
let rnode_ptr = &mut rnode;
let output = if output { 1u8 } else { 0u8 };

let ret = unsafe {
ffi::lyd_find_path(self.raw(), path.as_ptr(), 0u8, rnode_ptr)
ffi::lyd_find_path(self.raw(), path.as_ptr(), output, rnode_ptr)
};
if ret != ffi::LY_ERR::LY_SUCCESS {
return Err(Error::new(self.context()));
Expand Down Expand Up @@ -752,7 +758,7 @@ impl<'a> DataTree<'a> {

/// Remove a data node.
pub fn remove(&mut self, path: &str) -> Result<()> {
let dnode = self.find_path(path)?;
let dnode = self.find_path(path, false)?;
unsafe { ffi::lyd_free_tree(dnode.raw) };
Ok(())
}
Expand Down Expand Up @@ -988,7 +994,7 @@ impl<'a> DataTreeOwningRef<'a> {
let raw = {
match tree.new_path(path, value, output)? {
Some(node) => node.raw,
None => tree.find_path(path)?.raw,
None => tree.find_path(path, output)?.raw,
}
};
Ok(unsafe { DataTreeOwningRef::from_raw(tree, raw) })
Expand Down
33 changes: 25 additions & 8 deletions tests/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ fn create_context() -> Context {
"ietf-ip",
"ietf-routing",
"ietf-isis",
"ietf-mpls-ldp",
] {
ctx.load_module(module_name, None, &[])
.expect("Failed to load module");
Expand Down Expand Up @@ -307,10 +308,13 @@ fn data_find_path() {
let dtree1 = parse_json_data(&ctx, JSON_TREE1);

assert!(dtree1
.find_path("/ietf-interfaces:interfaces/interface")
.find_path("/ietf-interfaces:interfaces/interface", false)
.is_err());
assert!(dtree1
.find_path("/ietf-interfaces:interfaces/interface[name='eth/0/0']")
.find_path(
"/ietf-interfaces:interfaces/interface[name='eth/0/0']",
false
)
.is_ok());
}

Expand Down Expand Up @@ -387,7 +391,10 @@ fn data_duplicate_subtree() {
let dtree1 = parse_json_data(&ctx, JSON_TREE1);

let dnode = dtree1
.find_path("/ietf-interfaces:interfaces/interface[name='eth/0/0']")
.find_path(
"/ietf-interfaces:interfaces/interface[name='eth/0/0']",
false,
)
.expect("Failed to lookup data");

// Duplicate without parents.
Expand Down Expand Up @@ -672,6 +679,7 @@ fn data_iterator_ancestors() {
dtree1
.find_path(
"/ietf-interfaces:interfaces/interface[name='eth/0/0']/type",
false
)
.expect("Failed to lookup data")
.ancestors()
Expand All @@ -686,6 +694,7 @@ fn data_iterator_ancestors() {
dtree1
.find_path(
"/ietf-interfaces:interfaces/interface[name='eth/0/0']/type",
false
)
.expect("Failed to lookup data")
.inclusive_ancestors()
Expand All @@ -706,7 +715,10 @@ fn data_iterator_siblings() {

assert_eq!(
dtree1
.find_path("/ietf-interfaces:interfaces/interface[name='eth/0/0']")
.find_path(
"/ietf-interfaces:interfaces/interface[name='eth/0/0']",
false
)
.expect("Failed to lookup data")
.siblings()
.map(|dnode| dnode.path())
Expand All @@ -715,7 +727,10 @@ fn data_iterator_siblings() {
);
assert_eq!(
dtree1
.find_path("/ietf-interfaces:interfaces/interface[name='eth/0/0']")
.find_path(
"/ietf-interfaces:interfaces/interface[name='eth/0/0']",
false
)
.expect("Failed to lookup data")
.inclusive_siblings()
.map(|dnode| dnode.path())
Expand All @@ -734,7 +749,7 @@ fn data_iterator_children() {

assert_eq!(
dtree1
.find_path("/ietf-interfaces:interfaces")
.find_path("/ietf-interfaces:interfaces", false)
.expect("Failed to lookup data")
.children()
.map(|dnode| dnode.path())
Expand All @@ -754,7 +769,8 @@ fn data_is_default() {
assert_eq!(
dtree2
.find_path(
"/ietf-interfaces:interfaces/interface[name='eth/0/0']/enabled"
"/ietf-interfaces:interfaces/interface[name='eth/0/0']/enabled",
false
)
.expect("Failed to lookup data")
.is_default(),
Expand All @@ -763,7 +779,8 @@ fn data_is_default() {
assert_eq!(
dtree2
.find_path(
"/ietf-interfaces:interfaces/interface[name='eth/0/2']/enabled"
"/ietf-interfaces:interfaces/interface[name='eth/0/2']/enabled",
false
)
.expect("Failed to lookup data")
.is_default(),
Expand Down