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 2 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
48 changes: 31 additions & 17 deletions packages/@jsii/go-runtime/jsii-runtime-go/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,45 @@ package jsii

import "time"

type Type interface {
a-h marked this conversation as resolved.
Show resolved Hide resolved
bool | string | float64 | time.Time
}

func V[T Type](v T) *T {
a-h marked this conversation as resolved.
Show resolved Hide resolved
return &v
}

func Slice[T Type](v ...T) *[]*T {
a-h marked this conversation as resolved.
Show resolved Hide resolved
slice := make([]*T, len(v))
for i := 0; i < len(v); i++ {
slice[i] = V(v[i])
}
return &slice
}

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

// Bools obtains a pointer to a slice of pointers to all the provided booleans.
func Bools(v ...bool) *[]*bool {
slice := make([]*bool, len(v))
for i := 0; i < len(v); i++ {
slice[i] = Bool(v[i])
}
return &slice
return Slice(v...)
}

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

// Number obtains a pointer to the provided float64.
func Number(v float64) *float64 { return &v }
func Number[T numberType](v T) *float64 {
var n float64
n = float64(v)
return &n
}

// Numbers obtains a pointer to a slice of pointers to all the provided numbers.
func Numbers(v ...float64) *[]*float64 {
func Numbers[T numberType](v ...T) *[]*float64 {
slice := make([]*float64, len(v))
for i := 0; i < len(v); i++ {
slice[i] = Number(v[i])
Expand All @@ -31,21 +53,13 @@ func String(v string) *string { return &v }

// Strings obtains a pointer to a slice of pointers to all 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 Slice(v...)
}

// Time obtains a pointer to the provided time.Time.
func Time(v time.Time) *time.Time { return &v }
a-h marked this conversation as resolved.
Show resolved Hide resolved

// Times obtains a pointer to a slice of pointers to all the provided time.Time.
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 Slice(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, *V(true))
assert.Equal(t, false, *V(false))
// Bools
assert.Equal(t, []*bool{Bool(true), Bool(false), Bool(false), Bool(true)}, *Slice(true, false, false, true))
// Float64 is supported because it doesn't require conversion.
assert.Equal(t, 123.45, *V(123.45))
assert.Equal(t, float64(123.45), *V(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