tpl/collections: Make index work with slice as the last arg

Fixes #6496
This commit is contained in:
Bjørn Erik Pedersen
2019-11-11 13:54:57 +01:00
parent 79355043e8
commit 95ef93be66
2 changed files with 30 additions and 9 deletions

View File

@@ -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