Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions internal/concat.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,18 +205,30 @@ func ConcatSliceValue(val reflect.Value) (reflect.Value, error) {
}

func toSliceValue(vs []any) (reflect.Value, error) {
typ := reflect.TypeOf(vs[0])
// Filter out nil elements and find the first non-nil element's type.
var nonNil []any
for _, v := range vs {
if v != nil {
nonNil = append(nonNil, v)
}
}

ret := reflect.MakeSlice(reflect.SliceOf(typ), len(vs), len(vs))
ret.Index(0).Set(reflect.ValueOf(vs[0]))
if len(nonNil) == 0 {
// All elements are nil; return an empty []any slice.
return reflect.ValueOf([]any{}), nil
}

for i := 1; i < len(vs); i++ {
v := vs[i]
typ := reflect.TypeOf(nonNil[0])
for i, v := range nonNil {
vt := reflect.TypeOf(v)
if typ != vt {
return reflect.Value{}, fmt.Errorf("unexpected slice element type. Got %v, expected %v", typ, vt)
}
_ = i
}

ret := reflect.MakeSlice(reflect.SliceOf(typ), len(nonNil), len(nonNil))
for i, v := range nonNil {
ret.Index(i).Set(reflect.ValueOf(v))
}

Expand Down
24 changes: 24 additions & 0 deletions internal/concat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,28 @@ func TestConcat(t *testing.T) {
},
}, m)
})

t.Run("concat map with nil value in first chunk and non-nil in second", func(t *testing.T) {
c1 := map[string]any{"key": nil}
c2 := map[string]any{"key": "value"}
m, err := ConcatItems([]map[string]any{c1, c2})
assert.Nil(t, err)
assert.Equal(t, map[string]any{"key": "value"}, m)
})

t.Run("concat map with non-nil value in first chunk and nil in second", func(t *testing.T) {
c1 := map[string]any{"key": "value"}
c2 := map[string]any{"key": nil}
m, err := ConcatItems([]map[string]any{c1, c2})
assert.Nil(t, err)
assert.Equal(t, map[string]any{"key": "value"}, m)
})

t.Run("concat flat map with mixed nil values", func(t *testing.T) {
c1 := map[string]any{"a": nil, "b": "hello"}
c2 := map[string]any{"a": "world", "b": "hello2"}
m, err := ConcatItems([]map[string]any{c1, c2})
assert.Nil(t, err)
assert.Equal(t, map[string]any{"a": "world", "b": "hellohello2"}, m)
})
}