github.com/myhau/pulumi/pkg/v3@v3.70.2-0.20221116134521-f2775972e587/codegen/go/gen_program_json.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 jsonTemp struct { 13 Name string 14 Value *model.FunctionCallExpression 15 } 16 17 func (jt *jsonTemp) Type() model.Type { 18 return jt.Value.Type() 19 } 20 21 func (jt *jsonTemp) Traverse(traverser hcl.Traverser) (model.Traversable, hcl.Diagnostics) { 22 return jt.Type().Traverse(traverser) 23 } 24 25 func (jt *jsonTemp) SyntaxNode() hclsyntax.Node { 26 return syntax.None 27 } 28 29 type jsonSpiller struct { 30 temps []*jsonTemp 31 count int 32 } 33 34 func (js *jsonSpiller) spillExpression(x model.Expression) (model.Expression, hcl.Diagnostics) { 35 var temp *jsonTemp 36 switch x := x.(type) { 37 case *model.FunctionCallExpression: 38 switch x.Name { 39 case "toJSON": 40 temp = &jsonTemp{ 41 Name: fmt.Sprintf("json%d", js.count), 42 Value: x, 43 } 44 js.temps = append(js.temps, temp) 45 js.count++ 46 default: 47 return x, nil 48 } 49 default: 50 return x, nil 51 } 52 return &model.ScopeTraversalExpression{ 53 RootName: temp.Name, 54 Traversal: hcl.Traversal{hcl.TraverseRoot{Name: ""}}, 55 Parts: []model.Traversable{temp}, 56 }, nil 57 } 58 59 func (g *generator) rewriteToJSON(x model.Expression) (model.Expression, []*spillTemp, hcl.Diagnostics) { 60 return g.rewriteSpills(x, func(x model.Expression) (string, model.Expression, bool) { 61 if call, ok := x.(*model.FunctionCallExpression); ok && call.Name == "toJSON" { 62 return "json", x, true 63 } 64 return "", nil, false 65 }) 66 }