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

feat: enable Go generics for jsii Go CDK code #4009

Merged
merged 7 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 40 additions & 26 deletions packages/@jsii/go-runtime/jsii-runtime-go/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,64 @@ package jsii

import "time"

// Bool obtains a pointer to the provided bool.
func Bool(v bool) *bool { return &v }
type basicType interface {
bool | string | float64 | time.Time
}

// Bools obtains a pointer to a slice of pointers to all the provided booleans.
func Bools(v ...bool) *[]*bool {
slice := make([]*bool, len(v))
// Ptr returns a pointer to the provided value.
func Ptr[T basicType](v T) *T {
return &v
}

// PtrSlice returns a pointer to a slice of pointers to all of the provided values.
func PtrSlice[T basicType](v ...T) *[]*T {
slice := make([]*T, len(v))
for i := 0; i < len(v); i++ {
slice[i] = Bool(v[i])
slice[i] = Ptr(v[i])
}
return &slice
}

// Number obtains a pointer to the provided float64.
func Number(v float64) *float64 { return &v }
// Bool returns a pointer to the provided bool.
func Bool(v bool) *bool { return Ptr(v) }

// Bools returns a pointer to a slice of pointers to all of the provided booleans.
func Bools(v ...bool) *[]*bool {
return PtrSlice(v...)
}

type numberType interface {
~float32 | ~float64 |
~int | ~int8 | ~int16 | ~int32 | ~int64 |
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

// Numbers obtains a pointer to a slice of pointers to all the provided numbers.
func Numbers(v ...float64) *[]*float64 {
// Number returns a pointer to the provided float64.
func Number[T numberType](v T) *float64 {
return Ptr(float64(v))
}

// Numbers returns a pointer to a slice of pointers to all of the provided numbers.
func Numbers[T numberType](v ...T) *[]*float64 {
slice := make([]*float64, len(v))
for i := 0; i < len(v); i++ {
slice[i] = Number(v[i])
}
return &slice
}

// String obtains a pointer to the provided string.
func String(v string) *string { return &v }
// String returns a pointer to the provided string.
func String(v string) *string { return Ptr(v) }

// Strings obtains a pointer to a slice of pointers to all the provided strings.
// Strings returns a pointer to a slice of pointers to all of the provided strings.
func Strings(v ...string) *[]*string {
slice := make([]*string, len(v))
for i := 0; i < len(v); i++ {
slice[i] = String(v[i])
}
return &slice
return PtrSlice(v...)
}

// Time obtains a pointer to the provided time.Time.
func Time(v time.Time) *time.Time { return &v }
// Time returns a pointer to the provided time.Time.
func Time(v time.Time) *time.Time { return Ptr(v) }

// Times obtains a pointer to a slice of pointers to all the provided time.Time.
// Times returns a pointer to a slice of pointers to all of the provided time.Time values.
func Times(v ...time.Time) *[]*time.Time {
slice := make([]*time.Time, len(v))
for i := 0; i < len(v); i++ {
slice[i] = Time(v[i])
}
return &slice
return PtrSlice(v...)
}
35 changes: 35 additions & 0 deletions packages/@jsii/go-runtime/jsii-runtime-go/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,26 @@ import (
"github.com/stretchr/testify/assert"
)

func TestV(t *testing.T) {
// Bool
assert.Equal(t, true, *Ptr(true))
assert.Equal(t, false, *Ptr(false))
// Bools
assert.Equal(t, []*bool{Bool(true), Bool(false), Bool(false), Bool(true)}, *PtrSlice(true, false, false, true))
// Float64 is supported because it doesn't require conversion.
assert.Equal(t, 123.45, *Ptr(123.45))
assert.Equal(t, float64(123.45), *Ptr(float64(123.45)))
// String
assert.Equal(t, "Hello", *String("Hello"))
// Strings
assert.Equal(t, []*string{String("Hello"), String("World")}, *Strings("Hello", "World"))
// Time
now := time.Now()
assert.Equal(t, now, *Time(now))
// Times
assert.Equal(t, []*time.Time{Time(now)}, *Times(now))
}

func TestBool(t *testing.T) {
assert.Equal(t, true, *Bool(true))
assert.Equal(t, false, *Bool(false))
Expand All @@ -19,6 +39,21 @@ func TestBools(t *testing.T) {
func TestNumber(t *testing.T) {
assert.Equal(t, 123.45, *Number(123.45))
assert.Equal(t, 1337.0, *Number(1337))
// Floats.
assert.Equal(t, float64(float32(123.45)), *Number(float32(123.45)))
assert.Equal(t, float64(123.45), *Number(float64(123.45)))
// Ints.
assert.Equal(t, float64(1337), *Number(int(1337)))
// Signed.
assert.Equal(t, float64(127), *Number(int8(127)))
assert.Equal(t, float64(1337), *Number(int16(1337)))
assert.Equal(t, float64(1337), *Number(int32(1337)))
assert.Equal(t, float64(1337), *Number(int64(1337)))
// Unsigned.
assert.Equal(t, float64(127), *Number(uint8(127)))
assert.Equal(t, float64(1337), *Number(uint16(1337)))
assert.Equal(t, float64(1337), *Number(uint32(1337)))
assert.Equal(t, float64(1337), *Number(uint64(1337)))
}

func TestNumbers(t *testing.T) {
Expand Down