From 5378dd77684e2eee5f05aab4b77497bb635fd544 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Wed, 19 Oct 2016 09:11:16 -0400 Subject: text/template: add support for reflect.Value args, results in funcs Add support for passing reflect.Values to and returning reflect.Values from any registered functions in the FuncMap, much as if they were interface{} values. Keeping the reflect.Value instead of round-tripping to interface{} preserves addressability of the value, which is important for method lookup. Change index and a few other built-in functions to use reflect.Values, making a loop using explicit indexing now match the semantics that range has always had. Fixes #14916. Change-Id: Iae1a2fd9bb426886a7fcd9204f30a2d6ad4646ad Reviewed-on: https://go-review.googlesource.com/31462 Reviewed-by: Rob Pike --- src/text/template/exec.go | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) (limited to 'src/text/template/exec.go') diff --git a/src/text/template/exec.go b/src/text/template/exec.go index 8e5ad93ca6..5a6e454ec6 100644 --- a/src/text/template/exec.go +++ b/src/text/template/exec.go @@ -171,13 +171,19 @@ func (t *Template) ExecuteTemplate(wr io.Writer, name string, data interface{}) // execution stops, but partial results may already have been written to // the output writer. // A template may be executed safely in parallel. +// +// If data is a reflect.Value, the template applies to the concrete +// value that the reflect.Value holds, as in fmt.Print. func (t *Template) Execute(wr io.Writer, data interface{}) error { return t.execute(wr, data) } func (t *Template) execute(wr io.Writer, data interface{}) (err error) { defer errRecover(&err) - value := reflect.ValueOf(data) + value, ok := data.(reflect.Value) + if !ok { + value = reflect.ValueOf(data) + } state := &state{ tmpl: t, wr: wr, @@ -596,8 +602,9 @@ func (s *state) evalField(dot reflect.Value, fieldName string, node parse.Node, } var ( - errorType = reflect.TypeOf((*error)(nil)).Elem() - fmtStringerType = reflect.TypeOf((*fmt.Stringer)(nil)).Elem() + errorType = reflect.TypeOf((*error)(nil)).Elem() + fmtStringerType = reflect.TypeOf((*fmt.Stringer)(nil)).Elem() + reflectValueType = reflect.TypeOf((*reflect.Value)(nil)).Elem() ) // evalCall executes a function or method call. If it's a method, fun already has the receiver bound, so @@ -661,7 +668,11 @@ func (s *state) evalCall(dot, fun reflect.Value, node parse.Node, name string, a s.at(node) s.errorf("error calling %s: %s", name, result[1].Interface().(error)) } - return result[0] + v := result[0] + if v.Type() == reflectValueType { + v = v.Interface().(reflect.Value) + } + return v } // canBeNil reports whether an untyped nil can be assigned to the type. See reflect.Zero. @@ -669,6 +680,8 @@ func canBeNil(typ reflect.Type) bool { switch typ.Kind() { case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice: return true + case reflect.Struct: + return typ == reflectValueType } return false } @@ -682,6 +695,9 @@ func (s *state) validateType(value reflect.Value, typ reflect.Type) reflect.Valu } s.errorf("invalid value; expected %s", typ) } + if typ == reflectValueType && value.Type() != typ { + return reflect.ValueOf(value) + } if typ != nil && !value.Type().AssignableTo(typ) { if value.Kind() == reflect.Interface && !value.IsNil() { value = value.Elem() @@ -743,6 +759,10 @@ func (s *state) evalArg(dot reflect.Value, typ reflect.Type, n parse.Node) refle if typ.NumMethod() == 0 { return s.evalEmptyInterface(dot, n) } + case reflect.Struct: + if typ == reflectValueType { + return reflect.ValueOf(s.evalEmptyInterface(dot, n)) + } case reflect.String: return s.evalString(typ, n) case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: -- cgit v1.3-5-g9baa