Skip to content

Commit

Permalink
Merge #11480
Browse files Browse the repository at this point in the history
11480: Convert invoke results to ouputs when needed r=iwahbe a=iwahbe

Convert plain array values to input values when passing them into an array. 

Fixes #8440

Co-authored-by: Ian Wahbe <ian@wahbe.com>
  • Loading branch information
bors[bot] and iwahbe committed Nov 29, 2022
2 parents c0b521d + 095f5a9 commit 9086bf9
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 16 deletions.
@@ -0,0 +1,4 @@
changes:
- type: fix
scope: programgen/go
description: Convert the result of immediate invokes to ouputs when necessary.
4 changes: 1 addition & 3 deletions pkg/codegen/go/gen_program.go
Expand Up @@ -664,9 +664,7 @@ func (g *generator) genResource(w io.Writer, r *pcl.Resource) {
if len(r.Inputs) > 0 {
g.Fgenf(w, "&%s.%sArgs{\n", modOrAlias, typ)
for _, attr := range r.Inputs {
g.Fgenf(w, "%s: ", strings.Title(attr.Name))
g.Fgenf(w, "%.v,\n", attr.Value)

g.Fgenf(w, "%s: %.v,\n", strings.Title(attr.Name), attr.Value)
}
g.Fprint(w, "}")
} else {
Expand Down
28 changes: 20 additions & 8 deletions pkg/codegen/go/gen_program_expressions.go
Expand Up @@ -579,39 +579,51 @@ func (g *generator) genScopeTraversalExpression(
_, isInput = schemaType.(*schema.InputType)
}

if resource, ok := expr.Parts[0].(*pcl.Resource); ok {
var sourceIsPlain bool
switch root := expr.Parts[0].(type) {
case *pcl.Resource:
isInput = false
if _, ok := pcl.GetSchemaForType(resource.InputType); ok {
if _, ok := pcl.GetSchemaForType(root.InputType); ok {
// convert .id into .ID()
last := expr.Traversal[len(expr.Traversal)-1]
if attr, ok := last.(hcl.TraverseAttr); ok && attr.Name == "id" {
genIDCall = true
expr.Traversal = expr.Traversal[:len(expr.Traversal)-1]
}
}
case *pcl.LocalVariable:
if root, ok := root.Definition.Value.(*model.FunctionCallExpression); ok && !pcl.IsOutputVersionInvokeCall(root) {
sourceIsPlain = true
}
}

// TODO if it's an array type, we need a lowering step to turn []string -> pulumi.StringArray
if isInput {
argType := g.argumentTypeName(expr, expr.Type(), isInput)
if strings.HasSuffix(argType, "Array") {
argTypeName := g.argumentTypeName(expr, expr.Type(), isInput)
if strings.HasSuffix(argTypeName, "Array") {
destTypeName := g.argumentTypeName(expr, destType, isInput)
if argType != destTypeName {
// `argTypeName` == `destTypeName` and `argTypeName` ends with `Array`, we
// know that `destType` is an outputty type. If the source is plain (and thus
// not outputty), then the types can never line up and we will need a
// conversion helper method.
if argTypeName != destTypeName || sourceIsPlain {
// use a helper to transform prompt arrays into inputty arrays
var helper *promptToInputArrayHelper
if h, ok := g.arrayHelpers[argType]; ok {
if h, ok := g.arrayHelpers[argTypeName]; ok {
helper = h
} else {
// helpers are emitted at the end in the postamble step
helper = &promptToInputArrayHelper{
destType: argType,
destType: argTypeName,
}
g.arrayHelpers[argType] = helper
g.arrayHelpers[argTypeName] = helper
}
// Wrap the emitted expression in a call to the generated helper function.
g.Fgenf(w, "%s(", helper.getFnName())
defer g.Fgenf(w, ")")
}
} else {
// Wrap the emitted expression in a type conversion.
g.Fgenf(w, "%s(", g.argumentTypeName(expr, expr.Type(), isInput))
defer g.Fgenf(w, ")")
}
Expand Down
3 changes: 0 additions & 3 deletions pkg/codegen/testing/test/program_driver.go
Expand Up @@ -80,9 +80,6 @@ var PulumiPulumiProgramTests = []ProgramTest{
{
Directory: "aws-fargate",
Description: "AWS Fargate",

// TODO[pulumi/pulumi#8440]
SkipCompile: codegen.NewStringSet("go"),
},
{
Directory: "aws-s3-logging",
Expand Down
Expand Up @@ -85,7 +85,7 @@ func main() {
return err
}
webLoadBalancer, err := elasticloadbalancingv2.NewLoadBalancer(ctx, "webLoadBalancer", &elasticloadbalancingv2.LoadBalancerArgs{
Subnets: subnets.Ids,
Subnets: toPulumiStringArray(subnets.Ids),
SecurityGroups: pulumi.StringArray{
webSecurityGroup.ID(),
},
Expand Down Expand Up @@ -153,7 +153,7 @@ func main() {
TaskDefinition: appTask.Arn,
NetworkConfiguration: &ecs.ServiceNetworkConfigurationArgs{
AssignPublicIp: pulumi.Bool(true),
Subnets: subnets.Ids,
Subnets: toPulumiStringArray(subnets.Ids),
SecurityGroups: pulumi.StringArray{
webSecurityGroup.ID(),
},
Expand All @@ -175,3 +175,10 @@ func main() {
return nil
})
}
func toPulumiStringArray(arr []string) pulumi.StringArray {
var pulumiArr pulumi.StringArray
for _, v := range arr {
pulumiArr = append(pulumiArr, pulumi.String(v))
}
return pulumiArr
}

0 comments on commit 9086bf9

Please sign in to comment.