mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-31 22:41:53 +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:
@@ -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))
|
||||
return collections.Append(to, from...)
|
||||
|
||||
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 tov.Interface(), nil
|
||||
}
|
||||
|
@@ -18,11 +18,11 @@ import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/alecthomas/assert"
|
||||
"github.com/gohugoio/hugo/deps"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// Also see tests in common/collection.
|
||||
func TestAppend(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
@@ -36,16 +36,6 @@ func TestAppend(t *testing.T) {
|
||||
{[]string{"a", "b"}, []interface{}{"c"}, []string{"a", "b", "c"}},
|
||||
{[]string{"a", "b"}, []interface{}{"c", "d", "e"}, []string{"a", "b", "c", "d", "e"}},
|
||||
{[]string{"a", "b"}, []interface{}{[]string{"c", "d", "e"}}, []string{"a", "b", "c", "d", "e"}},
|
||||
{nil, []interface{}{"a", "b"}, []string{"a", "b"}},
|
||||
{nil, []interface{}{nil}, []interface{}{nil}},
|
||||
{tstSlicers{&tstSlicer{"a"}, &tstSlicer{"b"}},
|
||||
[]interface{}{&tstSlicer{"c"}},
|
||||
tstSlicers{&tstSlicer{"a"}, &tstSlicer{"b"}, &tstSlicer{"c"}}},
|
||||
{&tstSlicers{&tstSlicer{"a"}, &tstSlicer{"b"}},
|
||||
[]interface{}{&tstSlicer{"c"}},
|
||||
tstSlicers{&tstSlicer{"a"},
|
||||
&tstSlicer{"b"},
|
||||
&tstSlicer{"c"}}},
|
||||
// Errors
|
||||
{"", []interface{}{[]string{"a", "b"}}, false},
|
||||
{[]string{"a", "b"}, []interface{}{}, false},
|
||||
@@ -73,5 +63,4 @@ func TestAppend(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
assert.Len(t, ns.Slice(), 0)
|
||||
}
|
||||
|
@@ -136,7 +136,7 @@ func (ns *Namespace) lookupFunc(fname string) (reflect.Value, bool) {
|
||||
return m, true
|
||||
}
|
||||
|
||||
// indirect is taken from 'text/template/exec.go'
|
||||
// indirect is borrowed from the Go stdlib: 'text/template/exec.go'
|
||||
func indirect(v reflect.Value) (rv reflect.Value, isNil bool) {
|
||||
for ; v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface; v = v.Elem() {
|
||||
if v.IsNil() {
|
||||
|
@@ -523,39 +523,7 @@ func (ns *Namespace) Slice(args ...interface{}) interface{} {
|
||||
return args
|
||||
}
|
||||
|
||||
first := args[0]
|
||||
firstType := reflect.TypeOf(first)
|
||||
|
||||
if firstType == nil {
|
||||
return args
|
||||
}
|
||||
|
||||
if g, ok := first.(collections.Slicer); ok {
|
||||
v, err := g.Slice(args)
|
||||
if err == nil {
|
||||
return v
|
||||
}
|
||||
|
||||
// If Slice fails, the items are not of the same type and
|
||||
// []interface{} is the best we can do.
|
||||
return args
|
||||
}
|
||||
|
||||
if len(args) > 1 {
|
||||
// This can be a mix of types.
|
||||
for i := 1; i < len(args); i++ {
|
||||
if firstType != reflect.TypeOf(args[i]) {
|
||||
// []interface{} is the best we can do
|
||||
return args
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
slice := reflect.MakeSlice(reflect.SliceOf(firstType), len(args), len(args))
|
||||
for i, arg := range args {
|
||||
slice.Index(i).Set(reflect.ValueOf(arg))
|
||||
}
|
||||
return slice.Interface()
|
||||
return collections.Slice(args...)
|
||||
}
|
||||
|
||||
type intersector struct {
|
||||
|
@@ -25,7 +25,6 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gohugoio/hugo/common/collections"
|
||||
"github.com/gohugoio/hugo/config"
|
||||
"github.com/gohugoio/hugo/deps"
|
||||
"github.com/gohugoio/hugo/helpers"
|
||||
@@ -644,83 +643,7 @@ func TestShuffleRandomising(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
var _ collections.Slicer = (*tstSlicer)(nil)
|
||||
var _ collections.Slicer = (*tstSlicerIn1)(nil)
|
||||
var _ collections.Slicer = (*tstSlicerIn2)(nil)
|
||||
var _ testSlicerInterface = (*tstSlicerIn1)(nil)
|
||||
var _ testSlicerInterface = (*tstSlicerIn1)(nil)
|
||||
|
||||
type testSlicerInterface interface {
|
||||
Name() string
|
||||
}
|
||||
|
||||
type testSlicerInterfaces []testSlicerInterface
|
||||
|
||||
type tstSlicerIn1 struct {
|
||||
name string
|
||||
}
|
||||
|
||||
type tstSlicerIn2 struct {
|
||||
name string
|
||||
}
|
||||
|
||||
type tstSlicer struct {
|
||||
name string
|
||||
}
|
||||
|
||||
func (p *tstSlicerIn1) Slice(in interface{}) (interface{}, error) {
|
||||
items := in.([]interface{})
|
||||
result := make(testSlicerInterfaces, len(items))
|
||||
for i, v := range items {
|
||||
switch vv := v.(type) {
|
||||
case testSlicerInterface:
|
||||
result[i] = vv
|
||||
default:
|
||||
return nil, errors.New("invalid type")
|
||||
}
|
||||
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (p *tstSlicerIn2) Slice(in interface{}) (interface{}, error) {
|
||||
items := in.([]interface{})
|
||||
result := make(testSlicerInterfaces, len(items))
|
||||
for i, v := range items {
|
||||
switch vv := v.(type) {
|
||||
case testSlicerInterface:
|
||||
result[i] = vv
|
||||
default:
|
||||
return nil, errors.New("invalid type")
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (p *tstSlicerIn1) Name() string {
|
||||
return p.Name()
|
||||
}
|
||||
|
||||
func (p *tstSlicerIn2) Name() string {
|
||||
return p.Name()
|
||||
}
|
||||
|
||||
func (p *tstSlicer) Slice(in interface{}) (interface{}, error) {
|
||||
items := in.([]interface{})
|
||||
result := make(tstSlicers, len(items))
|
||||
for i, v := range items {
|
||||
switch vv := v.(type) {
|
||||
case *tstSlicer:
|
||||
result[i] = vv
|
||||
default:
|
||||
return nil, errors.New("invalid type")
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
type tstSlicers []*tstSlicer
|
||||
|
||||
// Also see tests in commons/collection.
|
||||
func TestSlice(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
@@ -731,14 +654,10 @@ func TestSlice(t *testing.T) {
|
||||
expected interface{}
|
||||
}{
|
||||
{[]interface{}{"a", "b"}, []string{"a", "b"}},
|
||||
{[]interface{}{&tstSlicer{"a"}, &tstSlicer{"b"}}, tstSlicers{&tstSlicer{"a"}, &tstSlicer{"b"}}},
|
||||
{[]interface{}{&tstSlicer{"a"}, "b"}, []interface{}{&tstSlicer{"a"}, "b"}},
|
||||
{[]interface{}{}, []interface{}{}},
|
||||
{[]interface{}{nil}, []interface{}{nil}},
|
||||
{[]interface{}{5, "b"}, []interface{}{5, "b"}},
|
||||
{[]interface{}{tstNoStringer{}}, []tstNoStringer{tstNoStringer{}}},
|
||||
{[]interface{}{&tstSlicerIn1{"a"}, &tstSlicerIn2{"b"}}, testSlicerInterfaces{&tstSlicerIn1{"a"}, &tstSlicerIn2{"b"}}},
|
||||
{[]interface{}{&tstSlicerIn1{"a"}, &tstSlicer{"b"}}, []interface{}{&tstSlicerIn1{"a"}, &tstSlicer{"b"}}},
|
||||
} {
|
||||
errMsg := fmt.Sprintf("[%d] %v", i, test.args)
|
||||
|
||||
|
Reference in New Issue
Block a user