/
gen_program_read_dir.go
69 lines (59 loc) 路 1.53 KB
/
gen_program_read_dir.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
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 readDirTemp struct {
Name string
Value *model.FunctionCallExpression
}
func (rt *readDirTemp) Type() model.Type {
return rt.Value.Type()
}
func (rt *readDirTemp) Traverse(traverser hcl.Traverser) (model.Traversable, hcl.Diagnostics) {
return rt.Type().Traverse(traverser)
}
func (rt *readDirTemp) SyntaxNode() hclsyntax.Node {
return syntax.None
}
type readDirSpiller struct {
temps []*readDirTemp
count int
}
func (rs *readDirSpiller) spillExpression(x model.Expression) (model.Expression, hcl.Diagnostics) {
var temp *readDirTemp
scopeName := ""
switch x := x.(type) {
case *model.FunctionCallExpression:
switch x.Name {
case "readDir":
scopeName = fmt.Sprintf("fileNames%d", rs.count)
temp = &readDirTemp{
Name: fmt.Sprintf("files%d", rs.count),
Value: x,
}
rs.temps = append(rs.temps, temp)
rs.count++
default:
return x, nil
}
default:
return x, nil
}
return &model.ScopeTraversalExpression{
RootName: scopeName,
Traversal: hcl.Traversal{hcl.TraverseRoot{Name: ""}},
Parts: []model.Traversable{temp},
}, nil
}
func (g *generator) rewriteReadDir(
x model.Expression,
spiller *readDirSpiller,
) (model.Expression, []*readDirTemp, hcl.Diagnostics) {
spiller.temps = nil
x, diags := model.VisitExpression(x, spiller.spillExpression, nil)
return x, spiller.temps, diags
}