pulumi/pkg/codegen/go/gen_program_ternaries.go

81 lines
2.1 KiB
Go

// Copyright 2020-2024, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
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 ternaryTemp struct {
Name string
Value *model.ConditionalExpression
}
func (tt *ternaryTemp) Type() model.Type {
return tt.Value.Type()
}
func (tt *ternaryTemp) Traverse(traverser hcl.Traverser) (model.Traversable, hcl.Diagnostics) {
return tt.Type().Traverse(traverser)
}
func (tt *ternaryTemp) SyntaxNode() hclsyntax.Node {
return syntax.None
}
type tempSpiller struct {
temps []*ternaryTemp
count int
}
func (ta *tempSpiller) spillExpression(x model.Expression) (model.Expression, hcl.Diagnostics) {
var temp *ternaryTemp
switch x := x.(type) {
case *model.ConditionalExpression:
x.Condition, _ = ta.spillExpression(x.Condition)
x.TrueResult, _ = ta.spillExpression(x.TrueResult)
x.FalseResult, _ = ta.spillExpression(x.FalseResult)
temp = &ternaryTemp{
Name: fmt.Sprintf("tmp%d", ta.count),
Value: x,
}
ta.temps = append(ta.temps, temp)
ta.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) rewriteTernaries(
x model.Expression,
spiller *tempSpiller,
) (model.Expression, []*ternaryTemp, hcl.Diagnostics) {
spiller.temps = nil
x, diags := model.VisitExpression(x, nil, spiller.spillExpression)
return x, spiller.temps, diags
}