mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-28 22:19:59 +02:00
tpl/collections: Fix some index cases where the indices given is a slice and be more lenient with nil inputs
See adjusted tests for detail. Fixes #10489
This commit is contained in:
@@ -35,12 +35,9 @@ import (
|
||||
func (ns *Namespace) Index(item any, args ...any) (any, error) {
|
||||
v := reflect.ValueOf(item)
|
||||
if !v.IsValid() {
|
||||
return nil, errors.New("index of untyped nil")
|
||||
}
|
||||
|
||||
lowerm, ok := item.(maps.Params)
|
||||
if ok {
|
||||
return lowerm.Get(cast.ToStringSlice(args)...), nil
|
||||
// See issue 10489
|
||||
// This used to be an error.
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var indices []any
|
||||
@@ -51,18 +48,25 @@ func (ns *Namespace) Index(item any, args ...any) (any, error) {
|
||||
for i := 0; i < v.Len(); i++ {
|
||||
indices = append(indices, v.Index(i).Interface())
|
||||
}
|
||||
} else {
|
||||
indices = append(indices, args[0])
|
||||
}
|
||||
} else {
|
||||
indices = args
|
||||
}
|
||||
|
||||
if indices == nil {
|
||||
indices = args
|
||||
lowerm, ok := item.(maps.Params)
|
||||
if ok {
|
||||
return lowerm.Get(cast.ToStringSlice(indices)...), nil
|
||||
}
|
||||
|
||||
for _, i := range indices {
|
||||
index := reflect.ValueOf(i)
|
||||
var isNil bool
|
||||
if v, isNil = indirect(v); isNil {
|
||||
return nil, errors.New("index of nil pointer")
|
||||
// See issue 10489
|
||||
// This used to be an error.
|
||||
return nil, nil
|
||||
}
|
||||
switch v.Kind() {
|
||||
case reflect.Array, reflect.Slice, reflect.String:
|
||||
|
Reference in New Issue
Block a user