From 28c1ad9d35f27b3b57afff4ee78faac746a8ed0a Mon Sep 17 00:00:00 2001 From: Daniel Martí Date: Sun, 17 Dec 2017 13:23:40 +0000 Subject: text/template: add variable assignments MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Variables can be declared and shadowing is supported, but modifying existing variables via assignments was not available. This meant that modifying a variable from a nested block was not possible: {{ $v := "init" }} {{ if true }} {{ $v := "changed" }} {{ end }} v: {{ $v }} {{/* "init" */}} Introduce the "=" assignment token, such that one can now do: {{ $v := "init" }} {{ if true }} {{ $v = "changed" }} {{ end }} v: {{ $v }} {{/* "changed" */}} To avoid confusion, rename PipeNode.Decl to PipeNode.Vars, as the variables may not always be declared after this change. Also change a few other names to better reflect the added ambiguity of variables in pipelines. Modifying the text/template/parse package in a backwards incompatible manner is acceptable, given that the package godoc clearly states that it isn't intended for general use. It's the equivalent of an internal package, back when internal packages didn't exist yet. To make the changes to the parse package sit well with the cmd/api test, update except.txt with the changes that we aren't worried about. Fixes #10608. Change-Id: I1f83a4297ee093fd45f9993cebb78fc9a9e81295 Reviewed-on: https://go-review.googlesource.com/84480 Run-TryBot: Daniel Martí TryBot-Result: Gobot Gobot Reviewed-by: Rob Pike --- src/text/template/parse/node.go | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) (limited to 'src/text/template/parse/node.go') diff --git a/src/text/template/parse/node.go b/src/text/template/parse/node.go index 55ff46c17a..737172dfdd 100644 --- a/src/text/template/parse/node.go +++ b/src/text/template/parse/node.go @@ -145,13 +145,14 @@ type PipeNode struct { NodeType Pos tr *Tree - Line int // The line number in the input. Deprecated: Kept for compatibility. - Decl []*VariableNode // Variable declarations in lexical order. - Cmds []*CommandNode // The commands in lexical order. + Line int // The line number in the input. Deprecated: Kept for compatibility. + Decl bool // The variables are being declared, not assigned + Vars []*AssignNode // Variables in lexical order. + Cmds []*CommandNode // The commands in lexical order. } -func (t *Tree) newPipeline(pos Pos, line int, decl []*VariableNode) *PipeNode { - return &PipeNode{tr: t, NodeType: NodePipe, Pos: pos, Line: line, Decl: decl} +func (t *Tree) newPipeline(pos Pos, line int, vars []*AssignNode) *PipeNode { + return &PipeNode{tr: t, NodeType: NodePipe, Pos: pos, Line: line, Vars: vars} } func (p *PipeNode) append(command *CommandNode) { @@ -160,8 +161,8 @@ func (p *PipeNode) append(command *CommandNode) { func (p *PipeNode) String() string { s := "" - if len(p.Decl) > 0 { - for i, v := range p.Decl { + if len(p.Vars) > 0 { + for i, v := range p.Vars { if i > 0 { s += ", " } @@ -186,11 +187,11 @@ func (p *PipeNode) CopyPipe() *PipeNode { if p == nil { return p } - var decl []*VariableNode - for _, d := range p.Decl { - decl = append(decl, d.Copy().(*VariableNode)) + var vars []*AssignNode + for _, d := range p.Vars { + vars = append(vars, d.Copy().(*AssignNode)) } - n := p.tr.newPipeline(p.Pos, p.Line, decl) + n := p.tr.newPipeline(p.Pos, p.Line, vars) for _, c := range p.Cmds { n.append(c.Copy().(*CommandNode)) } @@ -317,20 +318,20 @@ func (i *IdentifierNode) Copy() Node { return NewIdentifier(i.Ident).SetTree(i.tr).SetPos(i.Pos) } -// VariableNode holds a list of variable names, possibly with chained field +// AssignNode holds a list of variable names, possibly with chained field // accesses. The dollar sign is part of the (first) name. -type VariableNode struct { +type AssignNode struct { NodeType Pos tr *Tree Ident []string // Variable name and fields in lexical order. } -func (t *Tree) newVariable(pos Pos, ident string) *VariableNode { - return &VariableNode{tr: t, NodeType: NodeVariable, Pos: pos, Ident: strings.Split(ident, ".")} +func (t *Tree) newVariable(pos Pos, ident string) *AssignNode { + return &AssignNode{tr: t, NodeType: NodeVariable, Pos: pos, Ident: strings.Split(ident, ".")} } -func (v *VariableNode) String() string { +func (v *AssignNode) String() string { s := "" for i, id := range v.Ident { if i > 0 { @@ -341,12 +342,12 @@ func (v *VariableNode) String() string { return s } -func (v *VariableNode) tree() *Tree { +func (v *AssignNode) tree() *Tree { return v.tr } -func (v *VariableNode) Copy() Node { - return &VariableNode{tr: v.tr, NodeType: NodeVariable, Pos: v.Pos, Ident: append([]string{}, v.Ident...)} +func (v *AssignNode) Copy() Node { + return &AssignNode{tr: v.tr, NodeType: NodeVariable, Pos: v.Pos, Ident: append([]string{}, v.Ident...)} } // DotNode holds the special identifier '.'. -- cgit v1.3-5-g9baa