2022-02-17 15:51:19 +00:00
|
|
|
// Copyright 2022 The Hugo Authors. All rights reserved.
|
2019-12-10 07:02:15 +00:00
|
|
|
//
|
|
|
|
// 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 template
|
|
|
|
|
|
|
|
import (
|
2022-02-17 15:51:19 +00:00
|
|
|
"context"
|
2019-12-10 07:02:15 +00:00
|
|
|
"io"
|
|
|
|
"reflect"
|
|
|
|
|
2019-12-18 15:50:34 +00:00
|
|
|
"github.com/gohugoio/hugo/common/hreflect"
|
|
|
|
|
2019-12-10 07:02:15 +00:00
|
|
|
"github.com/gohugoio/hugo/tpl/internal/go_templates/texttemplate/parse"
|
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
This files contains the Hugo related addons. All the other files in this
|
|
|
|
package is auto generated.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2020-01-07 09:23:24 +00:00
|
|
|
// Export it so we can populate Hugo's func map with it, which makes it faster.
|
2020-05-23 13:32:27 +00:00
|
|
|
var GoFuncs = builtinFuncs()
|
2020-01-07 09:23:24 +00:00
|
|
|
|
2019-12-10 18:56:44 +00:00
|
|
|
// Preparer prepares the template before execution.
|
2019-12-10 07:02:15 +00:00
|
|
|
type Preparer interface {
|
|
|
|
Prepare() (*Template, error)
|
|
|
|
}
|
|
|
|
|
2019-12-10 18:56:44 +00:00
|
|
|
// ExecHelper allows some custom eval hooks.
|
|
|
|
type ExecHelper interface {
|
2022-02-17 15:51:19 +00:00
|
|
|
Init(ctx context.Context, tmpl Preparer)
|
|
|
|
GetFunc(ctx context.Context, tmpl Preparer, name string) (reflect.Value, reflect.Value, bool)
|
|
|
|
GetMethod(ctx context.Context, tmpl Preparer, receiver reflect.Value, name string) (method reflect.Value, firstArg reflect.Value)
|
|
|
|
GetMapValue(ctx context.Context, tmpl Preparer, receiver, key reflect.Value) (reflect.Value, bool)
|
2019-12-10 07:02:15 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 18:56:44 +00:00
|
|
|
// Executer executes a given template.
|
|
|
|
type Executer interface {
|
2022-03-17 21:03:27 +00:00
|
|
|
ExecuteWithContext(ctx context.Context, p Preparer, wr io.Writer, data any) error
|
2019-12-10 18:56:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type executer struct {
|
|
|
|
helper ExecHelper
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewExecuter(helper ExecHelper) Executer {
|
|
|
|
return &executer{helper: helper}
|
|
|
|
}
|
|
|
|
|
2022-02-17 15:51:19 +00:00
|
|
|
type (
|
2023-02-25 08:24:59 +00:00
|
|
|
pageContextKeyType string
|
2022-02-17 15:51:19 +00:00
|
|
|
hasLockContextKeyType string
|
2023-02-11 15:20:24 +00:00
|
|
|
stackContextKeyType string
|
2022-02-17 15:51:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2023-02-25 08:24:59 +00:00
|
|
|
// The data page passed to ExecuteWithContext gets stored with this key.
|
|
|
|
PageContextKey = pageContextKeyType("page")
|
2022-02-17 15:51:19 +00:00
|
|
|
// Used in partialCached to signal to nested templates that a lock is already taken.
|
|
|
|
HasLockContextKey = hasLockContextKeyType("hasLock")
|
|
|
|
)
|
|
|
|
|
2023-02-18 20:47:35 +00:00
|
|
|
// Note: The context is currently not fully implemented in Hugo. This is a work in progress.
|
2022-03-17 21:03:27 +00:00
|
|
|
func (t *executer) ExecuteWithContext(ctx context.Context, p Preparer, wr io.Writer, data any) error {
|
2023-02-25 08:24:59 +00:00
|
|
|
if ctx == nil {
|
|
|
|
panic("nil context")
|
|
|
|
}
|
|
|
|
|
2019-12-10 07:02:15 +00:00
|
|
|
tmpl, err := p.Prepare()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
value, ok := data.(reflect.Value)
|
|
|
|
if !ok {
|
|
|
|
value = reflect.ValueOf(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
state := &state{
|
2022-02-17 15:51:19 +00:00
|
|
|
ctx: ctx,
|
2019-12-10 18:56:44 +00:00
|
|
|
helper: t.helper,
|
2019-11-27 12:42:36 +00:00
|
|
|
prep: p,
|
2019-12-10 18:56:44 +00:00
|
|
|
tmpl: tmpl,
|
|
|
|
wr: wr,
|
|
|
|
vars: []variable{{"$", value}},
|
2019-12-10 07:02:15 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 15:51:19 +00:00
|
|
|
t.helper.Init(ctx, p)
|
|
|
|
|
2019-12-10 07:02:15 +00:00
|
|
|
return tmpl.executeWithState(state, value)
|
2022-02-17 15:51:19 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 07:02:15 +00:00
|
|
|
// Prepare returns a template ready for execution.
|
|
|
|
func (t *Template) Prepare() (*Template, error) {
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
2019-12-10 18:56:44 +00:00
|
|
|
func (t *Template) executeWithState(state *state, value reflect.Value) (err error) {
|
|
|
|
defer errRecover(&err)
|
|
|
|
if t.Tree == nil || t.Root == nil {
|
|
|
|
state.errorf("%q is an incomplete or empty template", t.Name())
|
|
|
|
}
|
|
|
|
state.walk(value, t.Root)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-16 11:11:32 +00:00
|
|
|
// Below are modified structs etc. The changes are marked with "Added for Hugo."
|
2019-12-10 07:02:15 +00:00
|
|
|
|
|
|
|
// state represents the state of an execution. It's not part of the
|
|
|
|
// template so that multiple executions of the same template
|
|
|
|
// can execute in parallel.
|
|
|
|
type state struct {
|
2019-12-10 18:56:44 +00:00
|
|
|
tmpl *Template
|
2023-02-18 20:47:35 +00:00
|
|
|
ctx context.Context // Added for Hugo. The original data context.
|
2022-02-17 15:51:19 +00:00
|
|
|
prep Preparer // Added for Hugo.
|
|
|
|
helper ExecHelper // Added for Hugo.
|
2019-12-10 18:56:44 +00:00
|
|
|
wr io.Writer
|
|
|
|
node parse.Node // current node, for errors
|
|
|
|
vars []variable // push-down stack of variable values.
|
|
|
|
depth int // the height of the stack of executing templates.
|
2019-12-10 07:02:15 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 18:56:44 +00:00
|
|
|
func (s *state) evalFunction(dot reflect.Value, node *parse.IdentifierNode, cmd parse.Node, args []parse.Node, final reflect.Value) reflect.Value {
|
|
|
|
s.at(node)
|
|
|
|
name := node.Ident
|
|
|
|
|
|
|
|
var function reflect.Value
|
2022-02-17 15:51:19 +00:00
|
|
|
// Added for Hugo.
|
|
|
|
var first reflect.Value
|
2019-12-10 18:56:44 +00:00
|
|
|
var ok bool
|
2022-03-16 08:04:30 +00:00
|
|
|
var isBuiltin bool
|
2019-12-10 18:56:44 +00:00
|
|
|
if s.helper != nil {
|
2022-03-16 08:04:30 +00:00
|
|
|
isBuiltin = name == "and" || name == "or"
|
2022-02-17 15:51:19 +00:00
|
|
|
function, first, ok = s.helper.GetFunc(s.ctx, s.prep, name)
|
2019-12-10 07:02:15 +00:00
|
|
|
}
|
2019-12-10 18:56:44 +00:00
|
|
|
|
|
|
|
if !ok {
|
2022-03-16 08:04:30 +00:00
|
|
|
function, isBuiltin, ok = findFunction(name, s.tmpl)
|
2019-12-10 18:56:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
s.errorf("%q is not a defined function", name)
|
|
|
|
}
|
2022-02-17 15:51:19 +00:00
|
|
|
if first != zero {
|
2022-03-16 08:04:30 +00:00
|
|
|
return s.evalCall(dot, function, isBuiltin, cmd, name, args, final, first)
|
2022-02-17 15:51:19 +00:00
|
|
|
}
|
2022-03-16 08:04:30 +00:00
|
|
|
return s.evalCall(dot, function, isBuiltin, cmd, name, args, final)
|
2019-12-10 18:56:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// evalField evaluates an expression like (.Field) or (.Field arg1 arg2).
|
|
|
|
// The 'final' argument represents the return value from the preceding
|
|
|
|
// value of the pipeline, if any.
|
|
|
|
func (s *state) evalField(dot reflect.Value, fieldName string, node parse.Node, args []parse.Node, final, receiver reflect.Value) reflect.Value {
|
|
|
|
if !receiver.IsValid() {
|
|
|
|
if s.tmpl.option.missingKey == mapError { // Treat invalid value as missing map key.
|
|
|
|
s.errorf("nil data; no entry for key %q", fieldName)
|
|
|
|
}
|
|
|
|
return zero
|
|
|
|
}
|
|
|
|
typ := receiver.Type()
|
|
|
|
receiver, isNil := indirect(receiver)
|
|
|
|
if receiver.Kind() == reflect.Interface && isNil {
|
|
|
|
// Calling a method on a nil interface can't work. The
|
|
|
|
// MethodByName method call below would panic.
|
|
|
|
s.errorf("nil pointer evaluating %s.%s", typ, fieldName)
|
|
|
|
return zero
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unless it's an interface, need to get to a value of type *T to guarantee
|
|
|
|
// we see all methods of T and *T.
|
|
|
|
ptr := receiver
|
2022-03-16 08:04:30 +00:00
|
|
|
if ptr.Kind() != reflect.Interface && ptr.Kind() != reflect.Pointer && ptr.CanAddr() {
|
2019-12-10 18:56:44 +00:00
|
|
|
ptr = ptr.Addr()
|
|
|
|
}
|
2022-03-16 08:04:30 +00:00
|
|
|
|
2019-11-27 12:42:36 +00:00
|
|
|
// Added for Hugo.
|
|
|
|
var first reflect.Value
|
|
|
|
var method reflect.Value
|
|
|
|
if s.helper != nil {
|
2022-02-17 15:51:19 +00:00
|
|
|
method, first = s.helper.GetMethod(s.ctx, s.prep, ptr, fieldName)
|
2019-11-27 12:42:36 +00:00
|
|
|
} else {
|
|
|
|
method = ptr.MethodByName(fieldName)
|
|
|
|
}
|
|
|
|
|
|
|
|
if method.IsValid() {
|
|
|
|
if first != zero {
|
2022-03-16 08:04:30 +00:00
|
|
|
return s.evalCall(dot, method, false, node, fieldName, args, final, first)
|
2019-11-27 12:42:36 +00:00
|
|
|
}
|
|
|
|
|
2022-03-16 08:04:30 +00:00
|
|
|
return s.evalCall(dot, method, false, node, fieldName, args, final)
|
2019-12-10 18:56:44 +00:00
|
|
|
}
|
2019-11-27 12:42:36 +00:00
|
|
|
|
2022-03-16 08:04:30 +00:00
|
|
|
if method := ptr.MethodByName(fieldName); method.IsValid() {
|
|
|
|
return s.evalCall(dot, method, false, node, fieldName, args, final)
|
|
|
|
}
|
2019-12-10 18:56:44 +00:00
|
|
|
hasArgs := len(args) > 1 || final != missingVal
|
|
|
|
// It's not a method; must be a field of a struct or an element of a map.
|
|
|
|
switch receiver.Kind() {
|
|
|
|
case reflect.Struct:
|
|
|
|
tField, ok := receiver.Type().FieldByName(fieldName)
|
|
|
|
if ok {
|
2022-03-16 08:04:30 +00:00
|
|
|
field, err := receiver.FieldByIndexErr(tField.Index)
|
|
|
|
if !tField.IsExported() {
|
2019-12-10 18:56:44 +00:00
|
|
|
s.errorf("%s is an unexported field of struct type %s", fieldName, typ)
|
|
|
|
}
|
2022-03-16 08:04:30 +00:00
|
|
|
if err != nil {
|
|
|
|
s.errorf("%v", err)
|
|
|
|
}
|
2019-12-10 18:56:44 +00:00
|
|
|
// If it's a function, we must call it.
|
|
|
|
if hasArgs {
|
|
|
|
s.errorf("%s has arguments but cannot be invoked as function", fieldName)
|
|
|
|
}
|
|
|
|
return field
|
|
|
|
}
|
|
|
|
case reflect.Map:
|
|
|
|
// If it's a map, attempt to use the field name as a key.
|
|
|
|
nameVal := reflect.ValueOf(fieldName)
|
|
|
|
if nameVal.Type().AssignableTo(receiver.Type().Key()) {
|
|
|
|
if hasArgs {
|
|
|
|
s.errorf("%s is not a method but has arguments", fieldName)
|
|
|
|
}
|
|
|
|
var result reflect.Value
|
|
|
|
if s.helper != nil {
|
2019-12-12 10:44:36 +00:00
|
|
|
// Added for Hugo.
|
2022-02-17 15:51:19 +00:00
|
|
|
result, _ = s.helper.GetMapValue(s.ctx, s.prep, receiver, nameVal)
|
2019-12-10 18:56:44 +00:00
|
|
|
} else {
|
|
|
|
result = receiver.MapIndex(nameVal)
|
|
|
|
}
|
|
|
|
if !result.IsValid() {
|
|
|
|
switch s.tmpl.option.missingKey {
|
|
|
|
case mapInvalid:
|
|
|
|
// Just use the invalid value.
|
|
|
|
case mapZeroValue:
|
|
|
|
result = reflect.Zero(receiver.Type().Elem())
|
|
|
|
case mapError:
|
|
|
|
s.errorf("map has no entry for key %q", fieldName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
2022-03-16 08:04:30 +00:00
|
|
|
case reflect.Pointer:
|
2019-12-10 18:56:44 +00:00
|
|
|
etyp := receiver.Type().Elem()
|
|
|
|
if etyp.Kind() == reflect.Struct {
|
|
|
|
if _, ok := etyp.FieldByName(fieldName); !ok {
|
|
|
|
// If there's no such field, say "can't evaluate"
|
|
|
|
// instead of "nil pointer evaluating".
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if isNil {
|
|
|
|
s.errorf("nil pointer evaluating %s.%s", typ, fieldName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.errorf("can't evaluate field %s in type %s", fieldName, typ)
|
|
|
|
panic("not reached")
|
2019-12-10 07:02:15 +00:00
|
|
|
}
|
2019-11-27 12:42:36 +00:00
|
|
|
|
|
|
|
// evalCall executes a function or method call. If it's a method, fun already has the receiver bound, so
|
|
|
|
// it looks just like a function call. The arg list, if non-nil, includes (in the manner of the shell), arg[0]
|
|
|
|
// as the function itself.
|
2022-03-16 08:04:30 +00:00
|
|
|
func (s *state) evalCall(dot, fun reflect.Value, isBuiltin bool, node parse.Node, name string, args []parse.Node, final reflect.Value, first ...reflect.Value) reflect.Value {
|
2019-11-27 12:42:36 +00:00
|
|
|
if args != nil {
|
|
|
|
args = args[1:] // Zeroth arg is function name/node; not passed to function.
|
|
|
|
}
|
2022-03-16 08:04:30 +00:00
|
|
|
|
2019-11-27 12:42:36 +00:00
|
|
|
typ := fun.Type()
|
|
|
|
numFirst := len(first)
|
2022-03-16 08:04:30 +00:00
|
|
|
numIn := len(args) + numFirst // Added for Hugo
|
2019-11-27 12:42:36 +00:00
|
|
|
if final != missingVal {
|
|
|
|
numIn++
|
|
|
|
}
|
2022-03-16 08:04:30 +00:00
|
|
|
numFixed := len(args) + len(first) // Adjusted for Hugo
|
2019-11-27 12:42:36 +00:00
|
|
|
if typ.IsVariadic() {
|
|
|
|
numFixed = typ.NumIn() - 1 // last arg is the variadic one.
|
|
|
|
if numIn < numFixed {
|
|
|
|
s.errorf("wrong number of args for %s: want at least %d got %d", name, typ.NumIn()-1, len(args))
|
|
|
|
}
|
|
|
|
} else if numIn != typ.NumIn() {
|
|
|
|
s.errorf("wrong number of args for %s: want %d got %d", name, typ.NumIn(), numIn)
|
|
|
|
}
|
|
|
|
if !goodFunc(typ) {
|
|
|
|
// TODO: This could still be a confusing error; maybe goodFunc should provide info.
|
|
|
|
s.errorf("can't call method/function %q with %d results", name, typ.NumOut())
|
|
|
|
}
|
2022-03-16 08:04:30 +00:00
|
|
|
|
|
|
|
unwrap := func(v reflect.Value) reflect.Value {
|
|
|
|
if v.Type() == reflectValueType {
|
|
|
|
v = v.Interface().(reflect.Value)
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
// Special case for builtin and/or, which short-circuit.
|
|
|
|
if isBuiltin && (name == "and" || name == "or") {
|
|
|
|
argType := typ.In(0)
|
|
|
|
var v reflect.Value
|
|
|
|
for _, arg := range args {
|
|
|
|
v = s.evalArg(dot, argType, arg).Interface().(reflect.Value)
|
|
|
|
if truth(v) == (name == "or") {
|
|
|
|
// This value was already unwrapped
|
|
|
|
// by the .Interface().(reflect.Value).
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if final != missingVal {
|
|
|
|
// The last argument to and/or is coming from
|
|
|
|
// the pipeline. We didn't short circuit on an earlier
|
|
|
|
// argument, so we are going to return this one.
|
|
|
|
// We don't have to evaluate final, but we do
|
|
|
|
// have to check its type. Then, since we are
|
|
|
|
// going to return it, we have to unwrap it.
|
|
|
|
v = unwrap(s.validateType(final, argType))
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2019-11-27 12:42:36 +00:00
|
|
|
// Build the arg list.
|
|
|
|
argv := make([]reflect.Value, numIn)
|
|
|
|
// Args must be evaluated. Fixed args first.
|
2022-03-16 08:04:30 +00:00
|
|
|
i := len(first) // Adjusted for Hugo.
|
|
|
|
for ; i < numFixed && i < len(args)+numFirst; i++ { // Adjusted for Hugo.
|
|
|
|
argv[i] = s.evalArg(dot, typ.In(i), args[i-numFirst]) // Adjusted for Hugo.
|
2019-11-27 12:42:36 +00:00
|
|
|
}
|
|
|
|
// Now the ... args.
|
|
|
|
if typ.IsVariadic() {
|
|
|
|
argType := typ.In(typ.NumIn() - 1).Elem() // Argument is a slice.
|
2022-03-16 08:04:30 +00:00
|
|
|
for ; i < len(args)+numFirst; i++ { // Adjusted for Hugo.
|
|
|
|
argv[i] = s.evalArg(dot, argType, args[i-numFirst]) // Adjusted for Hugo.
|
2019-11-27 12:42:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Add final value if necessary.
|
|
|
|
if final != missingVal {
|
|
|
|
t := typ.In(typ.NumIn() - 1)
|
|
|
|
if typ.IsVariadic() {
|
|
|
|
if numIn-1 < numFixed {
|
|
|
|
// The added final argument corresponds to a fixed parameter of the function.
|
|
|
|
// Validate against the type of the actual parameter.
|
|
|
|
t = typ.In(numIn - 1)
|
|
|
|
} else {
|
|
|
|
// The added final argument corresponds to the variadic part.
|
|
|
|
// Validate against the type of the elements of the variadic slice.
|
|
|
|
t = t.Elem()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argv[i] = s.validateType(final, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Added for Hugo
|
|
|
|
for i := 0; i < len(first); i++ {
|
|
|
|
argv[i] = s.validateType(first[i], typ.In(i))
|
|
|
|
}
|
|
|
|
|
|
|
|
v, err := safeCall(fun, argv)
|
|
|
|
// If we have an error that is not nil, stop execution and return that
|
|
|
|
// error to the caller.
|
|
|
|
if err != nil {
|
|
|
|
s.at(node)
|
2022-03-16 08:04:30 +00:00
|
|
|
s.errorf("error calling %s: %w", name, err)
|
2019-11-27 12:42:36 +00:00
|
|
|
}
|
2022-03-16 08:04:30 +00:00
|
|
|
return unwrap(v)
|
2019-11-27 12:42:36 +00:00
|
|
|
}
|
2019-12-18 15:50:34 +00:00
|
|
|
|
|
|
|
func isTrue(val reflect.Value) (truth, ok bool) {
|
|
|
|
return hreflect.IsTruthfulValue(val), true
|
|
|
|
}
|