common/collections: Fix type checking in Append

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 #5303
This commit is contained in:
Bjørn Erik Pedersen 2018-10-11 11:05:30 +02:00
parent 3583dd6d71
commit 535755e4f8
No known key found for this signature in database
GPG key ID: 330E6E2BD4859D8F
2 changed files with 7 additions and 1 deletions

View file

@ -59,7 +59,7 @@ func Append(to interface{}, from ...interface{}) (interface{}, error) {
for _, f := range from { for _, f := range from {
fv := reflect.ValueOf(f) 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()) return nil, fmt.Errorf("append element type mismatch: expected %v, got %v", tot, fv.Type())
} }
tov = reflect.Append(tov, fv) tov = reflect.Append(tov, fv)

View file

@ -43,7 +43,13 @@ func TestAppend(t *testing.T) {
tstSlicers{&tstSlicer{"a"}, tstSlicers{&tstSlicer{"a"},
&tstSlicer{"b"}, &tstSlicer{"b"},
&tstSlicer{"c"}}}, &tstSlicer{"c"}}},
{testSlicerInterfaces{&tstSlicerIn1{"a"}, &tstSlicerIn1{"b"}},
[]interface{}{&tstSlicerIn1{"c"}},
testSlicerInterfaces{&tstSlicerIn1{"a"}, &tstSlicerIn1{"b"}, &tstSlicerIn1{"c"}}},
// Errors // Errors
{testSlicerInterfaces{&tstSlicerIn1{"a"}, &tstSlicerIn1{"b"}},
[]interface{}{"c"},
false},
{"", []interface{}{[]string{"a", "b"}}, false}, {"", []interface{}{[]string{"a", "b"}}, false},
// No string concatenation. // No string concatenation.
{"ab", {"ab",