1use crate::parser::model::{Comparable, Comparison, Literal, SingularQuery, SingularQuerySegment};
2use crate::query::context::JsonPathEvalContext;
3use crate::query::queryable::Queryable;
4use crate::query::state::{Data, Pointer, State};
5use crate::query::Query;
6
7impl Query for Comparison {
8 fn process_with_context<'a, T: Queryable>(
9 &self,
10 state: State<'a, T>,
11 ctx: &mut JsonPathEvalContext,
12 ) -> State<'a, T> {
13 let root = state.root;
14 let (lhs, rhs) = self.vals();
15 let lhs = lhs.process_with_context(state.clone(), ctx);
16 let rhs = rhs.process_with_context(state, ctx);
17 match self {
18 Comparison::Eq(..) => State::bool(eq(lhs, rhs), root),
19 Comparison::Ne(..) => State::bool(!eq(lhs, rhs), root),
20 Comparison::Gt(..) => State::bool(lt(rhs, lhs), root),
21 Comparison::Gte(..) => State::bool(lt(rhs.clone(), lhs.clone()) || eq(lhs, rhs), root),
22 Comparison::Lt(..) => State::bool(lt(lhs, rhs), root),
23 Comparison::Lte(..) => State::bool(lt(lhs.clone(), rhs.clone()) || eq(lhs, rhs), root),
24 }
25 }
26}
27
28fn lt<'a, T: Queryable>(lhs: State<'a, T>, rhs: State<'a, T>) -> bool {
29 let cmp = |lhs: &T, rhs: &T| {
30 let lhs_f64 = lhs.as_f64().or_else(|| lhs.as_i64().map(|v| v as f64));
31 let rhs_f64 = rhs.as_f64().or_else(|| rhs.as_i64().map(|v| v as f64));
32 if let (Some(lhs_num), Some(rhs_num)) = (lhs_f64, rhs_f64) {
33 lhs_num < rhs_num
34 } else if let (Some(lhs), Some(rhs)) = (lhs.as_str(), rhs.as_str()) {
35 lhs < rhs
36 } else {
37 false
38 }
39 };
40
41 match (lhs.data, rhs.data) {
42 (Data::Value(lhs), Data::Value(rhs)) => cmp(&lhs, &rhs),
43 (Data::Value(v), Data::Ref(p)) => cmp(&v, p.inner),
44 (Data::Ref(p), Data::Value(v)) => cmp(p.inner, &v),
45 (Data::Ref(lhs), Data::Ref(rhs)) => cmp(lhs.inner, rhs.inner),
46 _ => false,
47 }
48}
49
50fn eq<'a, T: Queryable>(lhs_state: State<'a, T>, rhs_state: State<'a, T>) -> bool {
51 match (lhs_state.data, rhs_state.data) {
52 (Data::Value(lhs), Data::Value(rhs)) => eq_json(&lhs, &rhs),
53 (Data::Value(v), Data::Ref(p)) => eq_json(&v, p.inner),
54 (Data::Ref(p), Data::Value(v)) => eq_json(&v, p.inner),
55 (Data::Ref(lhs), Data::Ref(rhs)) => eq_json(lhs.inner, rhs.inner),
56 (Data::Refs(lhs), Data::Refs(rhs)) => lhs == rhs,
57 (Data::Ref(r), Data::Refs(rhs)) => eq_ref_to_array(r, &rhs),
58 (Data::Nothing, Data::Nothing) => true,
59 _ => false,
60 }
61}
62fn eq_json<T: Queryable>(lhs: &T, rhs: &T) -> bool {
65 let lhs_f64 = lhs.as_f64().or_else(|| lhs.as_i64().map(|v| v as f64));
66 let rhs_f64 = rhs.as_f64().or_else(|| rhs.as_i64().map(|v| v as f64));
67
68 if let (Some(lhs_num), Some(rhs_num)) = (lhs_f64, rhs_f64) {
69 (lhs_num - rhs_num).abs() < f64::EPSILON
70 } else {
71 lhs == rhs
72 }
73}
74fn eq_ref_to_array<T: Queryable>(r: Pointer<T>, rhs: &Vec<Pointer<T>>) -> bool {
75 r.inner.as_array().map_or(false, |array| {
76 eq_arrays(array, &rhs.iter().map(|p| p.inner).collect::<Vec<_>>())
77 })
78}
79
80fn eq_arrays<T: Queryable>(lhs: &Vec<T>, rhs: &Vec<&T>) -> bool {
81 lhs.len() == rhs.len() && lhs.iter().zip(rhs.iter()).all(|(a, b)| eq_json(a, *b))
82}
83
84#[cfg(test)]
85mod tests {
86 use crate::parser::model::{
87 Comparable, Comparison, Literal, SingularQuery, SingularQuerySegment,
88 };
89 use crate::query::state::{Data, Pointer, State};
90 use crate::query::Query;
91 use crate::singular_query;
92 use crate::{cmp, comparable, lit, q_segment, q_segments};
93 use serde_json::json;
94 #[test]
95 fn eq_comp_val() {
96 let data = json!({"key": "value"});
97 let state = State::root(&data);
98
99 let comparison = Comparison::Eq(comparable!(lit!(s "key")), comparable!(lit!(s "key")));
100 let result = comparison.process(state);
101 assert_eq!(result.ok_val(), Some(json!(true)));
102 }
103
104 #[test]
105 fn eq_comp_ref() {
106 let data = json!({"key": "value"});
107 let state = State::root(&data);
108
109 let comparison = Comparison::Eq(
110 comparable!(lit!(s "value")),
111 comparable!(> singular_query!(@ key)),
112 );
113
114 let result = comparison.process(state);
115 assert_eq!(result.ok_val(), Some(json!(true)));
116 }
117
118 #[test]
119 fn eq_comp_queries() {
120 let data = json!({"key": "value", "key2": "value"});
121 let state = State::root(&data);
122
123 let comparison = Comparison::Eq(
124 comparable!(> singular_query!(@ key)),
125 comparable!(> singular_query!(key2)),
126 );
127 let result = comparison.process(state);
128 assert_eq!(result.ok_val(), Some(json!(true)));
129 }
130
131 #[test]
132 fn neq_comp_val() {
133 let data = json!({"key": "value"});
134 let state = State::root(&data);
135
136 let comparison = Comparison::Ne(comparable!(lit!(s "key")), comparable!(lit!(s "key")));
137 let result = comparison.process(state);
138 assert_eq!(result.ok_val(), Some(json!(false)));
139 }
140
141 #[test]
142 fn less_than() {
143 let data = json!({"key": 3});
144 let state = State::root(&data);
145
146 let comparison = Comparison::Lt(
147 comparable!(lit!(i 2)),
148 comparable!(> singular_query!(@ key)),
149 );
150 let result = comparison.process(state);
151 assert_eq!(result.ok_val(), Some(json!(true)));
152 }
153
154 #[test]
155 fn less_than_false() {
156 let data = json!({"key": 1});
157 let state = State::root(&data);
158
159 let comparison = Comparison::Lt(
160 comparable!(lit!(i 2)),
161 comparable!(> singular_query!(@ key)),
162 );
163 let result = comparison.process(state);
164 assert_eq!(result.ok_val(), Some(json!(false)));
165 }
166
167 #[test]
168 fn less_true() {
169 let data = json!({"key": 1});
170 let state = State::root(&data);
171
172 let comparison = Comparison::Lt(
173 comparable!(> singular_query!(@ key)),
174 comparable!(lit!(i 2)),
175 );
176 let result = comparison.process(state);
177 assert_eq!(result.ok_val(), Some(json!(true)));
178 }
179}