2017-03-13 18:55:02 -04:00
|
|
|
// Copyright 2017 The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package collections
|
|
|
|
|
|
|
|
import (
|
2022-03-01 05:30:11 -05:00
|
|
|
"context"
|
2017-03-13 18:55:02 -04:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
2017-04-30 15:52:56 -04:00
|
|
|
|
2022-03-01 05:30:11 -05:00
|
|
|
"github.com/gohugoio/hugo/common/hreflect"
|
2017-06-13 12:42:45 -04:00
|
|
|
"github.com/gohugoio/hugo/tpl"
|
2017-03-13 18:55:02 -04:00
|
|
|
)
|
|
|
|
|
2022-12-21 07:11:08 -05:00
|
|
|
// Apply takes a map, array, or slice c and returns a new slice with the function fname applied over it.
|
|
|
|
func (ns *Namespace) Apply(ctx context.Context, c any, fname string, args ...any) (any, error) {
|
|
|
|
if c == nil {
|
2022-03-17 17:03:27 -04:00
|
|
|
return make([]any, 0), nil
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if fname == "apply" {
|
|
|
|
return nil, errors.New("can't apply myself (no turtles allowed)")
|
|
|
|
}
|
|
|
|
|
2022-12-21 07:11:08 -05:00
|
|
|
seqv := reflect.ValueOf(c)
|
2017-03-13 18:55:02 -04:00
|
|
|
seqv, isNil := indirect(seqv)
|
|
|
|
if isNil {
|
|
|
|
return nil, errors.New("can't iterate over a nil value")
|
|
|
|
}
|
|
|
|
|
|
|
|
fnv, found := ns.lookupFunc(fname)
|
|
|
|
if !found {
|
|
|
|
return nil, errors.New("can't find function " + fname)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch seqv.Kind() {
|
|
|
|
case reflect.Array, reflect.Slice:
|
2022-03-17 17:03:27 -04:00
|
|
|
r := make([]any, seqv.Len())
|
2017-03-13 18:55:02 -04:00
|
|
|
for i := 0; i < seqv.Len(); i++ {
|
|
|
|
vv := seqv.Index(i)
|
|
|
|
|
2022-03-01 05:30:11 -05:00
|
|
|
vvv, err := applyFnToThis(ctx, fnv, vv, args...)
|
2017-03-13 18:55:02 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
r[i] = vvv.Interface()
|
|
|
|
}
|
|
|
|
|
|
|
|
return r, nil
|
|
|
|
default:
|
2022-12-21 07:11:08 -05:00
|
|
|
return nil, fmt.Errorf("can't apply over %v", c)
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
func applyFnToThis(ctx context.Context, fn, this reflect.Value, args ...any) (reflect.Value, error) {
|
2022-03-01 05:30:11 -05:00
|
|
|
num := fn.Type().NumIn()
|
|
|
|
if num > 0 && fn.Type().In(0).Implements(hreflect.ContextInterface) {
|
2022-03-17 17:03:27 -04:00
|
|
|
args = append([]any{ctx}, args...)
|
2022-03-01 05:30:11 -05:00
|
|
|
}
|
|
|
|
|
2017-03-13 18:55:02 -04:00
|
|
|
n := make([]reflect.Value, len(args))
|
|
|
|
for i, arg := range args {
|
|
|
|
if arg == "." {
|
|
|
|
n[i] = this
|
|
|
|
} else {
|
|
|
|
n[i] = reflect.ValueOf(arg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if fn.Type().IsVariadic() {
|
|
|
|
num--
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(bep) see #1098 - also see template_tests.go
|
|
|
|
/*if len(args) < num {
|
|
|
|
return reflect.ValueOf(nil), errors.New("Too few arguments")
|
|
|
|
} else if len(args) > num {
|
|
|
|
return reflect.ValueOf(nil), errors.New("Too many arguments")
|
|
|
|
}*/
|
|
|
|
|
|
|
|
for i := 0; i < num; i++ {
|
|
|
|
// AssignableTo reports whether xt is assignable to type targ.
|
|
|
|
if xt, targ := n[i].Type(), fn.Type().In(i); !xt.AssignableTo(targ) {
|
|
|
|
return reflect.ValueOf(nil), errors.New("called apply using " + xt.String() + " as type " + targ.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res := fn.Call(n)
|
|
|
|
|
|
|
|
if len(res) == 1 || res[1].IsNil() {
|
|
|
|
return res[0], nil
|
|
|
|
}
|
|
|
|
return reflect.ValueOf(nil), res[1].Interface().(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ns *Namespace) lookupFunc(fname string) (reflect.Value, bool) {
|
2022-03-17 16:58:54 -04:00
|
|
|
namespace, methodName, ok := strings.Cut(fname, ".")
|
|
|
|
if !ok {
|
2020-01-15 09:59:56 -05:00
|
|
|
templ := ns.deps.Tmpl().(tpl.TemplateFuncGetter)
|
2019-12-10 13:56:44 -05:00
|
|
|
return templ.GetFunc(fname)
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
|
|
|
|
2022-01-16 16:01:13 -05:00
|
|
|
// Namespace
|
2022-03-17 16:58:54 -04:00
|
|
|
nv, found := ns.lookupFunc(namespace)
|
2017-03-13 18:55:02 -04:00
|
|
|
if !found {
|
|
|
|
return reflect.Value{}, false
|
|
|
|
}
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
fn, ok := nv.Interface().(func(...any) (any, error))
|
2022-01-16 16:01:13 -05:00
|
|
|
if !ok {
|
|
|
|
return reflect.Value{}, false
|
|
|
|
}
|
|
|
|
v, err := fn()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
nv = reflect.ValueOf(v)
|
|
|
|
|
2017-03-13 18:55:02 -04:00
|
|
|
// method
|
2022-03-17 16:58:54 -04:00
|
|
|
m := hreflect.GetMethodByName(nv, methodName)
|
2022-01-16 16:01:13 -05:00
|
|
|
|
2017-03-13 18:55:02 -04:00
|
|
|
if m.Kind() == reflect.Invalid {
|
|
|
|
return reflect.Value{}, false
|
|
|
|
}
|
|
|
|
return m, true
|
|
|
|
}
|
|
|
|
|
2018-10-08 04:25:15 -04:00
|
|
|
// indirect is borrowed from the Go stdlib: 'text/template/exec.go'
|
2017-03-13 18:55:02 -04:00
|
|
|
func indirect(v reflect.Value) (rv reflect.Value, isNil bool) {
|
|
|
|
for ; v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface; v = v.Elem() {
|
|
|
|
if v.IsNil() {
|
|
|
|
return v, true
|
|
|
|
}
|
|
|
|
if v.Kind() == reflect.Interface && v.NumMethod() > 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return v, false
|
|
|
|
}
|
2017-07-03 04:32:10 -04:00
|
|
|
|
|
|
|
func indirectInterface(v reflect.Value) (rv reflect.Value, isNil bool) {
|
|
|
|
for ; v.Kind() == reflect.Interface; v = v.Elem() {
|
|
|
|
if v.IsNil() {
|
|
|
|
return v, true
|
|
|
|
}
|
|
|
|
if v.Kind() == reflect.Interface && v.NumMethod() > 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return v, false
|
|
|
|
}
|