1
0
Fork 0
mirror of https://github.com/gohugoio/hugo.git synced 2025-04-21 21:28:16 +00:00

common: Fix elements are doubling when append a not assignable type

Fixes 
This commit is contained in:
Vazrupe (HyeonGyu Lee) 2019-10-09 18:36:25 +09:00 committed by Bjørn Erik Pedersen
parent 096a4b67b9
commit a9762b5c48
2 changed files with 3 additions and 0 deletions
common/collections

View file

@ -65,6 +65,7 @@ func Append(to interface{}, from ...interface{}) (interface{}, error) {
fv := reflect.ValueOf(f) fv := reflect.ValueOf(f)
if !fv.Type().AssignableTo(tot) { if !fv.Type().AssignableTo(tot) {
// Fall back to a []interface{} slice. // Fall back to a []interface{} slice.
tov, _ := indirect(reflect.ValueOf(to))
return appendToInterfaceSlice(tov, from...) return appendToInterfaceSlice(tov, from...)
} }
tov = reflect.Append(tov, fv) tov = reflect.Append(tov, fv)

View file

@ -14,6 +14,7 @@
package collections package collections
import ( import (
"html/template"
"testing" "testing"
qt "github.com/frankban/quicktest" qt "github.com/frankban/quicktest"
@ -31,6 +32,7 @@ func TestAppend(t *testing.T) {
{[]string{"a", "b"}, []interface{}{"c"}, []string{"a", "b", "c"}}, {[]string{"a", "b"}, []interface{}{"c"}, []string{"a", "b", "c"}},
{[]string{"a", "b"}, []interface{}{"c", "d", "e"}, []string{"a", "b", "c", "d", "e"}}, {[]string{"a", "b"}, []interface{}{"c", "d", "e"}, []string{"a", "b", "c", "d", "e"}},
{[]string{"a", "b"}, []interface{}{[]string{"c", "d", "e"}}, []string{"a", "b", "c", "d", "e"}}, {[]string{"a", "b"}, []interface{}{[]string{"c", "d", "e"}}, []string{"a", "b", "c", "d", "e"}},
{[]string{"a"}, []interface{}{"b", template.HTML("c")}, []interface{}{"a", "b", template.HTML("c")}},
{nil, []interface{}{"a", "b"}, []string{"a", "b"}}, {nil, []interface{}{"a", "b"}, []string{"a", "b"}},
{nil, []interface{}{nil}, []interface{}{nil}}, {nil, []interface{}{nil}, []interface{}{nil}},
{[]interface{}{}, []interface{}{[]string{"c", "d", "e"}}, []string{"c", "d", "e"}}, {[]interface{}{}, []interface{}{[]string{"c", "d", "e"}}, []string{"c", "d", "e"}},