Skip to content

Commit bedfa4e

Browse files
committed
text/template/parse: undo breaking API changes
golang.org/cl/84480 altered the API for the parse package for clarity and consistency. However, the changes also broke the API for consumers of the package. This CL reverts the API to the previous spelling, adding only a single new exported symbol. Fixes #25968 Change-Id: Ieb81054b61eeac7df3bc3864ef446df43c26b80f Reviewed-on: https://go-review.googlesource.com/120355 Reviewed-by: Daniel Martí <[email protected]> Reviewed-by: Rob Pike <[email protected]> Run-TryBot: Daniel Martí <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 4991bc6 commit bedfa4e

File tree

5 files changed

+34
-43
lines changed

5 files changed

+34
-43
lines changed

api/except.txt

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -362,15 +362,6 @@ pkg syscall (openbsd-386-cgo), const SYS_KILL = 37
362362
pkg syscall (openbsd-amd64), const SYS_KILL = 37
363363
pkg syscall (openbsd-amd64-cgo), const SYS_KILL = 37
364364
pkg unicode, const Version = "9.0.0"
365-
pkg text/template/parse, method (*VariableNode) Copy() Node
366-
pkg text/template/parse, method (*VariableNode) String() string
367-
pkg text/template/parse, method (VariableNode) Position() Pos
368-
pkg text/template/parse, method (VariableNode) Type() NodeType
369-
pkg text/template/parse, type PipeNode struct, Decl []*VariableNode
370-
pkg text/template/parse, type VariableNode struct
371-
pkg text/template/parse, type VariableNode struct, Ident []string
372-
pkg text/template/parse, type VariableNode struct, embedded NodeType
373-
pkg text/template/parse, type VariableNode struct, embedded Pos
374365
pkg syscall (windows-386), type CertChainPolicyPara struct, ExtraPolicyPara uintptr
375366
pkg syscall (windows-386), type CertChainPolicyStatus struct, ExtraPolicyStatus uintptr
376367
pkg syscall (windows-386), type CertContext struct, CertInfo uintptr

