From aca8071fd53fc4f60771fe816b1e7c20c5c674fb Mon Sep 17 00:00:00 2001 From: Rob Pike Date: Wed, 15 Feb 2012 16:05:34 +1100 Subject: [PATCH] text/template: evaluate function fields Just an oversight they didn't work and easy to address. Fixes #3025. R=golang-dev, dsymonds CC=golang-dev https://golang.org/cl/5656059 --- src/pkg/text/template/exec.go | 10 +++++++--- src/pkg/text/template/exec_test.go | 8 ++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/pkg/text/template/exec.go b/src/pkg/text/template/exec.go index 973189a8a6..af745286c0 100644 --- a/src/pkg/text/template/exec.go +++ b/src/pkg/text/template/exec.go @@ -419,10 +419,14 @@ func (s *state) evalField(dot reflect.Value, fieldName string, args []parse.Node tField, ok := receiver.Type().FieldByName(fieldName) if ok { field := receiver.FieldByIndex(tField.Index) - if hasArgs { - s.errorf("%s is not a method but has arguments", fieldName) - } if tField.PkgPath == "" { // field is exported + // If it's a function, we must call it. + if field.Type().Kind() == reflect.Func { + return s.evalCall(dot, field, fieldName, args, final) + } + if hasArgs { + s.errorf("%s is not a method or function but has arguments", fieldName) + } return field } } diff --git a/src/pkg/text/template/exec_test.go b/src/pkg/text/template/exec_test.go index 9bb55e48aa..159cf5100d 100644 --- a/src/pkg/text/template/exec_test.go +++ b/src/pkg/text/template/exec_test.go @@ -59,6 +59,8 @@ type T struct { PI *int PSI *[]int NIL *int + // Function (not method) + Func func(...string) string // Template to test evaluation of templates. Tmpl *Template } @@ -118,6 +120,7 @@ var tVal = &T{ Err: errors.New("erroozle"), PI: newInt(23), PSI: newIntSlice(21, 22, 23), + Func: func(s ...string) string { return fmt.Sprint("<", strings.Join(s, "+"), ">") }, Tmpl: Must(New("x").Parse("test template")), // "x" is the value of .X } @@ -297,8 +300,13 @@ var execTests = []execTest{ "{{with $x := .}}{{with .SI}}{{$.GetU.TrueFalse $.True}}{{end}}{{end}}", "true", tVal, true}, + // Function call + {".Func", "-{{.Func}}-", "-<>-", tVal, true}, + {".Func2", "-{{.Func `he` `llo`}}-", "--", tVal, true}, + // Pipelines. {"pipeline", "-{{.Method0 | .Method2 .U16}}-", "-Method2: 16 M0-", tVal, true}, + {"pipeline func", "-{{.Func `llo` | .Func `he` }}-", "->-", tVal, true}, // If. {"if true", "{{if true}}TRUE{{end}}", "TRUE", tVal, true}, -- 2.48.1