-
Notifications
You must be signed in to change notification settings - Fork 245
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
Support variadic parameters for .NET #153
Comments
I guess i'm running into a related issue to this now in 0.29 of the C# CDK AddActions of the PolicyStatement has the following signature But the generated C# method has the following signature, with a string Is there anyway i can create a correct string of a string params, or is this just something that need to be solved in this issue? |
This has been an issue for a while. The way I deal with this situation is to create an extension method to get the behavior I want. For my purposes I wanted to add multiple actions via a comma delimited set of actions as a single string, but you could easily go with an IEnumerable<string> instead. The extension method I use is included below. It would be better to teach JSII how to deal with variable number of arguments in C#. Unfortunately I’m slammed on other projects at the moment, or I’d look at contributing something.
…--Bonnie
/// <summary>
/// Adds actions to a policy statement from a comma separated list of actions names as part of initializing IAM objects.
/// </summary>
/// <param name="statement">What policy statement</param>
/// <param name="actions">The names of the actions to add</param>
/// <returns></returns>
public static PolicyStatement AddActionsFromString(this PolicyStatement statement, string actions)
{
var policyStatement = statement;
foreach (string action in actions.Split(", "))
{
policyStatement = policyStatement.AddAction(action);
}
return policyStatement;
}
From: Erik Karlsson <notifications@github.com>
Sent: Thursday, April 25, 2019 6:15 AM
To: awslabs/jsii <jsii@noreply.github.com>
Cc: Feinberg, Bonnie <feinbb@amazon.com>; Manual <manual@noreply.github.com>
Subject: Re: [awslabs/jsii] Support variadic parameters for .NET (#153)
I guess i'm running into a related issue to this now in 0.29 of the C# CDK
AddActions of the PolicyStatement has the following signature
[JsiiMethod("addActions", "{\"type\":{\"fqn\":\"@aws-cdk/aws-iam.PolicyStatement\"}}", "[{\"name\":\"actions\",\"variadic\":true,\"type\":{\"primitive\":\"string\"}}]", false, false)]
But the generated C# method has the following signature, with a string
public virtual PolicyStatement AddActions(string actions)
Is there anyway i can create a correct string of a string params, or is this just something that need to be solved in this issue?
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub<#153 (comment)>, or mute the thread<https://github.com/notifications/unsubscribe-auth/AKCL62L6TAIWZTQYUNEVUXTPSGVGDANCNFSM4FOENHGQ>.
|
Thank you @bfeinb I tried something similar with a static method and looping over several actions, but then i get the following error in the
Only reason i need it is beacuase of a related issue in the new 0.29, aws/aws-cdk#2375 |
Handling optional and variadic parameters in .NET Emits the =null or params[] keywords when required in constructors or methods. Ran pack.sh in the CDK with this change, and the S3 construct now looks better: **Optionals:** `public Bucket(Amazon.CDK.Construct scope, string id, Amazon.CDK.AWS.S3.IBucketProps props = null): base(new DeputyProps(new object[]{scope, id, props})) { } ` Making the C# call look like: ` var bucket = new Bucket(this, "bucketName");` Rather than ` var bucket = new Bucket(this, "bucketName", null);` **Variadic:** Tested with null values, empty array, one value array, multiple values array. ``` // Array with no value in constructor params var variadicClassNoParams = new VariadicMethod(); // Array with null value in constructor params var variadicClassNullParams = new VariadicMethod(null); // Array with one value in constructor params var variadicClassOneParam = new VariadicMethod(1); // Array with multiple values in constructor params var variadicClassMultipleParams = new VariadicMethod(1, 2, 3, 4); ``` Fixes #153 Fixes #210
This might be more involved that just adding the "params" keyword. Consider the following signature:
But in this case, there's no ambiguity:
This might not matter, since generated code does not include any invocations of the generated methods. Just something to keep in mind.
The text was updated successfully, but these errors were encountered: