pulumi/pkg/codegen/go/gen_spill.go
Pat Gavlin 7b1d6ec1ac
Reify Input and Optional types in the schema type system. (#7059)
These changes support arbitrary combinations of input + plain types
within a schema. Handling plain types at the property level was not
sufficient to support such combinations. Reifying these types
required updating quite a bit of code. This is likely to have caused
some temporary complications, but should eventually lead to
substantial simplification in the SDK and program code generators.

With the new design, input and optional types are explicit in the schema
type system. Optionals will only appear at the outermost level of a type
(i.e. Input<Optional<>>, Array<Optional<>>, etc. will not occur). In
addition to explicit input types, each object type now has a "plain"
shape and an "input" shape. The former uses only plain types; the latter
uses input shapes wherever a plain type is not specified. Plain types
are indicated in the schema by setting the "plain" property of a type spec
to true.
2021-06-24 09:17:55 -07:00

75 lines
1.5 KiB
Go

package gen
import (
"fmt"
"github.com/hashicorp/hcl/v2"
"github.com/pulumi/pulumi/pkg/v3/codegen/hcl2/model"
)
type spillFunc func(x model.Expression) (string, model.Expression, bool)
type spillTemp struct {
Kind string
Variable *model.Variable
Value model.Expression
}
type spills struct {
counts map[string]int
}
func (s *spills) newTemp(kind string, value model.Expression) *spillTemp {
i := s.counts[kind]
s.counts[kind] = i + 1
v := &model.Variable{
Name: fmt.Sprintf("%s%d", kind, i),
VariableType: value.Type(),
}
return &spillTemp{
Variable: v,
Value: value,
}
}
type spiller struct {
spills *spills
temps []*spillTemp
spill spillFunc
disabled bool
}
func (s *spiller) preVisit(x model.Expression) (model.Expression, hcl.Diagnostics) {
_, isfn := x.(*model.AnonymousFunctionExpression)
if isfn {
s.disabled = true
}
return x, nil
}
func (s *spiller) postVisit(x model.Expression) (model.Expression, hcl.Diagnostics) {
_, isfn := x.(*model.AnonymousFunctionExpression)
if isfn {
s.disabled = false
} else if !s.disabled {
if kind, value, ok := s.spill(x); ok {
t := s.spills.newTemp(kind, value)
s.temps = append(s.temps, t)
return model.VariableReference(t.Variable), nil
}
}
return x, nil
}
func (g *generator) rewriteSpills(
x model.Expression, spill spillFunc) (model.Expression, []*spillTemp, hcl.Diagnostics) {
spiller := &spiller{
spills: g.spills,
spill: spill,
}
x, diags := model.VisitExpression(x, spiller.preVisit, spiller.postVisit)
return x, spiller.temps, diags
}