-
Notifications
You must be signed in to change notification settings - Fork 247
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
(go): downcasting support #2819
Comments
Hi, am I right that my issue #2798 is related to this? |
A quick chain of hacks :) I did not want to understand/look at func (c *Client) CastAndSetToPtr(ptr interface{}, data interface{}) {
ptrVal := reflect.ValueOf(ptr).Elem()
dataVal := reflect.ValueOf(data)
if data, ok := data.(map[string]interface{}); ok {
if ref, ok := data["$jsii.byref"]; ok { // uhm
typRef := strings.Split(ref.(string), "@")[0] // oof
typ, ok := c.Types().FindType(api.FQN(typRef))
if ok && typRef == "aws-cdk-lib.aws_ec2.CfnLaunchTemplate" {
fmt.Println(typ, typRef)
n := reflect.New(typ)
c.castAndSetToPtr(n.Elem(), dataVal)
fmt.Println(n)
ptrVal.Set(n.Elem())
return
}
}
}
c.castAndSetToPtr(ptrVal, dataVal)
} CDK use case which is now possible: launchTemplate := awsec2.NewLaunchTemplate(stack, aws.String("lt"), &awsec2.LaunchTemplateProps{})
fmt.Println("Calling .DefaultChild")
cfnlt := launchTemplate.Node().DefaultChild().(awsec2.CfnLaunchTemplate)
// https://docs.aws.amazon.com/eks/latest/userguide/best-practices-security.html
cfnlt.AddPropertyOverride(aws.String("LaunchTemplateData.MetadataOptions.HttpTokens"), aws.String("required")) |
I was able to workaround this way for now:
|
The `UnsafeCast` function can be used to forcefully convert from one type of jsii proxy value (including `interface{}`) to another jsii interface (i.e: a class or interface instance). If performs a "clean" cast when possible, and creates a new, aliased proxy otherwise. It is the user's responsibility to ensure they are passing the correct arguments into the function. Fixes #2819 --- By submitting this pull request, I confirm that my contribution is made under the terms of the [Apache 2.0 license]. [Apache 2.0 license]: https://www.apache.org/licenses/LICENSE-2.0
|
It's closed but it's still a bug, spent my afternoon trying to override a property, I had to use @krautbax (thanks) even if jsii.Get is deprecated. |
…Proxy (#3354) With this PR, the type that is instantiated as JSII Proxy is looked up from the type registry using the TypeFQDN in the ref. If that type is registered in the type registry AND that type is assignable to the targeted value type, than that type is used to instantiate the JSII proxy. Fixes #3353 and relates to #2819.
The `UnsafeCast` function can be used to forcefully convert from one type of jsii proxy value (including `interface{}`) to another jsii interface (i.e: a class or interface instance). If performs a "clean" cast when possible, and creates a new, aliased proxy otherwise. It is the user's responsibility to ensure they are passing the correct arguments into the function. Fixes aws/jsii#2819 --- By submitting this pull request, I confirm that my contribution is made under the terms of the [Apache 2.0 license]. [Apache 2.0 license]: https://www.apache.org/licenses/LICENSE-2.0
Go interface types cannot be downcasted to more specific types using normal go casting functionality. There may be some reflection able to achieve this but its not readily apparent if so and should be documented if it is indeed possible.
Example:
error:
There may be a changes to code generation that would allow the above to work but more investigation is needed.
The text was updated successfully, but these errors were encountered: