-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
gen_program_splat.go
62 lines (52 loc) 路 1.35 KB
/
gen_program_splat.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
package gen
import (
"fmt"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/pulumi/pulumi/pkg/v3/codegen/hcl2/model"
"github.com/pulumi/pulumi/pkg/v3/codegen/hcl2/syntax"
)
type splatTemp struct {
Name string
Value *model.SplatExpression
}
func (st *splatTemp) Type() model.Type {
return st.Value.Type()
}
func (st *splatTemp) Traverse(traverser hcl.Traverser) (model.Traversable, hcl.Diagnostics) {
return st.Type().Traverse(traverser)
}
func (st *splatTemp) SyntaxNode() hclsyntax.Node {
return syntax.None
}
type splatSpiller struct {
temps []*splatTemp
count int
}
func (ss *splatSpiller) spillExpression(x model.Expression) (model.Expression, hcl.Diagnostics) {
var temp *splatTemp
switch x := x.(type) {
case *model.SplatExpression:
temp = &splatTemp{
Name: fmt.Sprintf("splat%d", ss.count),
Value: x,
}
ss.temps = append(ss.temps, temp)
ss.count++
default:
return x, nil
}
return &model.ScopeTraversalExpression{
RootName: temp.Name,
Traversal: hcl.Traversal{hcl.TraverseRoot{Name: ""}},
Parts: []model.Traversable{temp},
}, nil
}
func (g *generator) rewriteSplat(
x model.Expression,
spiller *splatSpiller,
) (model.Expression, []*splatTemp, hcl.Diagnostics) {
spiller.temps = nil
x, diags := model.VisitExpression(x, spiller.spillExpression, nil)
return x, spiller.temps, diags
}