common/collections: Fix type checking in Append
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 11 Oct 2018 09:05:30 +0000 (11:05 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 11 Oct 2018 09:24:10 +0000 (11:24 +0200)
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
common/collections/append_test.go

index e1008843b965150f50d0b6ea4b63aafcd04be424..97bf849889649555a60ea2c56cefa3e25a9a0769 100644 (file)
@@ -59,7 +59,7 @@ 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)
index e3361fb26be9d821e084f2b236da8c2c6e348caf..f89ec60f0b72b5b7df28318b6fc0f9c2d652ca06 100644 (file)
@@ -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",