Commit 3e79c958 authored by Rob Pike's avatar Rob Pike

exp/template: plain actions with declarations should produce no output.

This is already the behavior for pipelines producing values for if, with, and range.

R=golang-dev, gri
CC=golang-dev
https://golang.org/cl/4808050
parent 47647b98
...@@ -158,7 +158,8 @@ The initialization has syntax ...@@ -158,7 +158,8 @@ The initialization has syntax
$variable := pipeline $variable := pipeline
where $variable is the name of the variable. where $variable is the name of the variable. An action that declares a
variable produces no output.
If a "range" action initializes a variable, the variable is set to the If a "range" action initializes a variable, the variable is set to the
successive elements of the iteration. Also, a "range" may declare two successive elements of the iteration. Also, a "range" may declare two
......
...@@ -99,7 +99,11 @@ func (s *state) walk(dot reflect.Value, n node) { ...@@ -99,7 +99,11 @@ func (s *state) walk(dot reflect.Value, n node) {
case *actionNode: case *actionNode:
s.line = n.line s.line = n.line
// Do not pop variables so they persist until next end. // Do not pop variables so they persist until next end.
s.printValue(n, s.evalPipeline(dot, n.pipe)) // Also, if the action declares variables, don't print the result.
val := s.evalPipeline(dot, n.pipe)
if len(n.pipe.decl) == 0 {
s.printValue(n, val)
}
case *ifNode: case *ifNode:
s.line = n.line s.line = n.line
s.walkIfOrWith(nodeIf, dot, n.pipe, n.list, n.elseList) s.walkIfOrWith(nodeIf, dot, n.pipe, n.list, n.elseList)
......
...@@ -197,7 +197,7 @@ var execTests = []execTest{ ...@@ -197,7 +197,7 @@ var execTests = []execTest{
{"$ int", "{{$}}", "123", 123, true}, {"$ int", "{{$}}", "123", 123, true},
{"$.I", "{{$.I}}", "17", tVal, true}, {"$.I", "{{$.I}}", "17", tVal, true},
{"$.U.V", "{{$.U.V}}", "v", tVal, true}, {"$.U.V", "{{$.U.V}}", "v", tVal, true},
{"declare in action", "{{$x := $.U.V}},{{$x}}", "v,v", tVal, true}, {"declare in action", "{{$x := $.U.V}}{{$x}}", "v", tVal, true},
// Pointers. // Pointers.
{"*int", "{{.PI}}", "23", tVal, true}, {"*int", "{{.PI}}", "23", tVal, true},
...@@ -312,7 +312,7 @@ var execTests = []execTest{ ...@@ -312,7 +312,7 @@ var execTests = []execTest{
{"with empty interface, struct field", "{{with .Empty4}}{{.V}}{{end}}", "UinEmpty", tVal, true}, {"with empty interface, struct field", "{{with .Empty4}}{{.V}}{{end}}", "UinEmpty", tVal, true},
{"with $x int", "{{with $x := .I}}{{$x}}{{end}}", "17", tVal, true}, {"with $x int", "{{with $x := .I}}{{$x}}{{end}}", "17", tVal, true},
{"with $x struct.U.V", "{{with $x := $}}{{$x.U.V}}{{end}}", "v", tVal, true}, {"with $x struct.U.V", "{{with $x := $}}{{$x.U.V}}{{end}}", "v", tVal, true},
{"with variable and action", "{{with $x := $}}{{$y := $.U.V}},{{$y}}{{end}}", "v,v", tVal, true}, {"with variable and action", "{{with $x := $}}{{$y := $.U.V}}{{$y}}{{end}}", "v", tVal, true},
// Range. // Range.
{"range []int", "{{range .SI}}-{{.}}-{{end}}", "-3--4--5-", tVal, true}, {"range []int", "{{range .SI}}-{{.}}-{{end}}", "-3--4--5-", tVal, true},
...@@ -331,7 +331,7 @@ var execTests = []execTest{ ...@@ -331,7 +331,7 @@ var execTests = []execTest{
{"range $x MSIone", "{{range $x := .MSIone}}<{{$x}}>{{end}}", "<1>", tVal, true}, {"range $x MSIone", "{{range $x := .MSIone}}<{{$x}}>{{end}}", "<1>", tVal, true},
{"range $x $y MSIone", "{{range $x, $y := .MSIone}}<{{$x}}={{$y}}>{{end}}", "<one=1>", tVal, true}, {"range $x $y MSIone", "{{range $x, $y := .MSIone}}<{{$x}}={{$y}}>{{end}}", "<one=1>", tVal, true},
{"range $x PSI", "{{range $x := .PSI}}<{{$x}}>{{end}}", "<21><22><23>", tVal, true}, {"range $x PSI", "{{range $x := .PSI}}<{{$x}}>{{end}}", "<21><22><23>", tVal, true},
{"declare in range", "{{range $x := .PSI}}<{{$foo:=$x}}>{{end}}", "<21><22><23>", tVal, true}, {"declare in range", "{{range $x := .PSI}}<{{$foo:=$x}}{{$x}}>{{end}}", "<21><22><23>", tVal, true},
// Cute examples. // Cute examples.
{"or as if true", `{{or .SI "slice is empty"}}`, "[3 4 5]", tVal, true}, {"or as if true", `{{or .SI "slice is empty"}}`, "[3 4 5]", tVal, true},
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment