mirror of
https://github.com/golang/go
synced 2024-11-25 09:07:58 -07:00
template/parse: give if, range, and with a common representation.
No external changes. R=nigeltao CC=golang-dev https://golang.org/cl/4940042
This commit is contained in:
parent
8f37c8845c
commit
8b614b4213
@ -390,8 +390,8 @@ func (e *elseNode) String() string {
|
|||||||
return "{{else}}"
|
return "{{else}}"
|
||||||
}
|
}
|
||||||
|
|
||||||
// IfNode represents an {{if}} action and its commands.
|
// BranchNode is the common representation of if, range, and with.
|
||||||
type IfNode struct {
|
type BranchNode struct {
|
||||||
NodeType
|
NodeType
|
||||||
Line int // The line number in the input.
|
Line int // The line number in the input.
|
||||||
Pipe *PipeNode // The pipeline to be evaluated.
|
Pipe *PipeNode // The pipeline to be evaluated.
|
||||||
@ -399,35 +399,49 @@ type IfNode struct {
|
|||||||
ElseList *ListNode // What to execute if the value is empty (nil if absent).
|
ElseList *ListNode // What to execute if the value is empty (nil if absent).
|
||||||
}
|
}
|
||||||
|
|
||||||
func newIf(line int, pipe *PipeNode, list, elseList *ListNode) *IfNode {
|
func (b *BranchNode) String() string {
|
||||||
return &IfNode{NodeType: NodeIf, Line: line, Pipe: pipe, List: list, ElseList: elseList}
|
name := ""
|
||||||
|
switch b.NodeType {
|
||||||
|
case NodeIf:
|
||||||
|
name = "if"
|
||||||
|
case NodeRange:
|
||||||
|
name = "range"
|
||||||
|
case NodeWith:
|
||||||
|
name = "with"
|
||||||
|
default:
|
||||||
|
panic("unknown branch type")
|
||||||
|
}
|
||||||
|
if b.ElseList != nil {
|
||||||
|
return fmt.Sprintf("({{%s %s}} %s {{else}} %s)", name, b.Pipe, b.List, b.ElseList)
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("({{%s %s}} %s)", name, b.Pipe, b.List)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *IfNode) String() string {
|
// IfNode represents an {{if}} action and its commands.
|
||||||
if i.ElseList != nil {
|
type IfNode struct {
|
||||||
return fmt.Sprintf("({{if %s}} %s {{else}} %s)", i.Pipe, i.List, i.ElseList)
|
BranchNode
|
||||||
}
|
}
|
||||||
return fmt.Sprintf("({{if %s}} %s)", i.Pipe, i.List)
|
|
||||||
|
func newIf(line int, pipe *PipeNode, list, elseList *ListNode) *IfNode {
|
||||||
|
return &IfNode{BranchNode{NodeType: NodeIf, Line: line, Pipe: pipe, List: list, ElseList: elseList}}
|
||||||
}
|
}
|
||||||
|
|
||||||
// RangeNode represents a {{range}} action and its commands.
|
// RangeNode represents a {{range}} action and its commands.
|
||||||
type RangeNode struct {
|
type RangeNode struct {
|
||||||
NodeType
|
BranchNode
|
||||||
Line int // The line number in the input.
|
|
||||||
Pipe *PipeNode // The pipeline to be evaluated.
|
|
||||||
List *ListNode // What to execute if the value is non-empty.
|
|
||||||
ElseList *ListNode // What to execute if the value is empty (nil if absent).
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func newRange(line int, pipe *PipeNode, list, elseList *ListNode) *RangeNode {
|
func newRange(line int, pipe *PipeNode, list, elseList *ListNode) *RangeNode {
|
||||||
return &RangeNode{NodeType: NodeRange, Line: line, Pipe: pipe, List: list, ElseList: elseList}
|
return &RangeNode{BranchNode{NodeType: NodeRange, Line: line, Pipe: pipe, List: list, ElseList: elseList}}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RangeNode) String() string {
|
// WithNode represents a {{with}} action and its commands.
|
||||||
if r.ElseList != nil {
|
type WithNode struct {
|
||||||
return fmt.Sprintf("({{range %s}} %s {{else}} %s)", r.Pipe, r.List, r.ElseList)
|
BranchNode
|
||||||
}
|
}
|
||||||
return fmt.Sprintf("({{range %s}} %s)", r.Pipe, r.List)
|
|
||||||
|
func newWith(line int, pipe *PipeNode, list, elseList *ListNode) *WithNode {
|
||||||
|
return &WithNode{BranchNode{NodeType: NodeWith, Line: line, Pipe: pipe, List: list, ElseList: elseList}}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TemplateNode represents a {{template}} action.
|
// TemplateNode represents a {{template}} action.
|
||||||
@ -448,23 +462,3 @@ func (t *TemplateNode) String() string {
|
|||||||
}
|
}
|
||||||
return fmt.Sprintf("{{template %q %s}}", t.Name, t.Pipe)
|
return fmt.Sprintf("{{template %q %s}}", t.Name, t.Pipe)
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithNode represents a {{with}} action and its commands.
|
|
||||||
type WithNode struct {
|
|
||||||
NodeType
|
|
||||||
Line int // The line number in the input.
|
|
||||||
Pipe *PipeNode // The pipeline to be evaluated.
|
|
||||||
List *ListNode // What to execute if the value is non-empty.
|
|
||||||
ElseList *ListNode // What to execute if the value is empty (nil if absent).
|
|
||||||
}
|
|
||||||
|
|
||||||
func newWith(line int, pipe *PipeNode, list, elseList *ListNode) *WithNode {
|
|
||||||
return &WithNode{NodeType: NodeWith, Line: line, Pipe: pipe, List: list, ElseList: elseList}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (w *WithNode) String() string {
|
|
||||||
if w.ElseList != nil {
|
|
||||||
return fmt.Sprintf("({{with %s}} %s {{else}} %s)", w.Pipe, w.List, w.ElseList)
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("({{with %s}} %s)", w.Pipe, w.List)
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user