Skip to content

Commit 1314c4b

Browse files
committed
fix: allow nil to be considered false
updated getBoolean to return false with nil arguments updated consensus tests
1 parent fd6f279 commit 1314c4b

File tree

6 files changed

+60
-33
lines changed

6 files changed

+60
-33
lines changed

script/standard/logic_operators_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ func Test_notOperator(t *testing.T) {
522522
},
523523
},
524524
expected: operatorTestExpected{
525-
err: "invalid argument. is nil",
525+
value: true,
526526
},
527527
},
528528
{

script/standard/operators.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func getNumber(argument interface{}, parameters map[string]interface{}) (float64
7373

7474
func getBoolean(argument interface{}, parameters map[string]interface{}) (bool, error) {
7575
if argument == nil {
76-
return false, errInvalidArgumentNil
76+
return false, nil
7777
}
7878
if parameters == nil {
7979
parameters = make(map[string]interface{})
@@ -93,6 +93,10 @@ func getBoolean(argument interface{}, parameters map[string]interface{}) (bool,
9393
}
9494
}
9595

96+
if argument == nil {
97+
return false, nil
98+
}
99+
96100
str := fmt.Sprintf("%v", argument)
97101
boolValue, err := strconv.ParseBool(str)
98102
if err != nil {

script/standard/operators_test.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ func Test_getNumber(t *testing.T) {
213213

214214
func Test_getBoolean(t *testing.T) {
215215

216+
currentSelector, _ := newSelectorOperator("@", &ScriptEngine{}, nil)
217+
216218
type input struct {
217219
argument interface{}
218220
parameters map[string]interface{}
@@ -230,7 +232,7 @@ func Test_getBoolean(t *testing.T) {
230232
{
231233
input: input{},
232234
expected: expected{
233-
err: "invalid argument. is nil",
235+
value: false,
234236
},
235237
},
236238
{
@@ -287,6 +289,25 @@ func Test_getBoolean(t *testing.T) {
287289
value: true,
288290
},
289291
},
292+
{
293+
input: input{
294+
argument: currentSelector,
295+
},
296+
expected: expected{
297+
value: false,
298+
},
299+
},
300+
{
301+
input: input{
302+
argument: "null",
303+
parameters: map[string]interface{}{
304+
"null": nil,
305+
},
306+
},
307+
expected: expected{
308+
value: false,
309+
},
310+
},
290311
}
291312

292313
for idx, test := range tests {

test/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,17 +217,17 @@ This implementation would be closer to the 'Scalar consensus' as it does not alw
217217
|:question:|`$[?(@.key>42)]`|`[ {"key": 0}, {"key": 42}, {"key": -1}, {"key": 41}, {"key": 43}, {"key": 42.0001}, {"key": 41.9999}, {"key": 100}, {"key": "43"}, {"key": "42"}, {"key": "41"}, {"key": "value"}, {"some": "value"} ]`|none|`[{"key":43},{"key":42.0001},{"key":100}]`|
218218
|:question:|`$[?(@.key>=42)]`|`[ {"key": 0}, {"key": 42}, {"key": -1}, {"key": 41}, {"key": 43}, {"key": 42.0001}, {"key": 41.9999}, {"key": 100}, {"key": "43"}, {"key": "42"}, {"key": "41"}, {"key": "value"}, {"some": "value"} ]`|none|`[{"key":42},{"key":43},{"key":42.0001},{"key":100}]`|
219219
|:no_entry:|`$[?(@.d in [2, 3])]`|`[{"d": 1}, {"d": 2}, {"d": 1}, {"d": 3}, {"d": 4}]`|`nil`|`[]`|
220-
|:white_check_mark:|`$[?(2 in @.d)]`|`[{"d": [1, 2, 3]}, {"d": [2]}, {"d": [1]}, {"d": [3, 4]}, {"d": [4, 2]}]`|`nil`|`null`|
220+
|:no_entry:|`$[?(2 in @.d)]`|`[{"d": [1, 2, 3]}, {"d": [2]}, {"d": [1]}, {"d": [3, 4]}, {"d": [4, 2]}]`|`nil`|`[{"d":[1,2,3]},{"d":[2]},{"d":[1]},{"d":[3,4]},{"d":[4,2]}]`|
221221
|:question:|`$[?(@.key<42)]`|`[ {"key": 0}, {"key": 42}, {"key": -1}, {"key": 41}, {"key": 43}, {"key": 42.0001}, {"key": 41.9999}, {"key": 100}, {"key": "43"}, {"key": "42"}, {"key": "41"}, {"key": "value"}, {"some": "value"} ]`|none|`[{"key":0},{"key":-1},{"key":41},{"key":41.9999}]`|
222222
|:question:|`$[?(@.key<=42)]`|`[ {"key": 0}, {"key": 42}, {"key": -1}, {"key": 41}, {"key": 43}, {"key": 42.0001}, {"key": 41.9999}, {"key": 100}, {"key": "43"}, {"key": "42"}, {"key": "41"}, {"key": "value"}, {"some": "value"} ]`|none|`[{"key":0},{"key":42},{"key":-1},{"key":41},{"key":41.9999}]`|
223223
|:question:|`$[?(@.key*2==100)]`|`[{"key": 60}, {"key": 50}, {"key": 10}, {"key": -50}, {"key*2": 100}]`|none|`[{"key":50}]`|
224-
|:question:|`$[?(!(@.key==42))]`|`[ {"key": 0}, {"key": 42}, {"key": -1}, {"key": 41}, {"key": 43}, {"key": 42.0001}, {"key": 41.9999}, {"key": 100}, {"key": "43"}, {"key": "42"}, {"key": "41"}, {"key": "value"}, {"some": "value"} ]`|none|`[{"key":0},{"key":42},{"key":-1},{"key":41},{"key":43},{"key":42.0001},{"key":41.9999},{"key":100},{"key":"43"},{"key":"42"},{"key":"41"},{"key":"value"},{"some":"value"}]`|
225-
|:question:|`$[?(!(@.key<42))]`|`[ {"key": 0}, {"key": 42}, {"key": -1}, {"key": 41}, {"key": 43}, {"key": 42.0001}, {"key": 41.9999}, {"key": 100}, {"key": "43"}, {"key": "42"}, {"key": "41"}, {"key": "value"}, {"some": "value"} ]`|none|`[{"key":0},{"key":42},{"key":-1},{"key":41},{"key":43},{"key":42.0001},{"key":41.9999},{"key":100},{"key":"43"},{"key":"42"},{"key":"41"},{"key":"value"},{"some":"value"}]`|
226-
|:question:|`$[?([email protected])]`|`[ { "some": "some value" }, { "key": true }, { "key": false }, { "key": null }, { "key": "value" }, { "key": "" }, { "key": 0 }, { "key": 1 }, { "key": -1 }, { "key": 42 }, { "key": {} }, { "key": [] } ]`|none|`null`|
224+
|:question:|`$[?(!(@.key==42))]`|`[ {"key": 0}, {"key": 42}, {"key": -1}, {"key": 41}, {"key": 43}, {"key": 42.0001}, {"key": 41.9999}, {"key": 100}, {"key": "43"}, {"key": "42"}, {"key": "41"}, {"key": "value"}, {"some": "value"} ]`|none|`[{"key":0},{"key":-1},{"key":41},{"key":43},{"key":42.0001},{"key":41.9999},{"key":100},{"key":"43"},{"key":"42"},{"key":"41"},{"key":"value"}]`|
225+
|:question:|`$[?(!(@.key<42))]`|`[ {"key": 0}, {"key": 42}, {"key": -1}, {"key": 41}, {"key": 43}, {"key": 42.0001}, {"key": 41.9999}, {"key": 100}, {"key": "43"}, {"key": "42"}, {"key": "41"}, {"key": "value"}, {"some": "value"} ]`|none|`[{"key":42},{"key":43},{"key":42.0001},{"key":100}]`|
226+
|:question:|`$[?([email protected])]`|`[ { "some": "some value" }, { "key": true }, { "key": false }, { "key": null }, { "key": "value" }, { "key": "" }, { "key": 0 }, { "key": 1 }, { "key": -1 }, { "key": 42 }, { "key": {} }, { "key": [] } ]`|none|`[{"key":false},{"key":null},{"key":0}]`|
227227
|:question:|`$[?(@.key!=42)]`|`[ {"key": 0}, {"key": 42}, {"key": -1}, {"key": 1}, {"key": 41}, {"key": 43}, {"key": 42.0001}, {"key": 41.9999}, {"key": 100}, {"key": "some"}, {"key": "42"}, {"key": null}, {"key": 420}, {"key": ""}, {"key": {}}, {"key": []}, {"key": [42]}, {"key": {"key": 42}}, {"key": {"some": 42}}, {"some": "value"} ]`|none|`[{"key":0},{"key":-1},{"key":1},{"key":41},{"key":43},{"key":42.0001},{"key":41.9999},{"key":100},{"key":"some"},{"key":"42"},{"key":null},{"key":420},{"key":""},{"key":{}},{"key":[]},{"key":[42]},{"key":{"key":42}},{"key":{"some":42}}]`|
228228
|:no_entry:|`$[*].bookmarks[?(@.page == 45)]^^^`|`[ { "title": "Sayings of the Century", "bookmarks": [{ "page": 40 }] }, { "title": "Sword of Honour", "bookmarks": [ { "page": 35 }, { "page": 45 } ] }, { "title": "Moby Dick", "bookmarks": [ { "page": 3035 }, { "page": 45 } ] } ]`|`nil`|`[[],[],[]]`|
229229
|:question:|`$[?(@.name=~/hello.*/)]`|`[ {"name": "hullo world"}, {"name": "hello world"}, {"name": "yes hello world"}, {"name": "HELLO WORLD"}, {"name": "good bye"} ]`|none|`[]`|
230-
|:question:|`$[?(@.name=~/@.pattern/)]`|`[ {"name": "hullo world"}, {"name": "hello world"}, {"name": "yes hello world"}, {"name": "HELLO WORLD"}, {"name": "good bye"}, {"pattern": "hello.*"} ]`|none|`null`|
230+
|:question:|`$[?(@.name=~/@.pattern/)]`|`[ {"name": "hullo world"}, {"name": "hello world"}, {"name": "yes hello world"}, {"name": "HELLO WORLD"}, {"name": "good bye"}, {"pattern": "hello.*"} ]`|none|`[]`|
231231
|:question:|`$[?(@[*]>=4)]`|`[[1,2],[3,4],[5,6]]`|none|`[]`|
232232
|:question:|`$.x[?(@[*]>=$.y[*])]`|`{"x":[[1,2],[3,4],[5,6]],"y":[3,4,5]}`|none|`[]`|
233233
|:no_entry:|`$[?(@.key=42)]`|`[ {"key": 0}, {"key": 42}, {"key": -1}, {"key": 1}, {"key": 41}, {"key": 43}, {"key": 42.0001}, {"key": 41.9999}, {"key": 100}, {"key": "some"}, {"key": "42"}, {"key": null}, {"key": 420}, {"key": ""}, {"key": {}}, {"key": []}, {"key": [42]}, {"key": {"key": 42}}, {"key": {"some": 42}}, {"some": "value"} ]`|`nil`|`[]`|
@@ -268,7 +268,7 @@ This implementation would be closer to the 'Scalar consensus' as it does not alw
268268
|:white_check_mark:|`$[0,1]`|`["first", "second", "third"]`|`["first","second"]`|`["first","second"]`|
269269
|:white_check_mark:|`$[0,0]`|`["a"]`|`["a","a"]`|`["a","a"]`|
270270
|:white_check_mark:|`$['a','a']`|`{"a":1}`|`[1,1]`|`[1,1]`|
271-
|:question:|`$[?(@.key<3),?(@.key>6)]`|`[{"key": 1}, {"key": 8}, {"key": 3}, {"key": 10}, {"key": 7}, {"key": 2}, {"key": 6}, {"key": 4}]`|none|`null`|
271+
|:question:|`$[?(@.key<3),?(@.key>6)]`|`[{"key": 1}, {"key": 8}, {"key": 3}, {"key": 10}, {"key": 7}, {"key": 2}, {"key": 6}, {"key": 4}]`|none|`[]`|
272272
|:white_check_mark:|`$['key','another']`|`{ "key": "value", "another": "entry" }`|`["value","entry"]`|`["value","entry"]`|
273273
|:white_check_mark:|`$['missing','key']`|`{ "key": "value", "another": "entry" }`|`["value"]`|`["value"]`|
274274
|:no_entry:|`$[:]['c','d']`|`[{"c":"cc1","d":"dd1","e":"ee1"},{"c":"cc2","d":"dd2","e":"ee2"}]`|`["cc1","dd1","cc2","dd2"]`|`[["cc1","dd1"],["cc2","dd2"]]`|

test/filter_test.go

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -443,11 +443,17 @@ var filterTests []testData = []testData{
443443
expectedError: "",
444444
},
445445
{
446-
selector: `$[?(2 in @.d)]`, // TODO : in keywork will not work
447-
data: `[{"d": [1, 2, 3]}, {"d": [2]}, {"d": [1]}, {"d": [3, 4]}, {"d": [4, 2]}]`,
448-
expected: nil,
446+
selector: `$[?(2 in @.d)]`, // TODO : in keywork will not work
447+
data: `[{"d": [1, 2, 3]}, {"d": [2]}, {"d": [1]}, {"d": [3, 4]}, {"d": [4, 2]}]`,
448+
expected: []interface{}{
449+
map[string]interface{}{"d": []interface{}{float64(1), float64(2), float64(3)}},
450+
map[string]interface{}{"d": []interface{}{float64(2)}},
451+
map[string]interface{}{"d": []interface{}{float64(1)}},
452+
map[string]interface{}{"d": []interface{}{float64(3), float64(4)}},
453+
map[string]interface{}{"d": []interface{}{float64(4), float64(2)}},
454+
},
449455
consensus: nil,
450-
expectedError: "invalid expression. unexpected token '2' at index 0",
456+
expectedError: "",
451457
},
452458
{
453459
selector: `$[?(@.key<42)]`,
@@ -486,7 +492,6 @@ var filterTests []testData = []testData{
486492
data: `[ {"key": 0}, {"key": 42}, {"key": -1}, {"key": 41}, {"key": 43}, {"key": 42.0001}, {"key": 41.9999}, {"key": 100}, {"key": "43"}, {"key": "42"}, {"key": "41"}, {"key": "value"}, {"some": "value"} ]`,
487493
expected: []interface{}{
488494
map[string]interface{}{"key": float64(0)},
489-
map[string]interface{}{"key": float64(42)},
490495
map[string]interface{}{"key": float64(-1)},
491496
map[string]interface{}{"key": float64(41)},
492497
map[string]interface{}{"key": float64(43)},
@@ -497,38 +502,35 @@ var filterTests []testData = []testData{
497502
map[string]interface{}{"key": "42"},
498503
map[string]interface{}{"key": "41"},
499504
map[string]interface{}{"key": "value"},
500-
map[string]interface{}{"some": "value"},
505+
// map[string]interface{}{"some": "value"}, // TODO : it should include this
501506
},
502507
consensus: consensusNone,
503508
expectedError: "",
504509
},
505510
{
511+
// TODO : should include those without key fields
506512
selector: `$[?(!(@.key<42))]`,
507513
data: `[ {"key": 0}, {"key": 42}, {"key": -1}, {"key": 41}, {"key": 43}, {"key": 42.0001}, {"key": 41.9999}, {"key": 100}, {"key": "43"}, {"key": "42"}, {"key": "41"}, {"key": "value"}, {"some": "value"} ]`,
508-
expected: []interface{}{ // TODO : wrong
509-
map[string]interface{}{"key": float64(0)},
514+
expected: []interface{}{
510515
map[string]interface{}{"key": float64(42)},
511-
map[string]interface{}{"key": float64(-1)},
512-
map[string]interface{}{"key": float64(41)},
513516
map[string]interface{}{"key": float64(43)},
514517
map[string]interface{}{"key": float64(42.0001)},
515-
map[string]interface{}{"key": float64(41.9999)},
516518
map[string]interface{}{"key": float64(100)},
517-
map[string]interface{}{"key": "43"},
518-
map[string]interface{}{"key": "42"},
519-
map[string]interface{}{"key": "41"},
520-
map[string]interface{}{"key": "value"},
521-
map[string]interface{}{"some": "value"},
522519
},
523520
consensus: consensusNone,
524521
expectedError: "",
525522
},
526523
{
527-
selector: `$[?([email protected])]`, // TODO : not ! support
528-
data: `[ { "some": "some value" }, { "key": true }, { "key": false }, { "key": null }, { "key": "value" }, { "key": "" }, { "key": 0 }, { "key": 1 }, { "key": -1 }, { "key": 42 }, { "key": {} }, { "key": [] } ]`,
529-
expected: nil,
524+
// TODO : this should also include {some: some value} and the null and empty
525+
selector: `$[?([email protected])]`,
526+
data: `[ { "some": "some value" }, { "key": true }, { "key": false }, { "key": null }, { "key": "value" }, { "key": "" }, { "key": 0 }, { "key": 1 }, { "key": -1 }, { "key": 42 }, { "key": {} }, { "key": [] } ]`,
527+
expected: []interface{}{
528+
map[string]interface{}{"key": false},
529+
map[string]interface{}{"key": nil},
530+
map[string]interface{}{"key": float64(0)},
531+
},
530532
consensus: consensusNone,
531-
expectedError: "invalid expression. unexpected token '!' at index 0",
533+
expectedError: "",
532534
},
533535
{
534536
selector: `$[?(@.key!=42)]`,
@@ -573,9 +575,9 @@ var filterTests []testData = []testData{
573575
{
574576
selector: `$[?(@.name=~/@.pattern/)]`,
575577
data: `[ {"name": "hullo world"}, {"name": "hello world"}, {"name": "yes hello world"}, {"name": "HELLO WORLD"}, {"name": "good bye"}, {"pattern": "hello.*"} ]`,
576-
expected: nil,
578+
expected: []interface{}{},
577579
consensus: consensusNone,
578-
expectedError: "invalid expression. unexpected token '/' at index 0",
580+
expectedError: "",
579581
},
580582
{
581583
selector: `$[?(@[*]>=4)]`,

test/union_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ var unionTests []testData = []testData{
3131
{
3232
selector: `$[?(@.key<3),?(@.key>6)]`,
3333
data: `[{"key": 1}, {"key": 8}, {"key": 3}, {"key": 10}, {"key": 7}, {"key": 2}, {"key": 6}, {"key": 4}]`,
34-
expected: nil,
34+
expected: []interface{}{},
3535
consensus: consensusNone,
36-
expectedError: "invalid expression. unexpected token '3' at index 0",
36+
expectedError: "",
3737
},
3838
{
3939
selector: `$['key','another']`,

0 commit comments

Comments
 (0)