Skip to content

text/template: function chaining #3140

@gopherbot

Description

@gopherbot

by robfig@yext.com:

What steps will reproduce the problem?
Apply the attached patch to template/exec_test.go

What is the expected output?
All tests pass

What do you see instead?
10:12:52 [robfig@robfig ~/go/src/pkg/template]
$ gomake test
gotest
rm -f _test/template.a
6g  -o _gotest_.6 exec.go funcs.go helper.go parse.go set.go  exec_test.go set_test.go
rm -f _test/template.a
gopack grc _test/template.a _gotest_.6 
--- FAIL: template.TestExecute (0.01 seconds)
    mapString: expected
        "value"
    got
        ""
    mapNiladicFunc: unexpected execute error: template: mapNiladicFunc:1: wrong number of args for mapNiladicFunc: want 0 got 1
    mapVariadicFunc: unexpected execute error: template: mapVariadicFunc:1: wrong number of args for mapVariadicFunc: want 0 got 4
FAIL
gotest: "./6.out" failed: exit status 1
make: *** [test] Error 2

Which compiler are you using (5g, 6g, 8g, gccgo)?
6g

Which operating system are you using?
OSX

Patch from "hg diff": 

diff -r c1702f36df03 src/pkg/template/exec_test.go
--- a/src/pkg/template/exec_test.go Tue Oct 18 10:55:12 2011 +1100
+++ b/src/pkg/template/exec_test.go Mon Feb 27 10:16:46 2012 -0500
@@ -402,6 +402,11 @@
    {"error method, error", "{{.EPERM true}}", "", tVal, false},
    {"error method, no error", "{{.EPERM false}}", "false", tVal, true},
 
+   // Func chaining.
+   {"mapString", "{{mapString.key}}", "value", tVal, false},
+   {"mapNiladicFunc", "{{mapNiladicFunc.key}}", "value",
tVal, true},
+   {"mapVariadicFunc", "{{mapVariadicFunc.key 1 2 3}}",
"args=[1, 2, 3]", tVal, true},
+
    // Fixed bugs.
    // Must separate dot and receiver; otherwise args are evaluated with dot set to variable.
    {"bug0", "{{range .MSIone}}{{if $.Method1 .}}X{{end}}{{end}}", "X", tVal, true},
@@ -431,6 +436,26 @@
    return "oneArg=" + a
 }
 
+func mapString() map[string]string {
+   return map[string]string{
+       "key": "value",
+   }
+}
+
+func mapNiladicFunc() map[string]func()string {
+   return map[string]func()string {
+       "key": func() string {
+           return "value"
+       },
+   }
+}
+
+func mapVariadicFunc() func(...interface{})string {
+   return func(args ...interface{})string {
+       return fmt.Sprintf("args=%s", args)
+   }
+}
+
 // count returns a channel that will deliver n sequential 1-letter strings starting at "a"
 func count(n int) chan string {
    if n == 0 {
@@ -459,6 +484,9 @@
        "typeOf":   typeOf,
        "vfunc":    vfunc,
        "zeroArgs": zeroArgs,
+       "mapString": mapString,
+       "mapNiladicFunc": mapNiladicFunc,
+       "mapVariadicFunc": mapVariadicFunc,
    }
    for _, test := range execTests {
        tmpl := New(test.name).Funcs(funcs)

Which revision are you using?  (hg identify)


Please provide any additional information below.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions