Describe the issue
The code examples in the official CDK Go documentation fail to compile when copied directly.
This occurs with optional array fields, where there is a type mismatch in the generated examples.
Details:
- When a TypeScript property is defined as an optional array (e.g.,
removeHeaders?: string[]), jsii correctly generates the Go type as
*[]*string (pointer to a slice)
- However, the documentation examples show code that assigns
[]*string (a slice) directly to this field, which causes a type mismatch and compilation failure
This affects multiple packages across CDK, including:
@aws-cdk/aws-cloudfront - ResponseHeadersPolicyProps.RemoveHeaders
@aws-cdk/aws-certificatemanager - CertificateProps.SubjectAlternativeNames
Example from CloudFront documentation
TypeScript source:
/**
* A list of HTTP response headers that CloudFront removes from HTTP responses
* that it sends to viewers.
*
* @default - no headers are removed
*/
readonly removeHeaders?: string[];
Generated Go type:
RemoveHeaders *[]*string `field:"optional" json:"removeHeaders" yaml:"removeHeaders"`
Go Example Code:
// RemoveHeaders: []*string{
// jsii.String("Server"),
// },
Compilation error:
cannot use []*string{...} (value of type []*string) as type *[]*string in struct literal
Working code (not shown in documentation):
// Option 1: Take address of slice literal
myResponseHeadersPolicy := cloudfront.NewResponseHeadersPolicy(this, jsii.String("ResponseHeadersPolicy"), &ResponseHeadersPolicyProps{
RemoveHeaders: &[]*string{jsii.String("Server")},
})
// Option 2: Use intermediate variable
headers := []*string{jsii.String("Server")}
myResponseHeadersPolicy := cloudfront.NewResponseHeadersPolicy(this, jsii.String("ResponseHeadersPolicy"), &ResponseHeadersPolicyProps{
RemoveHeaders: &headers,
})
This appears to be a Rosetta (TypeScript-to-Go translation tool) issue. The generated examples don't account for the fact that *[]*string requires a pointer to a slice, not a slice directly.
Links
Describe the issue
The code examples in the official CDK Go documentation fail to compile when copied directly.
This occurs with optional array fields, where there is a type mismatch in the generated examples.
Details:
removeHeaders?: string[]), jsii correctly generates the Go type as*[]*string(pointer to a slice)[]*string(a slice) directly to this field, which causes a type mismatch and compilation failureThis affects multiple packages across CDK, including:
@aws-cdk/aws-cloudfront-ResponseHeadersPolicyProps.RemoveHeaders@aws-cdk/aws-certificatemanager-CertificateProps.SubjectAlternativeNamesExample from CloudFront documentation
TypeScript source:
Generated Go type:
Go Example Code:
Compilation error:
Working code (not shown in documentation):
This appears to be a Rosetta (TypeScript-to-Go translation tool) issue. The generated examples don't account for the fact that
*[]*stringrequires a pointer to a slice, not a slice directly.Links