2020-06-23 17:59:26 +00:00
|
|
|
package gen
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/hashicorp/hcl/v2"
|
|
|
|
"github.com/hashicorp/hcl/v2/hclsyntax"
|
2021-06-24 16:17:55 +00:00
|
|
|
"github.com/pulumi/pulumi/pkg/v3/codegen"
|
2021-03-17 13:20:05 +00:00
|
|
|
"github.com/pulumi/pulumi/pkg/v3/codegen/hcl2/model"
|
|
|
|
"github.com/pulumi/pulumi/pkg/v3/codegen/hcl2/syntax"
|
2021-09-30 03:11:56 +00:00
|
|
|
"github.com/pulumi/pulumi/pkg/v3/codegen/pcl"
|
2021-03-17 13:20:05 +00:00
|
|
|
"github.com/pulumi/pulumi/pkg/v3/codegen/schema"
|
2020-06-23 17:59:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type optionalTemp struct {
|
|
|
|
Name string
|
|
|
|
Value model.Expression
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ot *optionalTemp) Type() model.Type {
|
|
|
|
return ot.Value.Type()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ot *optionalTemp) Traverse(traverser hcl.Traverser) (model.Traversable, hcl.Diagnostics) {
|
|
|
|
return ot.Type().Traverse(traverser)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ot *optionalTemp) SyntaxNode() hclsyntax.Node {
|
|
|
|
return syntax.None
|
|
|
|
}
|
|
|
|
|
|
|
|
type optionalSpiller struct {
|
[codegen/go] Improve optional params in invoke
As described in #8821, docs generated for helper functions can be incorrect because optional arguments to parameter objects are not correctly handled.
Previously, code would be generated like so:
```go
policyDocument, err := iam.GetPolicyDocument(ctx, &iam.GetPolicyDocumentArgs{
Statements: []iam.GetPolicyDocumentStatement{
iam.GetPolicyDocumentStatement{
Sid: "1",
//...
```
However "Sid" is of type `*string`.
This four helper conversion functions, to handle the primitive types we lower from, and modifies codegen to recursively apply these functions inside of an invoke call.
In the new code generation, the above is instead rendered as:
```go
policyDocument, err := iam.GetPolicyDocument(ctx, &iam.GetPolicyDocumentArgs{
Statements: []iam.GetPolicyDocumentStatement{
iam.GetPolicyDocumentStatement{
Sid: pulumi.StringRef("1"),
//...
```
2022-02-01 03:18:15 +00:00
|
|
|
invocation *model.FunctionCallExpression
|
|
|
|
intrinsicConvertTo *model.Type
|
2020-06-23 17:59:26 +00:00
|
|
|
}
|
|
|
|
|
[codegen/go] Improve optional params in invoke
As described in #8821, docs generated for helper functions can be incorrect because optional arguments to parameter objects are not correctly handled.
Previously, code would be generated like so:
```go
policyDocument, err := iam.GetPolicyDocument(ctx, &iam.GetPolicyDocumentArgs{
Statements: []iam.GetPolicyDocumentStatement{
iam.GetPolicyDocumentStatement{
Sid: "1",
//...
```
However "Sid" is of type `*string`.
This four helper conversion functions, to handle the primitive types we lower from, and modifies codegen to recursively apply these functions inside of an invoke call.
In the new code generation, the above is instead rendered as:
```go
policyDocument, err := iam.GetPolicyDocument(ctx, &iam.GetPolicyDocumentArgs{
Statements: []iam.GetPolicyDocumentStatement{
iam.GetPolicyDocumentStatement{
Sid: pulumi.StringRef("1"),
//...
```
2022-02-01 03:18:15 +00:00
|
|
|
func (os *optionalSpiller) preVisitor(x model.Expression) (model.Expression, hcl.Diagnostics) {
|
2020-06-23 17:59:26 +00:00
|
|
|
switch x := x.(type) {
|
|
|
|
case *model.FunctionCallExpression:
|
|
|
|
if x.Name == "invoke" {
|
|
|
|
// recurse into invoke args
|
2021-11-18 22:50:51 +00:00
|
|
|
isOutputInvoke, _, _ := pcl.RecognizeOutputVersionedInvoke(x)
|
|
|
|
// ignore output-versioned invokes as they do not need converting
|
[codegen/go] Improve optional params in invoke
As described in #8821, docs generated for helper functions can be incorrect because optional arguments to parameter objects are not correctly handled.
Previously, code would be generated like so:
```go
policyDocument, err := iam.GetPolicyDocument(ctx, &iam.GetPolicyDocumentArgs{
Statements: []iam.GetPolicyDocumentStatement{
iam.GetPolicyDocumentStatement{
Sid: "1",
//...
```
However "Sid" is of type `*string`.
This four helper conversion functions, to handle the primitive types we lower from, and modifies codegen to recursively apply these functions inside of an invoke call.
In the new code generation, the above is instead rendered as:
```go
policyDocument, err := iam.GetPolicyDocument(ctx, &iam.GetPolicyDocumentArgs{
Statements: []iam.GetPolicyDocumentStatement{
iam.GetPolicyDocumentStatement{
Sid: pulumi.StringRef("1"),
//...
```
2022-02-01 03:18:15 +00:00
|
|
|
if !isOutputInvoke {
|
|
|
|
os.invocation = x
|
|
|
|
}
|
|
|
|
os.intrinsicConvertTo = nil
|
|
|
|
return x, nil
|
2020-06-23 17:59:26 +00:00
|
|
|
}
|
2021-09-30 03:11:56 +00:00
|
|
|
if x.Name == pcl.IntrinsicConvert {
|
[codegen/go] Improve optional params in invoke
As described in #8821, docs generated for helper functions can be incorrect because optional arguments to parameter objects are not correctly handled.
Previously, code would be generated like so:
```go
policyDocument, err := iam.GetPolicyDocument(ctx, &iam.GetPolicyDocumentArgs{
Statements: []iam.GetPolicyDocumentStatement{
iam.GetPolicyDocumentStatement{
Sid: "1",
//...
```
However "Sid" is of type `*string`.
This four helper conversion functions, to handle the primitive types we lower from, and modifies codegen to recursively apply these functions inside of an invoke call.
In the new code generation, the above is instead rendered as:
```go
policyDocument, err := iam.GetPolicyDocument(ctx, &iam.GetPolicyDocumentArgs{
Statements: []iam.GetPolicyDocumentStatement{
iam.GetPolicyDocumentStatement{
Sid: pulumi.StringRef("1"),
//...
```
2022-02-01 03:18:15 +00:00
|
|
|
if os.invocation != nil {
|
|
|
|
os.intrinsicConvertTo = &x.Signature.ReturnType
|
|
|
|
}
|
|
|
|
return x, nil
|
2020-06-23 17:59:26 +00:00
|
|
|
}
|
|
|
|
case *model.ObjectConsExpression:
|
[codegen/go] Improve optional params in invoke
As described in #8821, docs generated for helper functions can be incorrect because optional arguments to parameter objects are not correctly handled.
Previously, code would be generated like so:
```go
policyDocument, err := iam.GetPolicyDocument(ctx, &iam.GetPolicyDocumentArgs{
Statements: []iam.GetPolicyDocumentStatement{
iam.GetPolicyDocumentStatement{
Sid: "1",
//...
```
However "Sid" is of type `*string`.
This four helper conversion functions, to handle the primitive types we lower from, and modifies codegen to recursively apply these functions inside of an invoke call.
In the new code generation, the above is instead rendered as:
```go
policyDocument, err := iam.GetPolicyDocument(ctx, &iam.GetPolicyDocumentArgs{
Statements: []iam.GetPolicyDocumentStatement{
iam.GetPolicyDocumentStatement{
Sid: pulumi.StringRef("1"),
//...
```
2022-02-01 03:18:15 +00:00
|
|
|
if os.invocation == nil {
|
2020-06-23 17:59:26 +00:00
|
|
|
return x, nil
|
|
|
|
}
|
[codegen/go] Improve optional params in invoke
As described in #8821, docs generated for helper functions can be incorrect because optional arguments to parameter objects are not correctly handled.
Previously, code would be generated like so:
```go
policyDocument, err := iam.GetPolicyDocument(ctx, &iam.GetPolicyDocumentArgs{
Statements: []iam.GetPolicyDocumentStatement{
iam.GetPolicyDocumentStatement{
Sid: "1",
//...
```
However "Sid" is of type `*string`.
This four helper conversion functions, to handle the primitive types we lower from, and modifies codegen to recursively apply these functions inside of an invoke call.
In the new code generation, the above is instead rendered as:
```go
policyDocument, err := iam.GetPolicyDocument(ctx, &iam.GetPolicyDocumentArgs{
Statements: []iam.GetPolicyDocumentStatement{
iam.GetPolicyDocumentStatement{
Sid: pulumi.StringRef("1"),
//...
```
2022-02-01 03:18:15 +00:00
|
|
|
destType := x.Type()
|
|
|
|
if os.intrinsicConvertTo != nil {
|
|
|
|
destType = *os.intrinsicConvertTo
|
|
|
|
}
|
2021-09-30 03:11:56 +00:00
|
|
|
if schemaType, ok := pcl.GetSchemaForType(destType); ok {
|
2020-06-23 17:59:26 +00:00
|
|
|
if schemaType, ok := schemaType.(*schema.ObjectType); ok {
|
[codegen/go] Improve optional params in invoke
As described in #8821, docs generated for helper functions can be incorrect because optional arguments to parameter objects are not correctly handled.
Previously, code would be generated like so:
```go
policyDocument, err := iam.GetPolicyDocument(ctx, &iam.GetPolicyDocumentArgs{
Statements: []iam.GetPolicyDocumentStatement{
iam.GetPolicyDocumentStatement{
Sid: "1",
//...
```
However "Sid" is of type `*string`.
This four helper conversion functions, to handle the primitive types we lower from, and modifies codegen to recursively apply these functions inside of an invoke call.
In the new code generation, the above is instead rendered as:
```go
policyDocument, err := iam.GetPolicyDocument(ctx, &iam.GetPolicyDocumentArgs{
Statements: []iam.GetPolicyDocumentStatement{
iam.GetPolicyDocumentStatement{
Sid: pulumi.StringRef("1"),
//...
```
2022-02-01 03:18:15 +00:00
|
|
|
// map of item name to optional type wrapper fn
|
|
|
|
optionalPrimitives := make(map[string]schema.Type)
|
2020-06-23 17:59:26 +00:00
|
|
|
for _, v := range schemaType.Properties {
|
[codegen/go] Improve optional params in invoke
As described in #8821, docs generated for helper functions can be incorrect because optional arguments to parameter objects are not correctly handled.
Previously, code would be generated like so:
```go
policyDocument, err := iam.GetPolicyDocument(ctx, &iam.GetPolicyDocumentArgs{
Statements: []iam.GetPolicyDocumentStatement{
iam.GetPolicyDocumentStatement{
Sid: "1",
//...
```
However "Sid" is of type `*string`.
This four helper conversion functions, to handle the primitive types we lower from, and modifies codegen to recursively apply these functions inside of an invoke call.
In the new code generation, the above is instead rendered as:
```go
policyDocument, err := iam.GetPolicyDocument(ctx, &iam.GetPolicyDocumentArgs{
Statements: []iam.GetPolicyDocumentStatement{
iam.GetPolicyDocumentStatement{
Sid: pulumi.StringRef("1"),
//...
```
2022-02-01 03:18:15 +00:00
|
|
|
if !v.IsRequired() {
|
|
|
|
ty := codegen.UnwrapType(v.Type)
|
|
|
|
switch ty {
|
|
|
|
case schema.NumberType, schema.BoolType, schema.IntType, schema.StringType:
|
|
|
|
optionalPrimitives[v.Name] = ty
|
|
|
|
}
|
2020-06-23 17:59:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for i, item := range x.Items {
|
|
|
|
// keys for schematized objects should be simple strings
|
|
|
|
if key, ok := item.Key.(*model.LiteralValueExpression); ok {
|
2021-06-24 16:17:55 +00:00
|
|
|
if model.StringType.AssignableFrom(key.Type()) {
|
2020-06-23 17:59:26 +00:00
|
|
|
strKey := key.Value.AsString()
|
[codegen/go] Improve optional params in invoke
As described in #8821, docs generated for helper functions can be incorrect because optional arguments to parameter objects are not correctly handled.
Previously, code would be generated like so:
```go
policyDocument, err := iam.GetPolicyDocument(ctx, &iam.GetPolicyDocumentArgs{
Statements: []iam.GetPolicyDocumentStatement{
iam.GetPolicyDocumentStatement{
Sid: "1",
//...
```
However "Sid" is of type `*string`.
This four helper conversion functions, to handle the primitive types we lower from, and modifies codegen to recursively apply these functions inside of an invoke call.
In the new code generation, the above is instead rendered as:
```go
policyDocument, err := iam.GetPolicyDocument(ctx, &iam.GetPolicyDocumentArgs{
Statements: []iam.GetPolicyDocumentStatement{
iam.GetPolicyDocumentStatement{
Sid: pulumi.StringRef("1"),
//...
```
2022-02-01 03:18:15 +00:00
|
|
|
if schemaType, isOptional := optionalPrimitives[strKey]; isOptional {
|
|
|
|
functionName := os.getOptionalConversion(schemaType)
|
|
|
|
expectedModelType := os.getExpectedModelType(schemaType)
|
|
|
|
|
|
|
|
x.Items[i].Value = &model.FunctionCallExpression{
|
|
|
|
Name: functionName,
|
|
|
|
Signature: model.StaticFunctionSignature{
|
|
|
|
Parameters: []model.Parameter{{
|
|
|
|
Name: "val",
|
|
|
|
Type: expectedModelType,
|
|
|
|
}},
|
|
|
|
ReturnType: model.NewOptionalType(expectedModelType),
|
|
|
|
},
|
|
|
|
Args: []model.Expression{item.Value},
|
2020-06-23 17:59:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
[codegen/go] Improve optional params in invoke
As described in #8821, docs generated for helper functions can be incorrect because optional arguments to parameter objects are not correctly handled.
Previously, code would be generated like so:
```go
policyDocument, err := iam.GetPolicyDocument(ctx, &iam.GetPolicyDocumentArgs{
Statements: []iam.GetPolicyDocumentStatement{
iam.GetPolicyDocumentStatement{
Sid: "1",
//...
```
However "Sid" is of type `*string`.
This four helper conversion functions, to handle the primitive types we lower from, and modifies codegen to recursively apply these functions inside of an invoke call.
In the new code generation, the above is instead rendered as:
```go
policyDocument, err := iam.GetPolicyDocument(ctx, &iam.GetPolicyDocumentArgs{
Statements: []iam.GetPolicyDocumentStatement{
iam.GetPolicyDocumentStatement{
Sid: pulumi.StringRef("1"),
//...
```
2022-02-01 03:18:15 +00:00
|
|
|
// Clear before visiting children, require another __convert call to set again
|
|
|
|
os.intrinsicConvertTo = nil
|
|
|
|
return x, nil
|
|
|
|
default:
|
|
|
|
// Ditto
|
|
|
|
os.intrinsicConvertTo = nil
|
|
|
|
return x, nil
|
|
|
|
}
|
|
|
|
return x, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (os *optionalSpiller) postVisitor(x model.Expression) (model.Expression, hcl.Diagnostics) {
|
|
|
|
switch x := x.(type) {
|
|
|
|
case *model.FunctionCallExpression:
|
|
|
|
if x.Name == "invoke" {
|
|
|
|
if x == os.invocation {
|
|
|
|
// Clear invocation flag once we're done traversing children.
|
|
|
|
os.invocation = nil
|
|
|
|
}
|
|
|
|
}
|
2020-06-23 17:59:26 +00:00
|
|
|
}
|
|
|
|
return x, nil
|
|
|
|
}
|
|
|
|
|
[codegen/go] Improve optional params in invoke
As described in #8821, docs generated for helper functions can be incorrect because optional arguments to parameter objects are not correctly handled.
Previously, code would be generated like so:
```go
policyDocument, err := iam.GetPolicyDocument(ctx, &iam.GetPolicyDocumentArgs{
Statements: []iam.GetPolicyDocumentStatement{
iam.GetPolicyDocumentStatement{
Sid: "1",
//...
```
However "Sid" is of type `*string`.
This four helper conversion functions, to handle the primitive types we lower from, and modifies codegen to recursively apply these functions inside of an invoke call.
In the new code generation, the above is instead rendered as:
```go
policyDocument, err := iam.GetPolicyDocument(ctx, &iam.GetPolicyDocumentArgs{
Statements: []iam.GetPolicyDocumentStatement{
iam.GetPolicyDocumentStatement{
Sid: pulumi.StringRef("1"),
//...
```
2022-02-01 03:18:15 +00:00
|
|
|
func (*optionalSpiller) getOptionalConversion(ty schema.Type) string {
|
|
|
|
switch ty {
|
|
|
|
case schema.NumberType:
|
|
|
|
return "goOptionalFloat64"
|
|
|
|
case schema.BoolType:
|
|
|
|
return "goOptionalBool"
|
|
|
|
case schema.IntType:
|
|
|
|
return "goOptionalInt"
|
|
|
|
case schema.StringType:
|
|
|
|
return "goOptionalString"
|
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*optionalSpiller) getExpectedModelType(ty schema.Type) model.Type {
|
|
|
|
switch ty {
|
|
|
|
case schema.NumberType:
|
|
|
|
return model.NumberType
|
|
|
|
case schema.BoolType:
|
|
|
|
return model.BoolType
|
|
|
|
case schema.IntType:
|
|
|
|
return model.IntType
|
|
|
|
case schema.StringType:
|
|
|
|
return model.StringType
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
2020-06-23 17:59:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *generator) rewriteOptionals(
|
|
|
|
x model.Expression,
|
|
|
|
spiller *optionalSpiller,
|
|
|
|
) (model.Expression, []*optionalTemp, hcl.Diagnostics) {
|
[codegen/go] Improve optional params in invoke
As described in #8821, docs generated for helper functions can be incorrect because optional arguments to parameter objects are not correctly handled.
Previously, code would be generated like so:
```go
policyDocument, err := iam.GetPolicyDocument(ctx, &iam.GetPolicyDocumentArgs{
Statements: []iam.GetPolicyDocumentStatement{
iam.GetPolicyDocumentStatement{
Sid: "1",
//...
```
However "Sid" is of type `*string`.
This four helper conversion functions, to handle the primitive types we lower from, and modifies codegen to recursively apply these functions inside of an invoke call.
In the new code generation, the above is instead rendered as:
```go
policyDocument, err := iam.GetPolicyDocument(ctx, &iam.GetPolicyDocumentArgs{
Statements: []iam.GetPolicyDocumentStatement{
iam.GetPolicyDocumentStatement{
Sid: pulumi.StringRef("1"),
//...
```
2022-02-01 03:18:15 +00:00
|
|
|
// We want to recurse but we only want to use the previsitor, if post visitor is nil we don't
|
|
|
|
// recurse.
|
|
|
|
x, diags := model.VisitExpression(x, spiller.preVisitor, spiller.postVisitor)
|
2020-06-23 17:59:26 +00:00
|
|
|
|
[codegen/go] Improve optional params in invoke
As described in #8821, docs generated for helper functions can be incorrect because optional arguments to parameter objects are not correctly handled.
Previously, code would be generated like so:
```go
policyDocument, err := iam.GetPolicyDocument(ctx, &iam.GetPolicyDocumentArgs{
Statements: []iam.GetPolicyDocumentStatement{
iam.GetPolicyDocumentStatement{
Sid: "1",
//...
```
However "Sid" is of type `*string`.
This four helper conversion functions, to handle the primitive types we lower from, and modifies codegen to recursively apply these functions inside of an invoke call.
In the new code generation, the above is instead rendered as:
```go
policyDocument, err := iam.GetPolicyDocument(ctx, &iam.GetPolicyDocumentArgs{
Statements: []iam.GetPolicyDocumentStatement{
iam.GetPolicyDocumentStatement{
Sid: pulumi.StringRef("1"),
//...
```
2022-02-01 03:18:15 +00:00
|
|
|
return x, nil, diags
|
2020-06-23 17:59:26 +00:00
|
|
|
}
|