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

Conversation

Zaid-Ajaj
Copy link
Contributor

Description

Some resources, especially custom resources like those in awsx have properties which are plain lists. This means that the generated dotnet SDK for them uses List<T> rather than InputList<T>. In case of the former, when initializing the list, we cannot use new[] (this works for InputList<T> because of an implicit conversion from arrays). Instead we have to use new() which initializes an instance of the List<T> class.

This PR implements a fix such that the code generator knows when the current resource property is plain or not and subsequently emitting new() instead of new[]

Fixes pulumi/pulumi-dotnet#21

Checklist

  • I have run make tidy to update any new dependencies
  • I have run make lint to verify my code passes the lint check
    • I have formatted my code using gofumpt
  • I have added tests that prove my fix is effective or that my feature works
  • I have run make changelog and committed the changelog/pending/<file> documenting my change
  • Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version

@Zaid-Ajaj Zaid-Ajaj added language/dotnet area/codegen SDK-gen, program-gen, convert labels Jul 31, 2023
@Zaid-Ajaj Zaid-Ajaj requested a review from a team July 31, 2023 20:39
@pulumi-bot
Copy link
Contributor

pulumi-bot commented Jul 31, 2023

Changelog

[uncommitted] (2023-08-07)

Bug Fixes

  • [programgen/dotnet] Fixes list initializer for plain lists in resource properties
    #13630

// 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

@Zaid-Ajaj Zaid-Ajaj force-pushed the zaid/fix-plain-list-initializer-csharp branch from 8f6de1d to d66a037 Compare August 7, 2023 16:43
@Zaid-Ajaj
Copy link
Contributor Author

bors merge

bors bot added a commit that referenced this pull request Aug 7, 2023
13630: [dotnet/program-gen] Fixes list initializer for plain lists in resource properties r=Zaid-Ajaj a=Zaid-Ajaj

# Description

Some resources, especially custom resources like those in `awsx` have properties which are _plain_ lists. This means that the generated dotnet SDK for them uses `List<T>` rather than `InputList<T>`. In case of the former, when initializing the list, we cannot use `new[]` (this works for `InputList<T>` because of an implicit conversion from arrays). Instead we have to use `new()` which initializes an instance of the `List<T>` class. 

This PR implements a fix such that the code generator knows when the current resource property is plain or not and subsequently emitting `new()` instead of `new[]`

Fixes pulumi/pulumi-dotnet#21

## Checklist

- [ ] I have run `make tidy` to update any new dependencies
- [x] I have run `make lint` to verify my code passes the lint check
  - [ ] I have formatted my code using `gofumpt`

<!--- Please provide details if the checkbox below is to be left unchecked. -->
- [x] I have added tests that prove my fix is effective or that my feature works
<!--- 
User-facing changes require a CHANGELOG entry.
-->
- [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change
<!--
If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud,
then the service should honor older versions of the CLI where this change would not exist.
You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add
it to the service.
-->
- [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version
  <!-- `@Pulumi` employees: If yes, you must submit corresponding changes in the service repo. -->


Co-authored-by: Zaid Ajaj <zaid.naom@gmail.com>
@bors
Copy link
Contributor

bors bot commented Aug 7, 2023

Build failed:

@Zaid-Ajaj
Copy link
Contributor Author

bors merge

@bors
Copy link
Contributor

bors bot commented Aug 7, 2023

Build succeeded!

The publicly hosted instance of bors-ng is deprecated and will go away soon.

If you want to self-host your own instance, instructions are here.
For more help, visit the forum.

If you want to switch to GitHub's built-in merge queue, visit their help page.

@bors bors bot merged commit 2c7fb15 into master Aug 7, 2023
56 checks passed
@bors bors bot deleted the zaid/fix-plain-list-initializer-csharp branch August 7, 2023 18:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/codegen SDK-gen, program-gen, convert language/dotnet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

List property initializer not valid
3 participants