Skip to content

Commit

Permalink
Fix issue with reduction of terminal leafs in FilterDescendant::Pair
Browse files Browse the repository at this point in the history
  • Loading branch information
mitghi committed Jul 15, 2023
1 parent baa85e8 commit e06a557
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "jetro"
version = "0.2.3"
version = "0.2.4"
edition = "2021"
authors = ["Milad (Mike) Taghavi <mitghi.at.gmail.com>"]
license = "MIT"
Expand Down
36 changes: 34 additions & 2 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ pub(crate) struct Context<'a> {
stack: Rc<RefCell<Vec<StackItem<'a>>>>,
registry: Rc<RefCell<dyn crate::func::Registry>>,
pub results: Rc<RefCell<Vec<Rc<Value>>>>,
step_results: Rc<RefCell<Vec<Value>>>,
}

/// MapBody represents the body of map function.
Expand Down Expand Up @@ -773,6 +774,7 @@ impl<'a> Context<'a> {
let results: Rc<RefCell<Vec<Rc<Value>>>> = Rc::new(RefCell::new(Vec::new()));
let stack: Rc<RefCell<Vec<StackItem<'a>>>> = Rc::new(RefCell::new(Vec::new()));
let rv: Rc<Value> = Rc::new(value);
let step_results: Rc<RefCell<Vec<Value>>> = Rc::new(RefCell::new(Vec::new()));
let registry: Rc<RefCell<dyn crate::func::Registry>> = crate::func::default_registry();
stack
.borrow_mut()
Expand All @@ -783,6 +785,7 @@ impl<'a> Context<'a> {
stack,
registry,
results,
step_results,
}
}

Expand Down Expand Up @@ -1132,9 +1135,9 @@ impl<'a> Context<'a> {
};

if filters.len() == 0 && found_match {
self.results
self.step_results
.borrow_mut()
.insert(0, current.value.clone());
.push(serde_json::Value::Object(obj.clone()));
}
self.stack.borrow_mut().push(StackItem::new(
Rc::new(v.clone()),
Expand Down Expand Up @@ -1163,6 +1166,22 @@ impl<'a> Context<'a> {
}
}

// no more filter to process
// push intermediate results
// such as from recursive search
// with pair (key, value) into
// final results
{
let sr = self.step_results.borrow();
if sr.len() > 0 {
let mut output: Vec<Value> = Vec::new();
for v in self.step_results.borrow().iter() {
output.insert(0, v.clone());
}
self.results.borrow_mut().push(Value::Array(output).into());
}
}

Ok(())
}
}
Expand Down Expand Up @@ -1529,4 +1548,17 @@ mod test {
]))]
);
}

#[test]
fn test_descendant_keyed() {
let data = serde_json::json!({"entry": {"values": [{"name": "gearbox"}, {"name": "gearbox", "test": "2000"}]}});
let result = Path::collect(data, ">/..('name'='gearbox')").unwrap();
assert_eq!(
*result.0.borrow(),
vec![Rc::new(Value::Array(vec![
serde_json::json!({"name": "gearbox"}),
serde_json::json!({"name": "gearbox", "test": "2000"})
]))]
);
}
}

0 comments on commit e06a557

Please sign in to comment.