forked from carvel-dev/ytt
/
instructions.go
95 lines (76 loc) · 2.98 KB
/
instructions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package template
import (
"fmt"
"strings"
"github.com/get-ytt/ytt/pkg/structmeta"
)
type InstructionSet struct {
SetCtxType InstructionOp
StartCtx InstructionOp
EndCtx InstructionOp
StartNodeAnnotation InstructionOp
CollectNodeAnnotation InstructionOp
StartNode InstructionOp
SetNode InstructionOp
ReplaceNode InstructionOp
}
var (
globalInsSetId = 1
)
func NewInstructionSet() *InstructionSet {
globalInsSetId += 1
uniqueId := globalInsSetId
return &InstructionSet{
SetCtxType: InstructionOp{fmt.Sprintf("__ytt_tpl%d_set_ctx_type", uniqueId)},
StartCtx: InstructionOp{fmt.Sprintf("__ytt_tpl%d_start_ctx", uniqueId)},
EndCtx: InstructionOp{fmt.Sprintf("__ytt_tpl%d_end_ctx", uniqueId)},
StartNodeAnnotation: InstructionOp{fmt.Sprintf("__ytt_tpl%d_start_node_annotation", uniqueId)},
CollectNodeAnnotation: InstructionOp{fmt.Sprintf("__ytt_tpl%d_collect_node_annotation", uniqueId)},
StartNode: InstructionOp{fmt.Sprintf("__ytt_tpl%d_start_node", uniqueId)},
SetNode: InstructionOp{fmt.Sprintf("__ytt_tpl%d_set_node", uniqueId)},
ReplaceNode: InstructionOp{fmt.Sprintf("__ytt_tpl%d_replace_node", uniqueId)},
}
}
func (is *InstructionSet) NewSetCtxType(dialect EvaluationCtxDialectName) Instruction {
return is.SetCtxType.WithArgs(`"` + string(dialect) + `"`)
}
func (is *InstructionSet) NewStartCtx(dialect EvaluationCtxDialectName) Instruction {
return is.StartCtx.WithArgs(`"` + string(dialect) + `"`)
}
func (is *InstructionSet) NewEndCtx() Instruction {
return is.EndCtx.WithArgs()
}
func (is *InstructionSet) NewEndCtxNone() Instruction {
return is.EndCtx.WithArgs("None")
}
func (is *InstructionSet) NewStartNodeAnnotation(nodeTag NodeTag, ann structmeta.Annotation) Instruction {
collectedArgs := is.CollectNodeAnnotation.WithArgs(ann.Content).AsString()
return is.StartNodeAnnotation.WithArgs(nodeTag.AsString(), `"`+string(ann.Name)+`"`, collectedArgs)
}
func (is *InstructionSet) NewStartNode(nodeTag NodeTag) Instruction {
return is.StartNode.WithArgs(nodeTag.AsString())
}
func (is *InstructionSet) NewSetNode(nodeTag NodeTag) Instruction {
return is.SetNode.WithArgs(nodeTag.AsString())
}
func (is *InstructionSet) NewSetNodeValue(nodeTag NodeTag, code string) Instruction {
return is.SetNode.WithArgs(nodeTag.AsString(), "("+code+")")
}
func (is *InstructionSet) NewCode(code string) Instruction {
return Instruction{code: code}
}
type InstructionOp struct {
Name string
}
func (op InstructionOp) WithArgs(args ...string) Instruction {
return Instruction{op: op, code: fmt.Sprintf("%s(%s)", op.Name, strings.Join(args, ", "))}
}
type Instruction struct {
op InstructionOp
code string
}
func (i Instruction) Op() InstructionOp { return i.op }
func (i Instruction) AsString() string { return i.code }
func (i Instruction) WithDebug(info string) Instruction {
return Instruction{op: i.op, code: i.code + " # " + info}
}