From f6c1c76438a873a52d4180efc3df89be49d1d12c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Thu, 11 Oct 2018 11:05:30 +0200 Subject: [PATCH] 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 --- common/collections/append.go | 3 ++- common/collections/append_test.go | 6 ++++++ common/collections/slice_test.go | 1 + 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/common/collections/append.go b/common/collections/append.go index e1008843b96..56bfe82631a 100644 --- a/common/collections/append.go +++ b/common/collections/append.go @@ -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) diff --git a/common/collections/append_test.go b/common/collections/append_test.go index e3361fb26be..f89ec60f0b7 100644 --- a/common/collections/append_test.go +++ b/common/collections/append_test.go @@ -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", diff --git a/common/collections/slice_test.go b/common/collections/slice_test.go index 1103e2feaa2..b37b6634ffb 100644 --- a/common/collections/slice_test.go +++ b/common/collections/slice_test.go @@ -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...)