github.com/myhau/pulumi/pkg/v3@v3.70.2-0.20221116134521-f2775972e587/codegen/go/gen_program_read_dir.go (about) 1 package gen 2 3 import ( 4 "fmt" 5 6 "github.com/hashicorp/hcl/v2" 7 "github.com/hashicorp/hcl/v2/hclsyntax" 8 "github.com/pulumi/pulumi/pkg/v3/codegen/hcl2/model" 9 "github.com/pulumi/pulumi/pkg/v3/codegen/hcl2/syntax" 10 ) 11 12 type readDirTemp struct { 13 Name string 14 Value *model.FunctionCallExpression 15 } 16 17 func (rt *readDirTemp) Type() model.Type { 18 return rt.Value.Type() 19 } 20 21 func (rt *readDirTemp) Traverse(traverser hcl.Traverser) (model.Traversable, hcl.Diagnostics) { 22 return rt.Type().Traverse(traverser) 23 } 24 25 func (rt *readDirTemp) SyntaxNode() hclsyntax.Node { 26 return syntax.None 27 } 28 29 type readDirSpiller struct { 30 temps []*readDirTemp 31 count int 32 } 33 34 func (rs *readDirSpiller) spillExpression(x model.Expression) (model.Expression, hcl.Diagnostics) { 35 var temp *readDirTemp 36 scopeName := "" 37 switch x := x.(type) { 38 case *model.FunctionCallExpression: 39 switch x.Name { 40 case "readDir": 41 scopeName = fmt.Sprintf("fileNames%d", rs.count) 42 temp = &readDirTemp{ 43 Name: fmt.Sprintf("files%d", rs.count), 44 Value: x, 45 } 46 rs.temps = append(rs.temps, temp) 47 rs.count++ 48 default: 49 return x, nil 50 } 51 default: 52 return x, nil 53 } 54 return &model.ScopeTraversalExpression{ 55 RootName: scopeName, 56 Traversal: hcl.Traversal{hcl.TraverseRoot{Name: ""}}, 57 Parts: []model.Traversable{temp}, 58 }, nil 59 } 60 61 func (g *generator) rewriteReadDir( 62 x model.Expression, 63 spiller *readDirSpiller, 64 ) (model.Expression, []*readDirTemp, hcl.Diagnostics) { 65 spiller.temps = nil 66 x, diags := model.VisitExpression(x, spiller.spillExpression, nil) 67 68 return x, spiller.temps, diags 69 70 }