diff --git a/compiler.go b/compiler.go index 186042af..c7b5ee9a 100644 --- a/compiler.go +++ b/compiler.go @@ -1196,7 +1196,7 @@ func (c *compiler) funcModulemeta(v any, _ []any) any { if meta == nil { meta = make(map[string]any) } - var deps []any + deps := []any{} for _, i := range q.Imports { v := i.Meta.ToValue() if v == nil { diff --git a/execute.go b/execute.go index d43ef3e9..dcf9d984 100644 --- a/execute.go +++ b/execute.go @@ -436,7 +436,7 @@ func (env *env) pathIntact(v any) bool { } func (env *env) poppaths() []any { - var xs []any + xs := []any{} for { p := env.paths.pop().(pathValue) if p.path == nil { diff --git a/func.go b/func.go index 09bcfae9..6d586f84 100644 --- a/func.go +++ b/func.go @@ -573,7 +573,7 @@ func funcIndices(v, x any) any { } func indices(vs, xs []any) any { - var rs []any + rs := []any{} if len(xs) == 0 { return rs } @@ -1117,7 +1117,7 @@ func funcFlatten(v any, args []any) any { return &flattenDepthError{depth} } } - return flatten(nil, vs, depth) + return flatten([]any{}, vs, depth) } func flatten(xs, vs []any, depth float64) []any { @@ -1265,7 +1265,7 @@ func funcGroupBy(v, x any) any { if err != nil { return err } - var rs []any + rs := []any{} var last any for i, r := range items { if i == 0 || compare(last, r.key) != 0 { @@ -1290,7 +1290,7 @@ func uniqueBy(name string, v, x any) any { if err != nil { return err } - var rs []any + rs := []any{} var last any for i, r := range items { if i == 0 || compare(last, r.key) != 0 { diff --git a/module_loader.go b/module_loader.go index 6e9ba48c..599e37bf 100644 --- a/module_loader.go +++ b/module_loader.go @@ -84,7 +84,7 @@ func (l *moduleLoader) LoadJSONWithMeta(name string, meta map[string]any) (any, return nil, err } defer f.Close() - var vals []any + vals := []any{} dec := json.NewDecoder(f) dec.UseNumber() for { diff --git a/option_test.go b/option_test.go index 26f43b7f..a1bc581a 100644 --- a/option_test.go +++ b/option_test.go @@ -51,7 +51,7 @@ func TestWithModuleLoaderError(t *testing.T) { func TestWithModuleLoader_modulemeta(t *testing.T) { query, err := gojq.Parse(` - "module1" | modulemeta + "module1", "module2" | modulemeta `) if err != nil { t.Fatal(err) @@ -64,24 +64,34 @@ func TestWithModuleLoader_modulemeta(t *testing.T) { t.Fatal(err) } iter := code.Run(nil) + n := 0 for { got, ok := iter.Next() if !ok { break } - if expected := map[string]any{ - "deps": []any{ - map[string]any{ - "relpath": "module2", - "as": "foo", - "is_data": false, + if n == 0 { + if expected := map[string]any{ + "deps": []any{ + map[string]any{ + "relpath": "module2", + "as": "foo", + "is_data": false, + }, }, - }, - "name": "module1", - "test": 42, - }; !reflect.DeepEqual(got, expected) { - t.Errorf("expected: %v, got: %v", expected, got) + "name": "module1", + "test": 42, + }; !reflect.DeepEqual(got, expected) { + t.Errorf("expected: %v, got: %v", expected, got) + } + } else { + if expected := map[string]any{ + "deps": []any{}, // not a nil-slice + }; !reflect.DeepEqual(got, expected) { + t.Errorf("expected: %v, got: %v", expected, got) + } } + n++ } } diff --git a/query_test.go b/query_test.go index 32df5fec..87c4b527 100644 --- a/query_test.go +++ b/query_test.go @@ -250,6 +250,32 @@ func TestQueryRun_Input(t *testing.T) { } } +func TestQueryRun_NonNilSlice(t *testing.T) { + for _, f := range []string{"keys", "map(.)", "to_entries", "arrays", + "reverse", "flatten", "sort", "sort_by(.)", "group_by(.)", "unique", + "unique_by(.)", "transpose", "nth(.)", "indices([])", "path(.)"} { + t.Run(f, func(t *testing.T) { + query, err := gojq.Parse("[] | " + f) + if err != nil { + t.Fatal(err) + } + iter := query.Run(nil) + for { + v, ok := iter.Next() + if !ok { + break + } + if err, ok := v.(error); ok { + t.Fatal(err) + } + if expected := []any{}; !reflect.DeepEqual(v, expected) { + t.Errorf("expected: %#v, got: %#v", expected, v) + } + } + }) + } +} + func TestQueryRun_Race(t *testing.T) { query, err := gojq.Parse("range(10)") if err != nil {