mirror of
https://github.com/gohugoio/hugo.git
synced 2025-04-28 06:30:33 +03:00
Add list support in Scratch
This commit is contained in:
parent
435e996c4f
commit
eaba04e82b
3 changed files with 49 additions and 5 deletions
|
@ -15,6 +15,7 @@ package hugolib
|
|||
|
||||
import (
|
||||
"github.com/spf13/hugo/helpers"
|
||||
"reflect"
|
||||
"sort"
|
||||
)
|
||||
|
||||
|
@ -23,16 +24,30 @@ type Scratch struct {
|
|||
values map[string]interface{}
|
||||
}
|
||||
|
||||
// Add will add (using the + operator) the addend to the existing addend (if found).
|
||||
// For single values, Add will add (using the + operator) the addend to the existing addend (if found).
|
||||
// Supports numeric values and strings.
|
||||
//
|
||||
// If the first add for a key is an array or slice, then the next value(s) will be appended.
|
||||
func (c *Scratch) Add(key string, newAddend interface{}) (string, error) {
|
||||
var newVal interface{}
|
||||
existingAddend, found := c.values[key]
|
||||
if found {
|
||||
var err error
|
||||
newVal, err = helpers.DoArithmetic(existingAddend, newAddend, '+')
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
||||
addendV := reflect.ValueOf(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()
|
||||
}
|
||||
} else {
|
||||
newVal, err = helpers.DoArithmetic(existingAddend, newAddend, '+')
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
} else {
|
||||
newVal = newAddend
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue