mirror of
https://github.com/gohugoio/hugo.git
synced 2025-09-02 22:52:51 +02:00
tpl/collections: Make index work with slice as the last arg
Fixes #6496
This commit is contained in:
@@ -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
|
||||
|
Reference in New Issue
Block a user