From 74e79e6330c01f027ac6f772a7ceefdc807416ed Mon Sep 17 00:00:00 2001 From: freestrings Date: Sat, 21 Mar 2020 15:50:19 +0900 Subject: [PATCH] Empty result for filter expression returns array of null #39 --- src/select/mod.rs | 8 ++++---- tests/op.rs | 28 +++++++++++++--------------- tests/return_type.rs | 15 ++++++++++++--- 3 files changed, 29 insertions(+), 22 deletions(-) diff --git a/src/select/mod.rs b/src/select/mod.rs index ac639ea..cf5c312 100644 --- a/src/select/mod.rs +++ b/src/select/mod.rs @@ -220,7 +220,7 @@ impl<'a> FilterTerms<'a> { if tmp.is_empty() { self.0.pop(); - return Some(vec![&Value::Null]); + return Some(vec![]); } else { return Some(tmp); } @@ -275,7 +275,7 @@ impl<'a> FilterTerms<'a> { if tmp.is_empty() { self.0.pop(); - return Some(vec![&Value::Null]); + return Some(vec![]); } else { return Some(tmp); } @@ -530,7 +530,7 @@ impl<'a, 'b> Selector<'a, 'b> { } ExprTerm::Json(rel, _, v) => { if v.is_empty() { - self.current = Some(vec![&Value::Null]); + self.current = Some(vec![]); } else if let Some(vec) = rel { self.current = Some(vec); } else { @@ -538,7 +538,7 @@ impl<'a, 'b> Selector<'a, 'b> { } } ExprTerm::Bool(false) => { - self.current = Some(vec![&Value::Null]); + self.current = Some(vec![]); } _ => {} } diff --git a/tests/op.rs b/tests/op.rs index 1146ac0..1ade0dd 100644 --- a/tests/op.rs +++ b/tests/op.rs @@ -1,8 +1,6 @@ #[macro_use] extern crate serde_json; -use serde_json::Value; - use common::{read_json, select_and_then_compare, setup}; mod common; @@ -58,7 +56,7 @@ fn op_object_and_or() { select_and_then_compare( "$.friends[?( (@.id >= 2 || @.id == 1) && @.id == 0)]", read_json("./benchmark/data_obj.json"), - json!([Value::Null]), + json!([]), ); } @@ -202,7 +200,7 @@ fn op_lt_for_string_value() { setup(); select_and_then_compare( - r#"$.[?(@.a < "b")]"#, json!({ "a": "b" }), json!([Value::Null]), + r#"$.[?(@.a < "b")]"#, json!({ "a": "b" }), json!([]), ); } @@ -220,7 +218,7 @@ fn op_gt_for_string_value() { setup(); select_and_then_compare( - r#"$.[?(@.a > "b")]"#, json!({ "a": "b" }), json!([Value::Null]), + r#"$.[?(@.a > "b")]"#, json!({ "a": "b" }), json!([]), ); } @@ -251,7 +249,7 @@ fn op_ne_for_object_value() { select_and_then_compare( r#"$.[?(@.a != @.c)]"#, json!({"a": { "1": 1 }, "b": { "2": 2 }, "c": { "1": 1 }}), - json!([Value::Null]), + json!([]), ); } @@ -262,7 +260,7 @@ fn op_lt_for_object_value() { select_and_then_compare( r#"$.[?(@.a < @.c)]"#, json!({"a": { "1": 1 }, "b": { "2": 2 }, "c": { "1": 1 }}), - json!([Value::Null]), + json!([]), ); } @@ -273,7 +271,7 @@ fn op_le_for_object_value() { select_and_then_compare( r#"$.[?(@.a <= @.c)]"#, json!({"a": { "1": 1 }, "b": { "2": 2 }, "c": { "1": 1 }}), - json!([Value::Null]), + json!([]), ); } @@ -284,7 +282,7 @@ fn op_gt_for_object_value() { select_and_then_compare( r#"$.[?(@.a > @.c)]"#, json!({"a": { "1": 1 }, "b": { "2": 2 }, "c": { "1": 1 }}), - json!([Value::Null]), + json!([]), ); } @@ -295,7 +293,7 @@ fn op_ge_for_object_value() { select_and_then_compare( r#"$.[?(@.a >= @.c)]"#, json!({"a": { "1": 1 }, "b": { "2": 2 }, "c": { "1": 1 }}), - json!([Value::Null]), + json!([]), ); } @@ -306,7 +304,7 @@ fn op_eq_for_complex_value() { select_and_then_compare( r#"$.[?(1 == @.a)]"#, json!({ "a": { "b": 1 } }), - json!([Value::Null]), + json!([]), ); } @@ -317,7 +315,7 @@ fn op_ne_for_complex_value() { select_and_then_compare( r#"$.[?("1" != @.a)]"#, json!({ "a": { "b": 1 } }), - json!([Value::Null]), + json!([]), ); } @@ -328,7 +326,7 @@ fn op_le_for_complex_value() { select_and_then_compare( r#"$.[?(@.a <= 1)]"#, json!({ "a": { "b": 1 } }), - json!([Value::Null]), + json!([]), ); } @@ -339,7 +337,7 @@ fn op_gt_for_complex_value() { select_and_then_compare( r#"$.[?(@.a > "1")]"#, json!({ "a": { "b": 1 } }), - json!([Value::Null]), + json!([]), ); } @@ -355,7 +353,7 @@ fn op_compare_different_types() { ] .iter() { - select_and_then_compare(path, json!({}), json!([Value::Null])); + select_and_then_compare(path, json!({}), json!([])); } } diff --git a/tests/return_type.rs b/tests/return_type.rs index 04a5fed..0139dda 100644 --- a/tests/return_type.rs +++ b/tests/return_type.rs @@ -1,8 +1,6 @@ #[macro_use] extern crate serde_json; -use serde_json::Value; - use common::{read_json, select_and_then_compare, setup}; mod common; @@ -62,7 +60,7 @@ fn return_type_for_child_object_not_matched() { select_and_then_compare( "$.school[?(@.friends[10])]", read_json("./benchmark/data_obj.json"), - json!([Value::Null]), + json!([]), ); } @@ -96,4 +94,15 @@ fn return_type_for_array_filter_true() { {"id": 2, "name": "Gray Berry"} ]]), ); +} + +#[test] +fn return_type_empty() { + setup(); + + select_and_then_compare( + "$[?(@.key==43)]", + json!([{"key": 42}]), + json!([]), + ); } \ No newline at end of file