Skip to content

Commit

Permalink
Add keys method to nodes/rels
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwalker committed Aug 4, 2023
1 parent 48e5a9d commit 34b8b20
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/src/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ impl Node {
self.inner.labels.iter().map(|l| l.to_string()).collect()
}

/// Get the names of the attributes of this node
pub fn keys(&self) -> Vec<&str> {
self.to::<crate::Keys<_>>().unwrap().0
}

/// Get the attributes of the node
pub fn get<T: std::convert::TryFrom<BoltType>>(&self, key: &str) -> Option<T> {
self.inner.get(key)
Expand Down Expand Up @@ -184,6 +189,11 @@ impl Relation {
self.inner.typ.value.clone()
}

/// Get the names of the attributes of this relationship
pub fn keys(&self) -> Vec<&str> {
self.to::<crate::Keys<_>>().unwrap().0
}

pub fn get<T: std::convert::TryFrom<BoltType>>(&self, key: &str) -> Option<T> {
self.inner.get(key)
}
Expand All @@ -210,6 +220,11 @@ impl UnboundedRelation {
self.inner.typ.value.clone()
}

/// Get the names of the attributes of this relationship
pub fn keys(&self) -> Vec<&str> {
self.to::<crate::Keys<_>>().unwrap().0
}

pub fn get<T: std::convert::TryFrom<BoltType>>(&self, key: &str) -> Option<T> {
self.inner.get(key)
}
Expand Down
2 changes: 2 additions & 0 deletions lib/tests/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ async fn nodes() {
let node: Node = row.get("friend").unwrap();
let id = node.id();
let labels = node.labels();
let keys = node.keys();
let name: String = node.get("name").unwrap();
assert_eq!(name, "Mr Mark");
assert_eq!(labels, vec!["Person"]);
assert_eq!(keys, vec!["name"]);
assert!(id >= 0);
}
}
1 change: 1 addition & 0 deletions lib/tests/relationships.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ async fn relationships() {
assert!(relation.start_node_id() > -1);
assert!(relation.end_node_id() > -1);
assert_eq!(relation.typ(), "WORKS_AT");
assert_eq!(relation.keys(), vec!["as"]);
assert_eq!(relation.get::<String>("as").unwrap(), "Engineer");
}
1 change: 1 addition & 0 deletions lib/tests/unbounded_relationships.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ async fn unbounded_relationships() {
assert!(relation.start_node_id() > -1);
assert!(relation.end_node_id() > -1);
assert_eq!(relation.typ(), "RELATED");
assert_eq!(relation.keys(), vec!["as"]);
assert_eq!(relation.get::<String>("as").unwrap(), "friend");
}

0 comments on commit 34b8b20

Please sign in to comment.