Skip to content

Commit df85a80

Browse files
authored
feat(go-runtime): add helper functions for the new interface type (#4936)
### Description Some of our L1 resources will start to take array of interfaces(`*[]interface{}`) instead of (`*[]*string`) as props same applies for Numbers. This PR creating multiple helper functions that can help to make customer life easier. ### L1 Example [CfnUserProps](https://pkg.go.dev/github.com/aws/aws-cdk-go/awscdk/v2/awsiam#CfnUserProps) has now ``` ManagedPolicyArns *[]*[string](https://pkg.go.dev/builtin#string) `field:"optional" json:"managedPolicyArns" yaml:"managedPolicyArns"` ``` will change to ``` ManagedPolicyArns *[]interface{}`field:"optional" json:"managedPolicyArns" yaml:"managedPolicyArns"` ``` ### Helper Functions Usage Example #### Use Case 1 If you before was using `jsii.Strings()` or `jsii.Numbers()` ``` &CfnUserProps{ ManagedPolicyArns: jsii.Strings("arn1", "arn2"), } ``` Rename function to use `jsii.AnyStrings()` like this ``` &CfnUserProps{ ManagedPolicyArns: jsii.AnyStrings("arn1", "arn2"), } ``` #### Use Case 2 If you manually were creating the strings using without `jsii.Strings` syntax ``` func Arns() *[]*string { a := "arn:aws:s3:::bucket1" b := "arn:aws:s3:::bucket2" return &[]*string{&a, &b} } &CfnUserProps{ ManagedPolicyArns: Arns(), } ``` Wrap it with `jsii.AnySlice()` like this ``` &CfnUserProps{ ManagedPolicyArns: jsii.AnySlice(Arns()), } ``` --- 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
1 parent e91291d commit df85a80

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

packages/@jsii/go-runtime/jsii-runtime-go/helpers.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,35 @@ func Time(v time.Time) *time.Time { return Ptr(v) }
7272
func Times(v ...time.Time) *[]*time.Time {
7373
return PtrSlice(v...)
7474
}
75+
76+
// AnyStrings returns a pointer to a slice of any containing all the provided strings.
77+
func AnyStrings(v ...string) *[]interface{} {
78+
slice := make([]interface{}, len(v))
79+
for i := 0; i < len(v); i++ {
80+
slice[i] = v[i]
81+
}
82+
return &slice
83+
}
84+
85+
// AnyNumbers returns a pointer to a slice of any containing all the provided numbers.
86+
func AnyNumbers[T numberType](v ...T) *[]interface{} {
87+
slice := make([]interface{}, len(v))
88+
for i := 0; i < len(v); i++ {
89+
slice[i] = float64(v[i])
90+
}
91+
return &slice
92+
}
93+
94+
// AnySlice converts a pointer to a slice of pointers to a pointer to a slice of Any.
95+
func AnySlice[T any](v *[]*T) *[]interface{} {
96+
if v == nil {
97+
return nil
98+
}
99+
slice := make([]interface{}, len(*v))
100+
for i, ptr := range *v {
101+
if ptr != nil {
102+
slice[i] = *ptr
103+
}
104+
}
105+
return &slice
106+
}

packages/@jsii/go-runtime/jsii-runtime-go/helpers_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ func TestV(t *testing.T) {
2525
assert.Equal(t, now, *Time(now))
2626
// Times
2727
assert.Equal(t, []*time.Time{Time(now)}, *Times(now))
28+
// AnyStrings
29+
assert.Equal(t, []interface{}{"Hello", "World"}, *AnyStrings("Hello", "World"))
30+
// AnyNumbers
31+
assert.Equal(t, []interface{}{float64(42), float64(1337)}, *AnyNumbers(42, 1337))
32+
// AnySlice
33+
assert.Equal(t, []interface{}{"hello", "world"}, *AnySlice(Strings("hello", "world")))
2834
}
2935

3036
func TestBool(t *testing.T) {
@@ -81,3 +87,34 @@ func TestTimes(t *testing.T) {
8187
now := time.Now()
8288
assert.Equal(t, []*time.Time{Time(now)}, *Times(now))
8389
}
90+
91+
func TestAnyStrings(t *testing.T) {
92+
assert.Equal(t, []interface{}{"Hello", "World"}, *AnyStrings("Hello", "World"))
93+
}
94+
95+
func TestAnyNumbers(t *testing.T) {
96+
assert.Equal(t, []interface{}{float64(42), float64(1337)}, *AnyNumbers(42, 1337))
97+
}
98+
99+
func TestAnySlice(t *testing.T) {
100+
// Test with *[]*string
101+
strings := []*string{String("hello"), String("world")}
102+
result := AnySlice(&strings)
103+
assert.Equal(t, []interface{}{"hello", "world"}, *result)
104+
105+
// Test with strings
106+
result2 := AnySlice(Strings("hello", "world"))
107+
assert.Equal(t, []interface{}{"hello", "world"}, *result2)
108+
109+
// Test with *[]*float64
110+
floats := []*float64{Number(1.5), Number(2.5)}
111+
result3 := AnySlice(&floats)
112+
assert.Equal(t, []interface{}{1.5, 2.5}, *result3)
113+
114+
// Test with Numbers
115+
result4 := AnySlice(Numbers(1.5, 2.5))
116+
assert.Equal(t, []interface{}{1.5, 2.5}, *result4)
117+
118+
// Test with nil
119+
assert.Nil(t, AnySlice((*[]*string)(nil)))
120+
}

0 commit comments

Comments
 (0)