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.
66 lines
1.5 KiB
Go
66 lines
1.5 KiB
Go
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 jsonTemp struct {
|
|
Name string
|
|
Value *model.FunctionCallExpression
|
|
}
|
|
|
|
func (jt *jsonTemp) Type() model.Type {
|
|
return jt.Value.Type()
|
|
}
|
|
|
|
func (jt *jsonTemp) Traverse(traverser hcl.Traverser) (model.Traversable, hcl.Diagnostics) {
|
|
return jt.Type().Traverse(traverser)
|
|
}
|
|
|
|
func (jt *jsonTemp) SyntaxNode() hclsyntax.Node {
|
|
return syntax.None
|
|
}
|
|
|
|
type jsonSpiller struct {
|
|
temps []*jsonTemp
|
|
count int
|
|
}
|
|
|
|
func (js *jsonSpiller) spillExpression(x model.Expression) (model.Expression, hcl.Diagnostics) {
|
|
var temp *jsonTemp
|
|
switch x := x.(type) {
|
|
case *model.FunctionCallExpression:
|
|
switch x.Name {
|
|
case "toJSON":
|
|
temp = &jsonTemp{
|
|
Name: fmt.Sprintf("json%d", js.count),
|
|
Value: x,
|
|
}
|
|
js.temps = append(js.temps, temp)
|
|
js.count++
|
|
default:
|
|
return x, nil
|
|
}
|
|
default:
|
|
return x, nil
|
|
}
|
|
return &model.ScopeTraversalExpression{
|
|
RootName: temp.Name,
|
|
Traversal: hcl.Traversal{hcl.TraverseRoot{Name: ""}},
|
|
Parts: []model.Traversable{temp},
|
|
}, nil
|
|
}
|
|
|
|
func (g *generator) rewriteToJSON(x model.Expression) (model.Expression, []*spillTemp, hcl.Diagnostics) {
|
|
return g.rewriteSpills(x, func(x model.Expression) (string, model.Expression, bool) {
|
|
if call, ok := x.(*model.FunctionCallExpression); ok && call.Name == "toJSON" {
|
|
return "json", x, true
|
|
}
|
|
return "", nil, false
|
|
})
|
|
}
|