diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-10-08 10:25:15 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-10-08 12:30:50 +0200 |
commit | 31a8bb8c071c6f2ca8cbd73057912932a1e7e943 (patch) | |
tree | 698f8d82cf1dc2bb6c56ba3a615ef4b441361c6a /common/maps/scratch.go | |
parent | 8e825ddf5b31ce5fe570c78ac7a78c8056fb60f9 (diff) |
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
Diffstat (limited to 'common/maps/scratch.go')
-rw-r--r-- | common/maps/scratch.go | 11 |
1 files changed, 5 insertions, 6 deletions
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, '+') |