From a1b323a40b56f140826a966275c64196fa3595d5 Mon Sep 17 00:00:00 2001 From: freestrings Date: Mon, 9 Aug 2021 23:35:21 +0900 Subject: [PATCH] fix #50. unconsumed filter token --- src/selector/terms.rs | 39 +++++++++++++++++++++++++++--------- src/selector/value_walker.rs | 9 --------- tests/array_filter.rs | 11 ++++++++++ 3 files changed, 41 insertions(+), 18 deletions(-) diff --git a/src/selector/terms.rs b/src/selector/terms.rs index b3f5166e..0821c462 100644 --- a/src/selector/terms.rs +++ b/src/selector/terms.rs @@ -450,6 +450,27 @@ impl<'a> FilterTerms<'a> { return current; } + if let Some(Some(e)) = self.pop_term() { + match e { + ExprTerm::Json(rel, _, vec) => { + return if vec.is_empty() { + Some(Vec::new()) + } else if let Some(vec) = rel { + let index = utils::abs_index(index as isize, vec.len()); + let ret = vec.get(index).map_or(Vec::new(), |v| vec![*v]); + Some(ret) + } else { + let index = utils::abs_index(index as isize, vec.len()); + let ret = vec.get(index).map_or(Vec::new(), |v| vec![*v]); + Some(ret) + }; + } + _ => { + self.push_term(Some(e)); + } + } + } + let acc = ValueWalker::next_with_num(¤t.unwrap(), index); if acc.is_empty() { @@ -459,15 +480,6 @@ impl<'a> FilterTerms<'a> { Some(acc) } - pub fn collect_next_all(&mut self, current: Option>) -> Option> { - if current.is_none() { - debug!("collect_next_all : {:?}", ¤t); - return current; - } - - Some(ValueWalker::next_all(¤t.unwrap())) - } - pub fn collect_next_with_str(&mut self, current: Option>, keys: &[&'a str]) -> Option> { if current.is_none() { debug!( @@ -486,6 +498,15 @@ impl<'a> FilterTerms<'a> { Some(acc) } + pub fn collect_next_all(&mut self, current: Option>) -> Option> { + if current.is_none() { + debug!("collect_next_all : {:?}", ¤t); + return current; + } + + Some(ValueWalker::next_all(¤t.unwrap())) + } + pub fn collect_all(&mut self, current: Option>) -> Option> { if current.is_none() { debug!("collect_all: {:?}", ¤t); diff --git a/src/selector/value_walker.rs b/src/selector/value_walker.rs index a0dc73e4..bfa82bc2 100644 --- a/src/selector/value_walker.rs +++ b/src/selector/value_walker.rs @@ -32,15 +32,6 @@ impl<'a> ValueWalker { pub fn next_with_num(vec: &Vec<&'a Value>, index: f64) -> Vec<&'a Value> { vec.iter().fold(Vec::new(), |mut acc, v| { match v { - Value::Object(map) => { - for k in map.keys() { - if let Some(Value::Array(vec)) = map.get(k) { - if let Some(v) = vec.get(utils::abs_index(index as isize, vec.len())) { - acc.push(v); - } - } - } - } Value::Array(vec) => { if let Some(v) = vec.get(utils::abs_index(index as isize, vec.len())) { acc.push(v); diff --git a/tests/array_filter.rs b/tests/array_filter.rs index 9ffa79ed..7f1cf332 100644 --- a/tests/array_filter.rs +++ b/tests/array_filter.rs @@ -250,4 +250,15 @@ fn bugs40_bracket_notation_after_recursive_descent() { "more" ]), ); +} + +#[test] +fn bugs50() { + setup(); + + select_and_then_compare( + "$[0]", + json!({"f": [1,2,3]}), + json!([]) + ); } \ No newline at end of file