mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-31 22:41:53 +02:00
tpl/collections: Add collections.Append
Before this commit you would typically use `.Scratch.Add` to manually create slices in a loop. With variable overwrite in Go 1.11, we can do better. This commit adds the `append` template func. A made-up example: ```bash {{ $p1 := index .Site.RegularPages 0 }}{{ $p2 := index .Site.RegularPages 1 }} {{ $pages := slice }} {{ if true }} {{ $pages = $pages | append $p2 $p1 }} {{ end }} ``` Note that with 2 slices as arguments, the two examples below will give the same result: ```bash {{ $s1 := slice "a" "b" | append (slice "c" "d") }} {{ $s2 := slice "a" "b" | append "c" "d" }} ``` Both of the above will give `[]string{a, b, c, d}`. This commit also improves the type handling in the `slice` template function. Now `slice "a" "b"` will give a `[]string` slice. The old behaviour was to return a `[]interface{}`. Fixes #5190
This commit is contained in:
74
tpl/collections/append.go
Normal file
74
tpl/collections/append.go
Normal file
@@ -0,0 +1,74 @@
|
||||
// Copyright 2018 The Hugo Authors. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package collections
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// Append appends the arguments up to the last one to the slice in the last argument.
|
||||
// This construct allows template constructs like this:
|
||||
// {{ $pages = $pages | append $p2 $p1 }}
|
||||
// Note that with 2 arguments where both are slices of the same type,
|
||||
// the first slice will be appended to the second:
|
||||
// {{ $pages = $pages | append .Site.RegularPages }}
|
||||
func (ns *Namespace) Append(args ...interface{}) (interface{}, error) {
|
||||
if len(args) < 2 {
|
||||
return nil, errors.New("need at least 2 arguments to append")
|
||||
}
|
||||
|
||||
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 tov.Interface(), nil
|
||||
}
|
77
tpl/collections/append_test.go
Normal file
77
tpl/collections/append_test.go
Normal file
@@ -0,0 +1,77 @@
|
||||
// Copyright 2018 The Hugo Authors. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package collections
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/alecthomas/assert"
|
||||
"github.com/gohugoio/hugo/deps"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestAppend(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ns := New(&deps.Deps{})
|
||||
|
||||
for i, test := range []struct {
|
||||
start interface{}
|
||||
addend []interface{}
|
||||
expected interface{}
|
||||
}{
|
||||
{[]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},
|
||||
// No string concatenation.
|
||||
{"ab",
|
||||
[]interface{}{"c"},
|
||||
false},
|
||||
} {
|
||||
|
||||
errMsg := fmt.Sprintf("[%d]", i)
|
||||
|
||||
args := append(test.addend, test.start)
|
||||
|
||||
result, err := ns.Append(args...)
|
||||
|
||||
if b, ok := test.expected.(bool); ok && !b {
|
||||
require.Error(t, err, errMsg)
|
||||
continue
|
||||
}
|
||||
|
||||
require.NoError(t, err, errMsg)
|
||||
|
||||
if !reflect.DeepEqual(test.expected, result) {
|
||||
t.Fatalf("%s got\n%T: %v\nexpected\n%T: %v", errMsg, result, result, test.expected, test.expected)
|
||||
}
|
||||
}
|
||||
|
||||
assert.Len(t, ns.Slice(), 0)
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
// Copyright 2017 The Hugo Authors. All rights reserved.
|
||||
// Copyright 2018 The Hugo Authors. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@@ -520,10 +520,11 @@ func (ns *Namespace) Slice(args ...interface{}) interface{} {
|
||||
}
|
||||
|
||||
first := args[0]
|
||||
allTheSame := true
|
||||
if len(args) > 1 {
|
||||
firstType := reflect.TypeOf(first)
|
||||
|
||||
allTheSame := firstType != nil
|
||||
if allTheSame && len(args) > 1 {
|
||||
// This can be a mix of types.
|
||||
firstType := reflect.TypeOf(first)
|
||||
for i := 1; i < len(args); i++ {
|
||||
if firstType != reflect.TypeOf(args[i]) {
|
||||
allTheSame = false
|
||||
@@ -538,6 +539,12 @@ func (ns *Namespace) Slice(args ...interface{}) interface{} {
|
||||
if err == nil {
|
||||
return v
|
||||
}
|
||||
} else {
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,4 +1,4 @@
|
||||
// Copyright 2017 The Hugo Authors. All rights reserved.
|
||||
// Copyright 2018 The Hugo Authors. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@@ -647,7 +647,8 @@ type tstSlicer struct {
|
||||
name string
|
||||
}
|
||||
|
||||
func (p *tstSlicer) Slice(items []interface{}) (interface{}, error) {
|
||||
func (p *tstSlicer) Slice(in interface{}) (interface{}, error) {
|
||||
items := in.([]interface{})
|
||||
result := make(tstSlicers, len(items))
|
||||
for i, v := range items {
|
||||
result[i] = v.(*tstSlicer)
|
||||
@@ -666,13 +667,13 @@ func TestSlice(t *testing.T) {
|
||||
args []interface{}
|
||||
expected interface{}
|
||||
}{
|
||||
{[]interface{}{"a", "b"}, []interface{}{"a", "b"}},
|
||||
{[]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{}}, []interface{}{tstNoStringer{}}},
|
||||
{[]interface{}{tstNoStringer{}}, []tstNoStringer{tstNoStringer{}}},
|
||||
} {
|
||||
errMsg := fmt.Sprintf("[%d] %v", i, test.args)
|
||||
|
||||
|
@@ -138,6 +138,11 @@ func init() {
|
||||
[][2]string{},
|
||||
)
|
||||
|
||||
ns.AddMethodMapping(ctx.Append,
|
||||
[]string{"append"},
|
||||
[][2]string{},
|
||||
)
|
||||
|
||||
ns.AddMethodMapping(ctx.Group,
|
||||
[]string{"group"},
|
||||
[][2]string{},
|
||||
|
@@ -121,6 +121,10 @@ func (ns *Namespace) Join(elements ...interface{}) (string, error) {
|
||||
var pathElements []string
|
||||
for _, elem := range elements {
|
||||
switch v := elem.(type) {
|
||||
case []string:
|
||||
for _, e := range v {
|
||||
pathElements = append(pathElements, filepath.ToSlash(e))
|
||||
}
|
||||
case []interface{}:
|
||||
for _, e := range v {
|
||||
elemStr, err := cast.ToStringE(e)
|
||||
|
@@ -88,22 +88,11 @@ func (ns *Namespace) Concat(targetPathIn interface{}, r interface{}) (resource.R
|
||||
var rr resource.Resources
|
||||
|
||||
switch v := r.(type) {
|
||||
// This is what we get from the slice func.
|
||||
case []interface{}:
|
||||
rr = make([]resource.Resource, len(v))
|
||||
for i := 0; i < len(v); i++ {
|
||||
rv, ok := v[i].(resource.Resource)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("cannot concat type %T", v[i])
|
||||
}
|
||||
rr[i] = rv
|
||||
}
|
||||
// This is what we get from .Resources.Match etc.
|
||||
case resource.Resources:
|
||||
rr = v
|
||||
case resource.ResourcesConverter:
|
||||
rr = v.ToResources()
|
||||
default:
|
||||
// We may support Page collections at one point, but we need to think about ...
|
||||
// what to acutually concatenate.
|
||||
return nil, fmt.Errorf("slice %T not supported in concat", r)
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user