From b74b8d6478c7ad68c8f3fec6597b336f55ea0a94 Mon Sep 17 00:00:00 2001 From: khayyam Date: Wed, 28 Jun 2023 03:41:36 -0400 Subject: common/collections: Fix append regression to allow appending nil Closes #11180 --- common/collections/append.go | 10 +++++++++- common/collections/append_test.go | 3 +++ 2 files changed, 12 insertions(+), 1 deletion(-) (limited to 'common/collections') diff --git a/common/collections/append.go b/common/collections/append.go index 91abc46d3..8f1e21ea3 100644 --- a/common/collections/append.go +++ b/common/collections/append.go @@ -66,6 +66,10 @@ func Append(to any, from ...any) (any, error) { if len(from) == 1 { fromv := reflect.ValueOf(from[0]) + if !fromv.IsValid() { + // from[0] is nil + return appendToInterfaceSliceFromValues(tov, fromv) + } fromt := fromv.Type() if fromt.Kind() == reflect.Slice { fromt = fromt.Elem() @@ -94,7 +98,7 @@ func Append(to any, from ...any) (any, error) { for _, f := range from { fv := reflect.ValueOf(f) - if !fv.Type().AssignableTo(tot) { + if !fv.IsValid() || !fv.Type().AssignableTo(tot) { // Fall back to a []interface{} slice. tov, _ := indirect(reflect.ValueOf(to)) return appendToInterfaceSlice(tov, from...) @@ -109,6 +113,10 @@ func appendToInterfaceSliceFromValues(slice1, slice2 reflect.Value) ([]any, erro var tos []any for _, slice := range []reflect.Value{slice1, slice2} { + if !slice.IsValid() { + tos = append(tos, nil) + continue + } for i := 0; i < slice.Len(); i++ { tos = append(tos, slice.Index(i).Interface()) } diff --git a/common/collections/append_test.go b/common/collections/append_test.go index 415eb2f25..3c2aab2db 100644 --- a/common/collections/append_test.go +++ b/common/collections/append_test.go @@ -74,6 +74,9 @@ func TestAppend(t *testing.T) { []any{"c"}, false, }, + {[]string{"a", "b"}, []any{nil}, []any{"a", "b", nil}}, + {[]string{"a", "b"}, []any{nil, "d", nil}, []any{"a", "b", nil, "d", nil}}, + {[]any{"a", nil, "c"}, []any{"d", nil, "f"}, []any{"a", nil, "c", "d", nil, "f"}}, } { result, err := Append(test.start, test.addend...) -- cgit v1.2.3