| 1 |
// Copyright 2017 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package collections |
| 15 |
|
| 16 |
import ( |
| 17 |
"errors" |
| 18 |
"fmt" |
| 19 |
"reflect" |
| 20 |
|
| 21 |
"github.com/spf13/cast" |
| 22 |
|
| 23 |
"github.com/gohugoio/hugo/common/hmaps" |
| 24 |
"github.com/gohugoio/hugo/common/hreflect" |
| 25 |
) |
| 26 |
|
| 27 |
// Index returns the result of indexing its first argument by the following |
| 28 |
// arguments. Thus "index x 1 2 3" is, in Go syntax, x[1][2][3]. Each |
| 29 |
// indexed item must be a map, slice, or array. |
| 30 |
// |
| 31 |
// Adapted from Go stdlib src/text/template/funcs.go. |
| 32 |
// |
| 33 |
// We deviate from the stdlib mostly because of https://github.com/golang/go/issues/14751. |
| 34 |
func (ns *Namespace) Index(item any, args ...any) (any, error) { |
| 35 |
v, err := ns.doIndex(item, args...) |
| 36 |
if err != nil { |
| 37 |
return nil, fmt.Errorf("index of type %T with args %v failed: %s", item, args, err) |
| 38 |
} |
| 39 |
return v, nil |
| 40 |
} |
| 41 |
|
| 42 |
func (ns *Namespace) doIndex(item any, args ...any) (any, error) { |
| 43 |
// TODO(moorereason): merge upstream changes. |
| 44 |
v := reflect.ValueOf(item) |
| 45 |
if !v.IsValid() { |
| 46 |
// See issue 10489 |
| 47 |
// This used to be an error. |
| 48 |
return nil, nil |
| 49 |
} |
| 50 |
|
| 51 |
var indices []any |
| 52 |
|
| 53 |
if len(args) == 1 { |
| 54 |
v := reflect.ValueOf(args[0]) |
| 55 |
if v.Kind() == reflect.Slice { |
| 56 |
for i := range v.Len() { |
| 57 |
indices = append(indices, v.Index(i).Interface()) |
| 58 |
} |
| 59 |
} else { |
| 60 |
indices = append(indices, args[0]) |
| 61 |
} |
| 62 |
} else { |
| 63 |
indices = args |
| 64 |
} |
| 65 |
|
| 66 |
lowerm, ok := item.(hmaps.Params) |
| 67 |
if ok { |
| 68 |
return lowerm.GetNested(cast.ToStringSlice(indices)...), nil |
| 69 |
} |
| 70 |
|
| 71 |
for _, i := range indices { |
| 72 |
index := reflect.ValueOf(i) |
| 73 |
var isNil bool |
| 74 |
if v, isNil = hreflect.Indirect(v); isNil { |
| 75 |
// See issue 10489 |
| 76 |
// This used to be an error. |
| 77 |
return nil, nil |
| 78 |
} |
| 79 |
switch v.Kind() { |
| 80 |
case reflect.Array, reflect.Slice, reflect.String: |
| 81 |
var x int64 |
| 82 |
switch index.Kind() { |
| 83 |
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
| 84 |
x = index.Int() |
| 85 |
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: |
| 86 |
x = int64(index.Uint()) |
| 87 |
case reflect.Invalid: |
| 88 |
return nil, errors.New("cannot index slice/array with nil") |
| 89 |
default: |
| 90 |
return nil, fmt.Errorf("cannot index slice/array with type %s", index.Type()) |
| 91 |
} |
| 92 |
if x < 0 || x >= int64(v.Len()) { |
| 93 |
// We deviate from stdlib here. Don't return an error if the |
| 94 |
// index is out of range. |
| 95 |
return nil, nil |
| 96 |
} |
| 97 |
v = v.Index(int(x)) |
| 98 |
case reflect.Map: |
| 99 |
index, err := prepareArg(index, v.Type().Key()) |
| 100 |
if err != nil { |
| 101 |
return nil, err |
| 102 |
} |
| 103 |
|
| 104 |
if x := v.MapIndex(index); x.IsValid() { |
| 105 |
v = x |
| 106 |
} else { |
| 107 |
v = reflect.Zero(v.Type().Elem()) |
| 108 |
} |
| 109 |
case reflect.Invalid: |
| 110 |
// the loop holds invariant: v.IsValid() |
| 111 |
panic("unreachable") |
| 112 |
default: |
| 113 |
return nil, fmt.Errorf("can't index item of type %s", v.Type()) |
| 114 |
} |
| 115 |
} |
| 116 |
return v.Interface(), nil |
| 117 |
} |
| 118 |
|
| 119 |
// prepareArg checks if value can be used as an argument of type argType, and |
| 120 |
// converts an invalid value to appropriate zero if possible. |
| 121 |
// |
| 122 |
// Copied from Go stdlib src/text/template/funcs.go. |
| 123 |
func prepareArg(value reflect.Value, argType reflect.Type) (reflect.Value, error) { |
| 124 |
if !value.IsValid() { |
| 125 |
if !canBeNil(argType) { |
| 126 |
return reflect.Value{}, fmt.Errorf("value is nil; should be of type %s", argType) |
| 127 |
} |
| 128 |
value = reflect.Zero(argType) |
| 129 |
} |
| 130 |
if !value.Type().AssignableTo(argType) { |
| 131 |
return reflect.Value{}, fmt.Errorf("value has type %s; should be %s", value.Type(), argType) |
| 132 |
} |
| 133 |
return value, nil |
| 134 |
} |
| 135 |
|
| 136 |
// canBeNil reports whether an untyped nil can be assigned to the type. See reflect.Zero. |
| 137 |
// |
| 138 |
// Copied from Go stdlib src/text/template/exec.go. |
| 139 |
func canBeNil(typ reflect.Type) bool { |
| 140 |
switch typ.Kind() { |
| 141 |
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Pointer, reflect.Slice: |
| 142 |
return true |
| 143 |
} |
| 144 |
return false |
| 145 |
} |