summaryrefslogtreecommitdiffstats
path: root/tpl/collections/append.go
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2018-10-08 10:25:15 +0200
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2018-10-08 12:30:50 +0200
commit31a8bb8c071c6f2ca8cbd73057912932a1e7e943 (patch)
tree698f8d82cf1dc2bb6c56ba3a615ef4b441361c6a /tpl/collections/append.go
parent8e825ddf5b31ce5fe570c78ac7a78c8056fb60f9 (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 'tpl/collections/append.go')
-rw-r--r--tpl/collections/append.go42
1 files changed, 3 insertions, 39 deletions
diff --git a/tpl/collections/append.go b/tpl/collections/append.go
index 20afa0e72..297328dc8 100644
--- a/tpl/collections/append.go
+++ b/tpl/collections/append.go
@@ -15,8 +15,8 @@ package collections
import (
"errors"
- "fmt"
- "reflect"
+
+ "github.com/gohugoio/hugo/common/collections"
)
// Append appends the arguments up to the last one to the slice in the last argument.
@@ -33,42 +33,6 @@ func (ns *Namespace) Append(args ...interface{}) (interface{}, error) {
to := args[len(args)-1]
from := args[:len(args)-1]
- tov, toIsNil := indirect(reflect.ValueOf(to))
-
- toIsNil = toIsNil || to == nil
- var tot reflect.Type
-
- if !toIsNil {
- if tov.Kind() != reflect.Slice {
- return nil, fmt.Errorf("expected a slice, got %T", to)
- }
-
- tot = tov.Type().Elem()
- toIsNil = tov.Len() == 0
-
- if len(from) == 1 {
- // If we get []string []string, we append the from slice to to
- fromv := reflect.ValueOf(from[0])
- if fromv.Kind() == reflect.Slice {
- fromt := reflect.TypeOf(from[0]).Elem()
- if tot == fromt {
- return reflect.AppendSlice(tov, fromv).Interface(), nil
- }
- }
- }
- }
-
- if toIsNil {
- return ns.Slice(from...), nil
- }
-
- for _, f := range from {
- fv := reflect.ValueOf(f)
- if tot != fv.Type() {
- return nil, fmt.Errorf("append element type mismatch: expected %v, got %v", tot, fv.Type())
- }
- tov = reflect.Append(tov, fv)
- }
+ return collections.Append(to, from...)
- return tov.Interface(), nil
}