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

Commit 9de2cde

Browse files
author
Marko Mikulicic
committed
Implement wildcard matcher for arrays
Spec https://jsonpath-standard.github.io/internet-draft/#section-2.6.1 ``` A dot child name consisting of a single asterisk is a wild card. It selects all the values of any object node. It also selects all the elements of any array node. It selects no nodes from number, string, or literal nodes. ```
1 parent d27e6fd commit 9de2cde

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

src/matchers.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ pub struct WildcardedChild {}
3333

3434
impl Matcher for WildcardedChild {
3535
fn select<'a>(&self, node: &'a Value) -> Iter<'a> {
36-
if let Some(m) = node.as_object() {
37-
Box::new(m.values())
38-
} else {
39-
Box::new(iter::empty())
36+
match node {
37+
Value::Object(m) => Box::new(m.values()),
38+
Value::Array(a) => Box::new(a.iter()),
39+
_ => Box::new(iter::empty()),
4040
}
4141
}
4242
}
@@ -75,3 +75,25 @@ impl Matcher for Union {
7575
Box::new(self.elements.iter().flat_map(move |it| it.select(node)))
7676
}
7777
}
78+
79+
#[cfg(test)]
80+
mod tests {
81+
use super::*;
82+
use serde_json::{json, Value};
83+
84+
#[test]
85+
fn object_wildcard() {
86+
let s = WildcardedChild {};
87+
let j = json!({"a": 1, "b": 2});
88+
let r: Vec<&Value> = s.select(&j).collect();
89+
assert_eq!(format!("{:?}", r), "[Number(1), Number(2)]");
90+
}
91+
92+
#[test]
93+
fn array_wildcard() {
94+
let s = WildcardedChild {};
95+
let j = json!([1, 2]);
96+
let r: Vec<&Value> = s.select(&j).collect();
97+
assert_eq!(format!("{:?}", r), "[Number(1), Number(2)]");
98+
}
99+
}

tests/cts.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"name": "wildcarded child of array",
3838
"selector": "$.*",
3939
"document": ["first", "second"],
40-
"result": []
40+
"result": ["first", "second"]
4141
}, {
4242
"name": "wildcarded child dot child",
4343
"selector": "$.*.a",

0 commit comments

Comments
 (0)