Skip to content

Commit

Permalink
common/collections: Fix append regression to allow appending nil
Browse files Browse the repository at this point in the history
Closes #11180
  • Loading branch information
khayyamsaleem committed Jun 28, 2023
1 parent 793e38f commit b74b8d6
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
10 changes: 9 additions & 1 deletion common/collections/append.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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...)
Expand All @@ -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())
}
Expand Down
3 changes: 3 additions & 0 deletions common/collections/append_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down
60 changes: 60 additions & 0 deletions tpl/collections/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,63 @@ func TestAppendSliceToASliceOfSlices(t *testing.T) {
}

}

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 &lt;nil&gt;]")

}

}

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 &lt;nil&gt; c &lt;nil&gt;]")

}

}

0 comments on commit b74b8d6

Please sign in to comment.