mirror of
https://github.com/fluencelabs/jsonpath
synced 2025-06-12 23:51:26 +00:00
close #38
This commit is contained in:
@ -286,7 +286,23 @@ impl<'a> ExprTerm<'a> {
|
|||||||
if ret.is_empty() {
|
if ret.is_empty() {
|
||||||
ExprTerm::Bool(cmp_fn.default())
|
ExprTerm::Bool(cmp_fn.default())
|
||||||
} else if let Some(rel) = rel {
|
} else if let Some(rel) = rel {
|
||||||
ExprTerm::Json(Some(rel.to_vec()), None, ret)
|
if let ExprTerm::Json(_, _, _) = &other {
|
||||||
|
ExprTerm::Json(Some(rel.to_vec()), None, ret)
|
||||||
|
} else {
|
||||||
|
let mut tmp = Vec::new();
|
||||||
|
for rel_value in rel {
|
||||||
|
if let Value::Object(map) = rel_value {
|
||||||
|
for map_value in map.values() {
|
||||||
|
for result_value in &ret {
|
||||||
|
if map_value.eq(*result_value) {
|
||||||
|
tmp.push(*rel_value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ExprTerm::Json(Some(tmp), None, ret)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
ExprTerm::Json(None, None, ret)
|
ExprTerm::Json(None, None, ret)
|
||||||
}
|
}
|
||||||
@ -865,6 +881,7 @@ impl<'a, 'b> Selector<'a, 'b> {
|
|||||||
self.current = Some(vec![v]);
|
self.current = Some(vec![v]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_relative(&mut self) {
|
fn visit_relative(&mut self) {
|
||||||
if let Some(ParseToken::Array) = self.tokens.last() {
|
if let Some(ParseToken::Array) = self.tokens.last() {
|
||||||
let array_token = self.tokens.pop();
|
let array_token = self.tokens.pop();
|
||||||
@ -878,6 +895,18 @@ impl<'a, 'b> Selector<'a, 'b> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn visit_array_eof(&mut self) {
|
fn visit_array_eof(&mut self) {
|
||||||
|
if self.is_nested_array() {
|
||||||
|
if let Some(Some(e)) = self.terms.pop() {
|
||||||
|
if let ExprTerm::String(key) = e {
|
||||||
|
self.next_in_filter_with_str(&key);
|
||||||
|
self.tokens.pop();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.terms.push(Some(e));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(Some(e)) = self.terms.pop() {
|
if let Some(Some(e)) = self.terms.pop() {
|
||||||
match e {
|
match e {
|
||||||
ExprTerm::Number(n) => {
|
ExprTerm::Number(n) => {
|
||||||
@ -925,6 +954,21 @@ impl<'a, 'b> Selector<'a, 'b> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_nested_array(&mut self) -> bool {
|
||||||
|
let mut is_nested_array = false;
|
||||||
|
|
||||||
|
if let Some(t) = self.tokens.pop() {
|
||||||
|
if let ParseToken::Array = t {
|
||||||
|
if let Some(ParseToken::Array) = self.tokens.last() {
|
||||||
|
is_nested_array = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.tokens.push(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
is_nested_array
|
||||||
|
}
|
||||||
|
|
||||||
fn visit_key(&mut self, key: &str) {
|
fn visit_key(&mut self, key: &str) {
|
||||||
if let Some(ParseToken::Array) = self.tokens.last() {
|
if let Some(ParseToken::Array) = self.tokens.last() {
|
||||||
self.terms.push(Some(ExprTerm::String(key.to_string())));
|
self.terms.push(Some(ExprTerm::String(key.to_string())));
|
||||||
|
@ -126,6 +126,22 @@ fn filter_parent_exist_child() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn filter_parent_paths() {
|
||||||
|
setup();
|
||||||
|
|
||||||
|
select_and_then_compare(
|
||||||
|
"$[?(@.key.subKey == 'subKey2')]",
|
||||||
|
json!([
|
||||||
|
{"key": {"seq": 1, "subKey": "subKey1"}},
|
||||||
|
{"key": {"seq": 2, "subKey": "subKey2"}},
|
||||||
|
{"key": 42},
|
||||||
|
{"some": "value"}
|
||||||
|
]),
|
||||||
|
json!([{"key": {"seq": 2, "subKey": "subKey2"}}]),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn bugs33_exist_in_all() {
|
fn bugs33_exist_in_all() {
|
||||||
setup();
|
setup();
|
||||||
@ -207,4 +223,58 @@ fn bugs33_exist_right_in_all_with_and_condition() {
|
|||||||
}
|
}
|
||||||
]),
|
]),
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn bugs38_array_notation_in_filter() {
|
||||||
|
setup();
|
||||||
|
|
||||||
|
select_and_then_compare(
|
||||||
|
"$[?(@['key']==42)]",
|
||||||
|
json!([
|
||||||
|
{"key": 0},
|
||||||
|
{"key": 42},
|
||||||
|
{"key": -1},
|
||||||
|
{"key": 41},
|
||||||
|
{"key": 43},
|
||||||
|
{"key": 42.0001},
|
||||||
|
{"key": 41.9999},
|
||||||
|
{"key": 100},
|
||||||
|
{"some": "value"}
|
||||||
|
]),
|
||||||
|
json!([{"key": 42}]),
|
||||||
|
);
|
||||||
|
|
||||||
|
select_and_then_compare(
|
||||||
|
"$[?(@['key'].subKey == 'subKey2')]",
|
||||||
|
json!([
|
||||||
|
{"key": {"seq": 1, "subKey": "subKey1"}},
|
||||||
|
{"key": {"seq": 2, "subKey": "subKey2"}},
|
||||||
|
{"key": 42},
|
||||||
|
{"some": "value"}
|
||||||
|
]),
|
||||||
|
json!([{"key": {"seq": 2, "subKey": "subKey2"}}]),
|
||||||
|
);
|
||||||
|
|
||||||
|
select_and_then_compare(
|
||||||
|
"$[?(@['key']['subKey'] == 'subKey2')]",
|
||||||
|
json!([
|
||||||
|
{"key": {"seq": 1, "subKey": "subKey1"}},
|
||||||
|
{"key": {"seq": 2, "subKey": "subKey2"}},
|
||||||
|
{"key": 42},
|
||||||
|
{"some": "value"}
|
||||||
|
]),
|
||||||
|
json!([{"key": {"seq": 2, "subKey": "subKey2"}}]),
|
||||||
|
);
|
||||||
|
|
||||||
|
select_and_then_compare(
|
||||||
|
"$..key[?(@['subKey'] == 'subKey2')]",
|
||||||
|
json!([
|
||||||
|
{"key": {"seq": 1, "subKey": "subKey1"}},
|
||||||
|
{"key": {"seq": 2, "subKey": "subKey2"}},
|
||||||
|
{"key": 42},
|
||||||
|
{"some": "value"}
|
||||||
|
]),
|
||||||
|
json!([{"seq": 2, "subKey": "subKey2"}]),
|
||||||
|
);
|
||||||
}
|
}
|
Reference in New Issue
Block a user