1
0
mirror of https://github.com/golang/go synced 2024-11-20 01:14:40 -07:00

text/template: fix bug in evaluating a chain starting with a function.

R=golang-dev, alberto.garcia.hierro
CC=golang-dev
https://golang.org/cl/7861046
This commit is contained in:
Rob Pike 2013-03-27 16:31:14 -07:00
parent b06b77b8cf
commit 99645db926
2 changed files with 19 additions and 10 deletions

View File

@ -619,6 +619,8 @@ func (s *state) evalArg(dot reflect.Value, typ reflect.Type, n parse.Node) refle
return s.validateType(s.evalVariableNode(dot, arg, nil, zero), typ) return s.validateType(s.evalVariableNode(dot, arg, nil, zero), typ)
case *parse.PipeNode: case *parse.PipeNode:
return s.validateType(s.evalPipeline(dot, arg), typ) return s.validateType(s.evalPipeline(dot, arg), typ)
case *parse.IdentifierNode:
return s.evalFunction(dot, arg, arg, nil, zero)
} }
switch typ.Kind() { switch typ.Kind() {
case reflect.Bool: case reflect.Bool:

View File

@ -499,6 +499,8 @@ var execTests = []execTest{
{"bug8b", "{{4|dddArg 3}}", "", tVal, false}, {"bug8b", "{{4|dddArg 3}}", "", tVal, false},
// A bug was introduced that broke map lookups for lower-case names. // A bug was introduced that broke map lookups for lower-case names.
{"bug9", "{{.cause}}", "neglect", map[string]string{"cause": "neglect"}, true}, {"bug9", "{{.cause}}", "neglect", map[string]string{"cause": "neglect"}, true},
// Field chain starting with function did not work.
{"bug10", "{{mapOfThree.three}}-{{(mapOfThree).three}}", "3-3", 0, true},
} }
func zeroArgs() string { func zeroArgs() string {
@ -560,19 +562,24 @@ func stringer(s fmt.Stringer) string {
return s.String() return s.String()
} }
func mapOfThree() interface{} {
return map[string]int{"three": 3}
}
func testExecute(execTests []execTest, template *Template, t *testing.T) { func testExecute(execTests []execTest, template *Template, t *testing.T) {
b := new(bytes.Buffer) b := new(bytes.Buffer)
funcs := FuncMap{ funcs := FuncMap{
"add": add, "add": add,
"count": count, "count": count,
"dddArg": dddArg, "dddArg": dddArg,
"echo": echo, "echo": echo,
"makemap": makemap, "makemap": makemap,
"oneArg": oneArg, "mapOfThree": mapOfThree,
"typeOf": typeOf, "oneArg": oneArg,
"vfunc": vfunc, "stringer": stringer,
"zeroArgs": zeroArgs, "typeOf": typeOf,
"stringer": stringer, "vfunc": vfunc,
"zeroArgs": zeroArgs,
} }
for _, test := range execTests { for _, test := range execTests {
var tmpl *Template var tmpl *Template