Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dotnet/program-gen] Fixes list initializer for plain lists in resource properties #13630

Merged
merged 1 commit into from Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,4 @@
changes:
- type: fix
scope: programgen/dotnet
description: Fixes list initializer for plain lists in resource properties
29 changes: 29 additions & 0 deletions pkg/codegen/dotnet/gen_program.go
Expand Up @@ -73,6 +73,15 @@ type generator struct {
// Program generation options
generateOptions GenerateProgramOptions
isComponent bool
// when creating a list of items, we need to know the type of the list
// if is it a plain list, then `new()` should be used because we are creating List<T>
// however if we have InputList<T> or anything else, we use `new[]` because InputList<T> can be implicitly casted
// from an array
listInitializer string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bit grim this goes on the generator state? Can't we work this out, and thread it down more locally?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bit grim this goes on the generator state?

Yes

Can't we work this out, and thread it down more locally

Not unless we rewrite ExpressionGenerator because it not possible to know at the point of generating an expression (i.e. GenTupleConsExpxression for arrays) what context information about that expression is available 😞

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eh fine

}

func (g *generator) resetListInitializer() {
g.listInitializer = "new[]"
}

const (
Expand Down Expand Up @@ -126,6 +135,7 @@ func GenerateProgramWithOptions(
functionArgs: functionArgs,
functionInvokes: map[string]*schema.Function{},
generateOptions: options,
listInitializer: "new[]",
}

g.Formatter = format.NewFormatter(g)
Expand Down Expand Up @@ -164,6 +174,7 @@ func GenerateProgramWithOptions(
functionInvokes: map[string]*schema.Function{},
generateOptions: options,
isComponent: true,
listInitializer: "new[]",
}

componentGenerator.Formatter = format.NewFormatter(componentGenerator)
Expand Down Expand Up @@ -1205,6 +1216,19 @@ func AnnotateComponentInputs(component *pcl.Component) {
}
}

func isPlainResourceProperty(r *pcl.Resource, name string) bool {
if r.Schema == nil {
return false
}

for _, property := range r.Schema.InputProperties {
if property.Name == name {
return property.Plain
}
}
return false
}

// genResource handles the generation of instantiations of non-builtin resources.
func (g *generator) genResource(w io.Writer, r *pcl.Resource) {
qualifiedMemberName := g.resourceTypeName(r)
Expand Down Expand Up @@ -1247,7 +1271,12 @@ func (g *generator) genResource(w io.Writer, r *pcl.Resource) {
g.Indented(func() {
for _, attr := range r.Inputs {
g.Fgenf(w, "%s%s =", g.Indent, propertyName(attr.Name))
if isPlainResourceProperty(r, attr.Name) {
g.listInitializer = "new()"
}

g.Fgenf(w, " %.v,\n", attr.Value)
g.resetListInitializer()
}
})
g.Fgenf(w, "%s}%s)", g.Indent, g.genResourceOptions(r.Options, "CustomResourceOptions"))
Expand Down
10 changes: 5 additions & 5 deletions pkg/codegen/dotnet/gen_program_expressions.go
Expand Up @@ -1032,14 +1032,14 @@ func (g *generator) isListOfDifferentTypes(expr *model.TupleConsExpression) bool
func (g *generator) GenTupleConsExpression(w io.Writer, expr *model.TupleConsExpression) {
switch len(expr.Expressions) {
case 0:
g.Fgen(w, "new[] {}")
g.Fgenf(w, "%s {}", g.listInitializer)
default:
if !g.isListOfDifferentTypes(expr) {
// only generate this when we don't have a list of union types
// list of a union is mapped to InputList<object>
// only generate a list initializer when we don't have a list of union types
// because list of a union is mapped to InputList<object>
// which means new[] will not work because type-inference won't
// know the type of the array before hand
g.Fgen(w, "new[]")
// know the type of the array beforehand
g.Fgenf(w, "%s", g.listInitializer)
}

g.Fgenf(w, "\n%s{", g.Indent)
Expand Down
5 changes: 5 additions & 0 deletions pkg/codegen/testing/test/program_driver.go
Expand Up @@ -361,6 +361,11 @@ var PulumiPulumiProgramTests = []ProgramTest{
Description: "Tests that interpolated string keys are supported in maps. ",
Skip: allProgLanguages.Except("nodejs").Except("python"),
},
{
Directory: "csharp-plain-lists",
Description: "Tests that plain lists are supported in C#",
Skip: allProgLanguages.Except("dotnet"),
},
{
Directory: "csharp-typed-for-expressions",
Description: "Testing for expressions with typed target expressions in csharp",
Expand Down
@@ -0,0 +1,6 @@
resource vpc "awsx:ec2:Vpc" {
subnetSpecs = [
{ type = "Public", cidrMask = 22 },
{ type = "Private", cidrMask = 20 }
]
}
@@ -0,0 +1,26 @@
using System.Collections.Generic;
using System.Linq;
using Pulumi;
using Awsx = Pulumi.Awsx;

return await Deployment.RunAsync(() =>
{
var vpc = new Awsx.Ec2.Vpc("vpc", new()
{
SubnetSpecs = new()
{
new Awsx.Ec2.Inputs.SubnetSpecArgs
{
Type = Awsx.Ec2.SubnetType.Public,
CidrMask = 22,
},
new Awsx.Ec2.Inputs.SubnetSpecArgs
{
Type = Awsx.Ec2.SubnetType.Private,
CidrMask = 20,
},
},
});

});