From 31a8bb8c071c6f2ca8cbd73057912932a1e7e943 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Mon, 8 Oct 2018 10:25:15 +0200 Subject: common/maps: Improve append in Scratch This commit consolidates the reflective collections handling in `.Scratch` vs the `tpl` package so they use the same code paths. This commit also adds support for a corner case where a typed slice is appended to a nil or empty `[]interface{}`. Fixes #5275 --- common/maps/scratch.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'common/maps/scratch.go') diff --git a/common/maps/scratch.go b/common/maps/scratch.go index 4b062d139..2972e2022 100644 --- a/common/maps/scratch.go +++ b/common/maps/scratch.go @@ -18,6 +18,7 @@ import ( "sort" "sync" + "github.com/gohugoio/hugo/common/collections" "github.com/gohugoio/hugo/common/math" ) @@ -40,14 +41,12 @@ func (c *Scratch) Add(key string, newAddend interface{}) (string, error) { if found { var err error - addendV := reflect.ValueOf(existingAddend) + addendV := reflect.TypeOf(existingAddend) if addendV.Kind() == reflect.Slice || addendV.Kind() == reflect.Array { - nav := reflect.ValueOf(newAddend) - if nav.Kind() == reflect.Slice || nav.Kind() == reflect.Array { - newVal = reflect.AppendSlice(addendV, nav).Interface() - } else { - newVal = reflect.Append(addendV, nav).Interface() + newVal, err = collections.Append(existingAddend, newAddend) + if err != nil { + return "", err } } else { newVal, err = math.DoArithmetic(existingAddend, newAddend, '+') -- cgit v1.2.3