Skip to content

Commit

Permalink
fix #50. unconsumed filter token
Browse files Browse the repository at this point in the history
  • Loading branch information
freestrings committed Aug 9, 2021
1 parent 33a9f99 commit a1b323a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 18 deletions.
39 changes: 30 additions & 9 deletions src/selector/terms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(&current.unwrap(), index);

if acc.is_empty() {
Expand All @@ -459,15 +480,6 @@ impl<'a> FilterTerms<'a> {
Some(acc)
}

pub fn collect_next_all(&mut self, current: Option<Vec<&'a Value>>) -> Option<Vec<&'a Value>> {
if current.is_none() {
debug!("collect_next_all : {:?}", &current);
return current;
}

Some(ValueWalker::next_all(&current.unwrap()))
}

pub fn collect_next_with_str(&mut self, current: Option<Vec<&'a Value>>, keys: &[&'a str]) -> Option<Vec<&'a Value>> {
if current.is_none() {
debug!(
Expand All @@ -486,6 +498,15 @@ impl<'a> FilterTerms<'a> {
Some(acc)
}

pub fn collect_next_all(&mut self, current: Option<Vec<&'a Value>>) -> Option<Vec<&'a Value>> {
if current.is_none() {
debug!("collect_next_all : {:?}", &current);
return current;
}

Some(ValueWalker::next_all(&current.unwrap()))
}

pub fn collect_all(&mut self, current: Option<Vec<&'a Value>>) -> Option<Vec<&'a Value>> {
if current.is_none() {
debug!("collect_all: {:?}", &current);
Expand Down
9 changes: 0 additions & 9 deletions src/selector/value_walker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions tests/array_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!([])
);
}

0 comments on commit a1b323a

Please sign in to comment.