Skip to content
This repository was archived by the owner on Feb 22, 2024. It is now read-only.

Implement wildcard matcher for arrays #13

Merged
merged 1 commit into from
Oct 1, 2020
Merged
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
30 changes: 26 additions & 4 deletions src/matchers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ pub struct WildcardedChild {}

impl Matcher for WildcardedChild {
fn select<'a>(&self, node: &'a Value) -> Iter<'a> {
if let Some(m) = node.as_object() {
Box::new(m.values())
} else {
Box::new(iter::empty())
match node {
Value::Object(m) => Box::new(m.values()),
Value::Array(a) => Box::new(a.iter()),
_ => Box::new(iter::empty()),
}
}
}
Expand Down Expand Up @@ -75,3 +75,25 @@ impl Matcher for Union {
Box::new(self.elements.iter().flat_map(move |it| it.select(node)))
}
}

#[cfg(test)]
Copy link
Contributor

Choose a reason for hiding this comment

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

Yes. Love it!

mod tests {
use super::*;
use serde_json::{json, Value};

#[test]
fn object_wildcard() {
let s = WildcardedChild {};
let j = json!({"a": 1, "b": 2});
let r: Vec<&Value> = s.select(&j).collect();
assert_eq!(format!("{:?}", r), "[Number(1), Number(2)]");
}

#[test]
fn array_wildcard() {
let s = WildcardedChild {};
let j = json!([1, 2]);
let r: Vec<&Value> = s.select(&j).collect();
assert_eq!(format!("{:?}", r), "[Number(1), Number(2)]");
}
}
2 changes: 1 addition & 1 deletion tests/cts.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"name": "wildcarded child of array",
"selector": "$.*",
"document": ["first", "second"],
"result": []
"result": ["first", "second"]
}, {
"name": "wildcarded child dot child",
"selector": "$.*.a",
Expand Down