aboutsummaryrefslogtreecommitdiff
path: root/src/text/template/exec.go
diff options
context:
space:
mode:
authorZxilly <zhouxinyu1001@gmail.com>2024-03-12 10:36:15 +0000
committerGopher Robot <gobot@golang.org>2024-05-24 21:22:24 +0000
commitc506f035d99153accf7a9b322c6596fc7652aea6 (patch)
tree62f84af72b45c340a7102d5a5856692f45e12c46 /src/text/template/exec.go
parentb89f946c8814b3d984f06cd836c74ef95bc0b868 (diff)
downloadgo-c506f035d99153accf7a9b322c6596fc7652aea6.tar.xz
text/template: add detailed info for goodFunc check
goodFunc now returns a error describe the exact error it met. builtin call function can print the name of the callee function if the goodFunc check failed. For input {{call .InvalidReturnCountFunc}} before: can't evaluate field InvalidReturnTypeFunc in type *template.T after: invalid function signature for .InvalidReturnTypeFunc: second argument should be error; is bool Change-Id: I9aa53424ac9a2bffbdbeac889390f41218817575 GitHub-Last-Rev: 7c1e0dbd08884a38d92a42530104884a9ca52b44 GitHub-Pull-Request: golang/go#65509 Reviewed-on: https://go-review.googlesource.com/c/go/+/561115 Reviewed-by: Rob Pike <r@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Commit-Queue: Ian Lance Taylor <iant@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Diffstat (limited to 'src/text/template/exec.go')
-rw-r--r--src/text/template/exec.go14
1 files changed, 11 insertions, 3 deletions
diff --git a/src/text/template/exec.go b/src/text/template/exec.go
index 1a8f2fa0df..5b35b3e5a8 100644
--- a/src/text/template/exec.go
+++ b/src/text/template/exec.go
@@ -734,9 +734,8 @@ func (s *state) evalCall(dot, fun reflect.Value, isBuiltin bool, node parse.Node
} 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())
+ if err := goodFunc(name, typ); err != nil {
+ s.errorf("%v", err)
}
unwrap := func(v reflect.Value) reflect.Value {
@@ -800,6 +799,15 @@ func (s *state) evalCall(dot, fun reflect.Value, isBuiltin bool, node parse.Node
}
argv[i] = s.validateType(final, t)
}
+
+ // Special case for the "call" builtin.
+ // Insert the name of the callee function as the first argument.
+ if isBuiltin && name == "call" {
+ calleeName := args[0].String()
+ argv = append([]reflect.Value{reflect.ValueOf(calleeName)}, argv...)
+ fun = reflect.ValueOf(call)
+ }
+
v, err := safeCall(fun, argv)
// If we have an error that is not nil, stop execution and return that
// error to the caller.