src/html/template/escape.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func (e *escaper) escape(c context, n parse.Node) context {
142142

143143
// escapeAction escapes an action template node.
144144
func (e *escaper) escapeAction(c context, n *parse.ActionNode) context {
145-
if len(n.Pipe.Vars) != 0 {
145+
if len(n.Pipe.Decl) != 0 {
146146
// A local variable assignment, not an interpolation.
147147
return c
148148
}

src/text/template/exec.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ func (s *state) walk(dot reflect.Value, node parse.Node) {
252252
// Do not pop variables so they persist until next end.
253253
// Also, if the action declares variables, don't print the result.
254254
val := s.evalPipeline(dot, node.Pipe)
255-
if len(node.Pipe.Vars) == 0 {
255+
if len(node.Pipe.Decl) == 0 {
256256
s.printValue(node, val)
257257
}
258258
case *parse.IfNode:
@@ -339,11 +339,11 @@ func (s *state) walkRange(dot reflect.Value, r *parse.RangeNode) {
339339
mark := s.mark()
340340
oneIteration := func(index, elem reflect.Value) {
341341
// Set top var (lexically the second if there are two) to the element.
342-
if len(r.Pipe.Vars) > 0 {
342+
if len(r.Pipe.Decl) > 0 {
343343
s.setTopVar(1, elem)
344344
}
345345
// Set next var (lexically the first if there are two) to the index.
346-
if len(r.Pipe.Vars) > 1 {
346+
if len(r.Pipe.Decl) > 1 {
347347
s.setTopVar(2, index)
348348
}
349349
s.walk(elem, r.List)
@@ -432,11 +432,11 @@ func (s *state) evalPipeline(dot reflect.Value, pipe *parse.PipeNode) (value ref
432432
value = reflect.ValueOf(value.Interface()) // lovely!
433433
}
434434
}
435-
for _, variable := range pipe.Vars {
436-
if pipe.Decl {
437-
s.push(variable.Ident[0], value)
438-
} else {
435+
for _, variable := range pipe.Decl {
436+
if pipe.IsAssign {
439437
s.setVar(variable.Ident[0], value)
438+
} else {
439+
s.push(variable.Ident[0], value)
440440
}
441441
}
442442
return value
@@ -461,7 +461,7 @@ func (s *state) evalCommand(dot reflect.Value, cmd *parse.CommandNode, final ref
461461
case *parse.PipeNode:
462462
// Parenthesized pipeline. The arguments are all inside the pipeline; final is ignored.
463463
return s.evalPipeline(dot, n)
464-
case *parse.AssignNode:
464+
case *parse.VariableNode:
465465
return s.evalVariableNode(dot, n, cmd.Args, final)
466466
}
467467
s.at(firstWord)
@@ -530,7 +530,7 @@ func (s *state) evalChainNode(dot reflect.Value, chain *parse.ChainNode, args []
530530
return s.evalFieldChain(dot, pipe, chain, chain.Field, args, final)
531531
}
532532

533-
func (s *state) evalVariableNode(dot reflect.Value, variable *parse.AssignNode, args []parse.Node, final reflect.Value) reflect.Value {
533+
func (s *state) evalVariableNode(dot reflect.Value, variable *parse.VariableNode, args []parse.Node, final reflect.Value) reflect.Value {
534534
// $x.Field has $x as the first ident, Field as the second. Eval the var, then the fields.
535535
s.at(variable)
536536
value := s.varValue(variable.Ident[0])
@@ -771,7 +771,7 @@ func (s *state) evalArg(dot reflect.Value, typ reflect.Type, n parse.Node) refle
771771
s.errorf("cannot assign nil to %s", typ)
772772
case *parse.FieldNode:
773773
return s.validateType(s.evalFieldNode(dot, arg, []parse.Node{n}, missingVal), typ)
774-
case *parse.AssignNode:
774+
case *parse.VariableNode:
775775
return s.validateType(s.evalVariableNode(dot, arg, nil, missingVal), typ)
776776
case *parse.PipeNode:
777777
return s.validateType(s.evalPipeline(dot, arg), typ)
@@ -889,7 +889,7 @@ func (s *state) evalEmptyInterface(dot reflect.Value, n parse.Node) reflect.Valu
889889
return s.idealConstant(n)
890890
case *parse.StringNode:
891891
return reflect.ValueOf(n.Text)
892-
case *parse.AssignNode:
892+
case *parse.VariableNode:
893893
return s.evalVariableNode(dot, n, nil, missingVal)
894894
case *parse.PipeNode:
895895
return s.evalPipeline(dot, n)

src/text/template/parse/node.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -144,15 +144,15 @@ func (t *TextNode) Copy() Node {
144144
type PipeNode struct {
145145
NodeType
146146
Pos
147-
tr *Tree
148-
Line int // The line number in the input. Deprecated: Kept for compatibility.
149-
Decl bool // The variables are being declared, not assigned
150-
Vars []*AssignNode // Variables in lexical order.
151-
Cmds []*CommandNode // The commands in lexical order.
147+
tr *Tree
148+
Line int // The line number in the input. Deprecated: Kept for compatibility.
149+
IsAssign bool // The variables are being assigned, not declared.
150+
Decl []*VariableNode // Variables in lexical order.
151+
Cmds []*CommandNode // The commands in lexical order.
152152
}
153153

154-
func (t *Tree) newPipeline(pos Pos, line int, vars []*AssignNode) *PipeNode {
155-
return &PipeNode{tr: t, NodeType: NodePipe, Pos: pos, Line: line, Vars: vars}
154+
func (t *Tree) newPipeline(pos Pos, line int, vars []*VariableNode) *PipeNode {
155+
return &PipeNode{tr: t, NodeType: NodePipe, Pos: pos, Line: line, Decl: vars}
156156
}
157157

158158
func (p *PipeNode) append(command *CommandNode) {
@@ -161,8 +161,8 @@ func (p *PipeNode) append(command *CommandNode) {
161161

162162
func (p *PipeNode) String() string {
163163
s := ""
164-
if len(p.Vars) > 0 {
165-
for i, v := range p.Vars {
164+
if len(p.Decl) > 0 {
165+
for i, v := range p.Decl {
166166
if i > 0 {
167167
s += ", "
168168
}
@@ -187,12 +187,12 @@ func (p *PipeNode) CopyPipe() *PipeNode {
187187
if p == nil {
188188
return p
189189
}
190-
var vars []*AssignNode
191-
for _, d := range p.Vars {
192-
vars = append(vars, d.Copy().(*AssignNode))
190+
var vars []*VariableNode
191+
for _, d := range p.Decl {
192+
vars = append(vars, d.Copy().(*VariableNode))
193193
}
194194
n := p.tr.newPipeline(p.Pos, p.Line, vars)
195-
n.Decl = p.Decl
195+
n.IsAssign = p.IsAssign
196196
for _, c := range p.Cmds {
197197
n.append(c.Copy().(*CommandNode))
198198
}
@@ -321,18 +321,18 @@ func (i *IdentifierNode) Copy() Node {
321321

322322
// AssignNode holds a list of variable names, possibly with chained field
323323
// accesses. The dollar sign is part of the (first) name.
324-
type AssignNode struct {
324+
type VariableNode struct {
325325
NodeType
326326
Pos
327327
tr *Tree
328328
Ident []string // Variable name and fields in lexical order.
329329
}
330330

331-
func (t *Tree) newVariable(pos Pos, ident string) *AssignNode {
332-
return &AssignNode{tr: t, NodeType: NodeVariable, Pos: pos, Ident: strings.Split(ident, ".")}
331+
func (t *Tree) newVariable(pos Pos, ident string) *VariableNode {
332+
return &VariableNode{tr: t, NodeType: NodeVariable, Pos: pos, Ident: strings.Split(ident, ".")}
333333
}
334334

335-
func (v *AssignNode) String() string {
335+
func (v *VariableNode) String() string {
336336
s := ""
337337
for i, id := range v.Ident {
338338
if i > 0 {
@@ -343,12 +343,12 @@ func (v *AssignNode) String() string {
343343
return s
344344
}
345345

346-
func (v *AssignNode) tree() *Tree {
346+
func (v *VariableNode) tree() *Tree {
347347
return v.tr
348348
}
349349

350-
func (v *AssignNode) Copy() Node {
351-
return &AssignNode{tr: v.tr, NodeType: NodeVariable, Pos: v.Pos, Ident: append([]string{}, v.Ident...)}
350+
func (v *VariableNode) Copy() Node {
351+
return &VariableNode{tr: v.tr, NodeType: NodeVariable, Pos: v.Pos, Ident: append([]string{}, v.Ident...)}
352352
}
353353

354354
// DotNode holds the special identifier '.'.

src/text/template/parse/parse.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ func (t *Tree) action() (n Node) {
384384
// declarations? command ('|' command)*
385385
func (t *Tree) pipeline(context string) (pipe *PipeNode) {
386386
decl := false
387-
var vars []*AssignNode
387+
var vars []*VariableNode
388388
token := t.peekNonSpace()
389389
pos := token.pos
390390
// Are there declarations or assignments?
@@ -422,7 +422,7 @@ func (t *Tree) pipeline(context string) (pipe *PipeNode) {
422422
break
423423
}
424424
pipe = t.newPipeline(pos, token.line, vars)
425-
pipe.Decl = decl
425+
pipe.IsAssign = !decl
426426
for {
427427
switch token := t.nextNonSpace(); token.typ {
428428
case itemRightDelim, itemRightParen:

0 commit comments

Comments
 (0)