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