Skip to content

Commit 77e3f96

Browse files
author
meir
committed
support boolean eq
1 parent 128c565 commit 77e3f96

File tree

3 files changed

+46
-14
lines changed

3 files changed

+46
-14
lines changed

src/json_path.rs

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -304,24 +304,50 @@ impl<'i, 'j, S:SelectValue> TermEvaluationResult<'i, 'j, S> {
304304
}
305305

306306
fn eq(&self, s: &Self) -> bool {
307-
if let (TermEvaluationResult::Value(v1), TermEvaluationResult::Value(v2)) = (self, s) {
308-
if v1 == v2 {
309-
true
310-
} else {
311-
false
307+
match (self, s){
308+
(TermEvaluationResult::Bool(b1), TermEvaluationResult::Bool(b2)) => {
309+
if *b1 == *b2 {
310+
true
311+
} else {
312+
false
313+
}
314+
}
315+
(TermEvaluationResult::Value(v1), TermEvaluationResult::Bool(b2)) => {
316+
if v1.get_type() == SelectValueType::Bool {
317+
let b1 = v1.get_bool();
318+
if b1 == *b2 {
319+
true
320+
} else {
321+
false
322+
}
323+
} else {
324+
false
325+
}
326+
}
327+
(TermEvaluationResult::Bool(_), TermEvaluationResult::Value(_)) => {
328+
s.eq(self)
329+
}
330+
(TermEvaluationResult::Value(v1), TermEvaluationResult::Value(v2)) => {
331+
if v1 == v2 {
332+
true
333+
} else {
334+
false
335+
}
336+
}
337+
(_, _) => {
338+
match self.cmp(s) {
339+
CmpResult::Ord(o) => o.is_eq(),
340+
CmpResult::NotCmparable => false,
341+
}
312342
}
313-
} else {
314-
match self.cmp(s) {
315-
CmpResult::Ord(o) => o.is_eq(),
316-
CmpResult::NotCmparable => false,
317-
}
318343
}
319344
}
320345

321346
fn ne(&self, s: &Self) -> bool {
322-
match self.cmp(s) {
323-
CmpResult::Ord(o) => o.is_ne(),
324-
CmpResult::NotCmparable => false,
347+
if self.eq(s) {
348+
false
349+
} else {
350+
true
325351
}
326352
}
327353

src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,12 @@ mod json_path_tests {
372372
verify_json!(path:"$*[?(@.code==\"2\")].code", json:[{"code":"1"},{"code":"2"}], results:["2"]);
373373
}
374374

375+
#[test]
376+
fn test_filter_bool() {
377+
verify_json!(path:"$.*[?(@==true)]", json:{"a":true, "b":false}, results:[true]);
378+
verify_json!(path:"$.*[?(@==false)]", json:{"a":true, "b":false}, results:[false]);
379+
}
380+
375381
#[test]
376382
fn test_complex_filter_from_root() {
377383
verify_json!(path:"$.bar.*[?@ == $.foo]",

tests/op.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ fn op_ne_for_complex_value() {
315315
select_and_then_compare(
316316
r#"$.[?("1" != @.a)]"#,
317317
json!({ "a": { "b": 1 } }),
318-
json!([]),
318+
json!([{ "a": { "b": 1 } }]),
319319
);
320320
}
321321

0 commit comments

Comments
 (0)