mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-20 21:31:32 +02:00
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
This commit is contained in:
@@ -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, '+')
|
||||
|
Reference in New Issue
Block a user