Skip to content

Commit

Permalink
fix functions returning arrays not to emit nil slices
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed May 29, 2023
1 parent 4a681c8 commit 96c4843
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 19 deletions.
2 changes: 1 addition & 1 deletion compiler.go
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion execute.go
Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions func.go
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion module_loader.go
Expand Up @@ -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 {
Expand Down
34 changes: 22 additions & 12 deletions option_test.go
Expand Up @@ -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)
Expand All @@ -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++
}
}

Expand Down
26 changes: 26 additions & 0 deletions query_test.go
Expand Up @@ -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 {
Expand Down

0 comments on commit 96c4843

Please sign in to comment.