Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/gohugoio/hugo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/tpl
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2019-11-11 15:54:57 +0300
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2019-11-11 15:54:57 +0300
commit95ef93be667afb480184175a319584fd651abf03 (patch)
tree01435fe1ea537d207ff9436869bcb87e257d5c6a /tpl
parent79355043e8f805cfdd911efed3613f0c5b32e853 (diff)
tpl/collections: Make index work with slice as the last arg
Fixes #6496
Diffstat (limited to 'tpl')
-rw-r--r--tpl/collections/index.go18
-rw-r--r--tpl/collections/index_test.go21
2 files changed, 30 insertions, 9 deletions
diff --git a/tpl/collections/index.go b/tpl/collections/index.go
index b08151188..d2989e22f 100644
--- a/tpl/collections/index.go
+++ b/tpl/collections/index.go
@@ -28,11 +28,27 @@ import (
// We deviate from the stdlib due to https://github.com/golang/go/issues/14751.
//
// TODO(moorereason): merge upstream changes.
-func (ns *Namespace) Index(item interface{}, indices ...interface{}) (interface{}, error) {
+func (ns *Namespace) Index(item interface{}, args ...interface{}) (interface{}, error) {
v := reflect.ValueOf(item)
if !v.IsValid() {
return nil, errors.New("index of untyped nil")
}
+
+ var indices []interface{}
+
+ if len(args) == 1 {
+ v := reflect.ValueOf(args[0])
+ if v.Kind() == reflect.Slice {
+ for i := 0; i < v.Len(); i++ {
+ indices = append(indices, v.Index(i).Interface())
+ }
+ }
+ }
+
+ if indices == nil {
+ indices = args
+ }
+
for _, i := range indices {
index := reflect.ValueOf(i)
var isNil bool
diff --git a/tpl/collections/index_test.go b/tpl/collections/index_test.go
index 6e9071aba..c4cded47c 100644
--- a/tpl/collections/index_test.go
+++ b/tpl/collections/index_test.go
@@ -14,6 +14,7 @@
package collections
import (
+ "fmt"
"testing"
qt "github.com/frankban/quicktest"
@@ -40,22 +41,26 @@ func TestIndex(t *testing.T) {
{map[string]map[string]string{"a": {"b": "c"}}, []interface{}{"a", "b"}, "c", false},
{[]map[string]map[string]string{{"a": {"b": "c"}}}, []interface{}{0, "a", "b"}, "c", false},
{map[string]map[string]interface{}{"a": {"b": []string{"c", "d"}}}, []interface{}{"a", "b", 1}, "d", false},
+ {map[string]map[string]string{"a": {"b": "c"}}, []interface{}{[]string{"a", "b"}}, "c", false},
+
// errors
{nil, nil, nil, true},
{[]int{0, 1}, []interface{}{"1"}, nil, true},
{[]int{0, 1}, []interface{}{nil}, nil, true},
{tstNoStringer{}, []interface{}{0}, nil, true},
} {
- errMsg := qt.Commentf("[%d] %v", i, test)
- result, err := ns.Index(test.item, test.indices...)
+ c.Run(fmt.Sprint(i), func(c *qt.C) {
+ errMsg := qt.Commentf("[%d] %v", i, test)
- if test.isErr {
- c.Assert(err, qt.Not(qt.IsNil), errMsg)
- continue
- }
+ result, err := ns.Index(test.item, test.indices...)
- c.Assert(err, qt.IsNil, errMsg)
- c.Assert(result, qt.DeepEquals, test.expect, errMsg)
+ if test.isErr {
+ c.Assert(err, qt.Not(qt.IsNil), errMsg)
+ return
+ }
+ c.Assert(err, qt.IsNil, errMsg)
+ c.Assert(result, qt.DeepEquals, test.expect, errMsg)
+ })
}
}