Skip to content

Commit

Permalink
common/collections: Fix type checking in Append
Browse files Browse the repository at this point in the history
The fix introduced in Hugo `0.49.1` had an unintended side-effect in the `Append` func used in both `append` and `.Scratch.Add`.

This commit fixes that by loosen/fixing the type checking so concrete types can be appended to interface slices.

Fixes gohugoio#5303
  • Loading branch information
bep committed Oct 11, 2018
1 parent 3583dd6 commit f6c1c76
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
3 changes: 2 additions & 1 deletion common/collections/append.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ func Append(to interface{}, from ...interface{}) (interface{}, error) {

for _, f := range from {
fv := reflect.ValueOf(f)
if tot != fv.Type() {

if !fv.Type().AssignableTo(tot) {
return nil, fmt.Errorf("append element type mismatch: expected %v, got %v", tot, fv.Type())
}
tov = reflect.Append(tov, fv)
Expand Down
6 changes: 6 additions & 0 deletions common/collections/append_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ func TestAppend(t *testing.T) {
tstSlicers{&tstSlicer{"a"},
&tstSlicer{"b"},
&tstSlicer{"c"}}},
{testSlicerInterfaces{&tstSlicerIn1{"a"}, &tstSlicerIn1{"b"}},
[]interface{}{&tstSlicerIn1{"c"}},
testSlicerInterfaces{&tstSlicerIn1{"a"}, &tstSlicerIn1{"b"}, &tstSlicerIn1{"c"}}},
// Errors
{testSlicerInterfaces{&tstSlicerIn1{"a"}, &tstSlicerIn1{"b"}},
[]interface{}{"c"},
false},
{"", []interface{}{[]string{"a", "b"}}, false},
// No string concatenation.
{"ab",
Expand Down
1 change: 1 addition & 0 deletions common/collections/slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ func TestSlice(t *testing.T) {
{[]interface{}{&tstSlicerIn1{"a"}, &tstSlicerIn2{"b"}}, testSlicerInterfaces{&tstSlicerIn1{"a"}, &tstSlicerIn2{"b"}}},
{[]interface{}{&tstSlicerIn1{"a"}, &tstSlicer{"b"}}, []interface{}{&tstSlicerIn1{"a"}, &tstSlicer{"b"}}},
} {

errMsg := fmt.Sprintf("[%d] %v", i, test.args)

result := Slice(test.args...)
Expand Down

0 comments on commit f6c1c76

Please sign in to comment.