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()
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...)
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())
}
[]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...)
}
}
+
+func TestAppendNilToSlice(t *testing.T) {
+
+ t.Parallel()
+
+ files := `
+-- hugo.toml --
+-- layouts/index.html --
+{{ $obj := (slice "a") }}
+{{ $obj = $obj | append nil }}
+
+{{ $obj }}
+
+
+ `
+
+ for i := 0; i < 4; i++ {
+
+ b := hugolib.NewIntegrationTestBuilder(
+ hugolib.IntegrationTestConfig{
+ T: t,
+ TxtarString: files,
+ },
+ ).Build()
+
+ b.AssertFileContent("public/index.html", "[a <nil>]")
+
+ }
+
+}
+
+func TestAppendNilsToSliceWithNils(t *testing.T) {
+
+ t.Parallel()
+
+ files := `
+-- hugo.toml --
+-- layouts/index.html --
+{{ $obj := (slice "a" nil "c") }}
+{{ $obj = $obj | append nil }}
+
+{{ $obj }}
+
+
+ `
+
+ for i := 0; i < 4; i++ {
+
+ b := hugolib.NewIntegrationTestBuilder(
+ hugolib.IntegrationTestConfig{
+ T: t,
+ TxtarString: files,
+ },
+ ).Build()
+
+ b.AssertFileContent("public/index.html", "[a <nil> c <nil>]")
+
+ }
+
+}