Skip to main content

jsonpath_rust/query/
atom.rs

1use crate::parser::model::FilterAtom;
2use crate::query::context::JsonPathEvalContext;
3use crate::query::queryable::Queryable;
4use crate::query::state::{Data, State};
5use crate::query::Query;
6
7impl Query for FilterAtom {
8    fn process_with_context<'a, T: Queryable>(
9        &self,
10        state: State<'a, T>,
11        ctx: &mut JsonPathEvalContext,
12    ) -> State<'a, T> {
13        match self {
14            FilterAtom::Filter { expr, not } => {
15                let bool_res = expr.process_with_context(state, ctx);
16                if *not {
17                    invert_bool(bool_res)
18                } else {
19                    bool_res
20                }
21            }
22            FilterAtom::Test { expr, not } => {
23                let new_state = |b| State::bool(b, state.root);
24                let res = expr.process_with_context(state.clone(), ctx);
25                if expr.is_res_bool() {
26                    if *not {
27                        invert_bool(res)
28                    } else {
29                        res
30                    }
31                } else {
32                    // RFC 9535 2.3.5.2: a test expression is true when the
33                    // nodelist it produces is non-empty. The value of the nodes
34                    // does not take part in the decision.
35                    let struct_presented = match res.data {
36                        Data::Ref(_) => true,
37                        Data::Refs(elems) => !elems.is_empty(),
38                        _ => false,
39                    };
40
41                    if struct_presented {
42                        new_state(!*not)
43                    } else {
44                        new_state(*not)
45                    }
46                }
47            }
48            FilterAtom::Comparison(cmp) => cmp.process_with_context(state, ctx),
49        }
50    }
51}
52
53fn invert_bool<T: Queryable>(state: State<T>) -> State<T> {
54    let root = state.root;
55    State::bool(
56        !state.ok_val().and_then(|v| v.as_bool()).unwrap_or_default(),
57        root,
58    )
59}
60
61#[cfg(test)]
62mod tests {
63    use crate::parser::model::Comparable;
64    use crate::parser::model::Literal;
65    use crate::parser::model::SingularQuery;
66    use crate::parser::model::SingularQuerySegment;
67    use crate::parser::model::{Comparison, FilterAtom};
68    use crate::q_segment;
69    use crate::query::queryable::Queryable;
70    use crate::query::state::State;
71    use crate::query::Query;
72    use crate::{atom, comparable, lit};
73    use crate::{cmp, singular_query};
74    use crate::{filter_, q_segments};
75    use serde_json::json;
76
77    #[test]
78    fn test_comparison() {
79        let json = json!({"i": 1});
80        let atom = atom!(comparable!(lit!(i 1)), ">=", comparable!(lit!(i 1)));
81        let state = State::root(&json);
82        let res = atom.process(state);
83        assert_eq!(res.ok_val().and_then(|v| v.as_bool()), Some(true));
84    }
85
86    #[test]
87    fn test_not_filter_atom() {
88        let json = json!({"a": 1 , "b": 2});
89        let state = State::root(&json);
90
91        let f1 = filter_!(atom!(
92            comparable!(> SingularQuery::Current(vec![])),
93            ">",
94            comparable!(lit!(i 2))
95        ));
96        let f2 = filter_!(atom!(
97            comparable!(> singular_query!(b)),
98            "!=",
99            comparable!(> singular_query!(a))
100        ));
101
102        let atom_or = atom!(!filter_!(or f1.clone(), f2.clone()));
103        let atom_and = atom!(!filter_!(and f1, f2));
104
105        assert_eq!(
106            atom_or
107                .process(state.clone())
108                .ok_val()
109                .and_then(|v| v.as_bool()),
110            Some(true)
111        );
112        assert_eq!(
113            atom_and.process(state).ok_val().and_then(|v| v.as_bool()),
114            Some(true)
115        );
116    }
117}