Skip to content

Commit

Permalink
chore(release): v1.27.0
Browse files Browse the repository at this point in the history
  • Loading branch information
AWS CDK Team committed Apr 6, 2021
1 parent ec37187 commit 8c33a80
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 15 deletions.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/aws/jsii-runtime-go

go 1.16

require github.com/Masterminds/semver/v3 v3.1.1
require (
github.com/Masterminds/semver/v3 v3.1.1
github.com/stretchr/testify v1.7.0
)
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030IGemrRc=
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
4 changes: 4 additions & 0 deletions internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ type EnumRef struct {
MemberFQN string `json:"$jsii.enum"`
}

type WireDate struct {
Timestamp string `json:"$jsii.date"`
}

type WireMap struct {
MapData map[string]interface{} `json:"$jsii.map"`
}
Expand Down
2 changes: 1 addition & 1 deletion internal/embedded/resources/lib/program.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 42 additions & 8 deletions internal/kernel/conversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ package kernel

import (
"reflect"
"time"

"github.com/aws/jsii-runtime-go/internal/api"
)

var (
anyType = reflect.TypeOf((*interface{})(nil)).Elem()
)

// CastAndSetToPtr accepts a pointer to any type and attempts to cast the value
// argument to be the same type. Then it sets the value of the pointer element
// to be the newly cast data. This is used to cast payloads from JSII to
Expand Down Expand Up @@ -97,15 +102,21 @@ func (c *Client) castAndSetToPtr(ptr reflect.Value, data reflect.Value) {
}

// arrays
if ptr.Kind() == reflect.Slice && data.Kind() == reflect.Slice {
if data.Kind() == reflect.Slice {
len := data.Len()
ptr.Set(reflect.MakeSlice(ptr.Type(), len, len))
var slice reflect.Value
if ptr.Kind() == reflect.Slice {
slice = reflect.MakeSlice(ptr.Type(), len, len)
} else {
slice = reflect.MakeSlice(reflect.SliceOf(anyType), len, len)
}

// If return type is a slice, recursively cast elements
for i := 0; i < len; i++ {
c.castAndSetToPtr(ptr.Index(i), data.Index(i))
c.castAndSetToPtr(slice.Index(i), data.Index(i))
}

ptr.Set(slice)
return
}

Expand All @@ -126,6 +137,11 @@ func (c *Client) CastPtrToRef(dataVal reflect.Value) interface{} {
return nil
}

// In case we got a time.Time value (or pointer to one).
if wireDate, isDate := castPtrToDate(dataVal); isDate {
return wireDate
}

switch dataVal.Kind() {
case reflect.Map:
result := api.WireMap{MapData: make(map[string]interface{})}
Expand Down Expand Up @@ -180,7 +196,7 @@ func (c *Client) CastPtrToRef(dataVal reflect.Value) interface{} {
case reflect.Slice:
refs := make([]interface{}, dataVal.Len())
for i := 0; i < dataVal.Len(); i++ {
refs[i] = dataVal.Index(i).Interface()
refs[i] = c.CastPtrToRef(dataVal.Index(i))
}
return refs

Expand All @@ -192,6 +208,24 @@ func (c *Client) CastPtrToRef(dataVal reflect.Value) interface{} {
return dataVal.Interface()
}

// castPtrToDate obtains an api.WireDate from the provided reflect.Value if it
// represents a time.Time or *time.Time value. It accepts both a pointer and
// direct value as a convenience (when passing time.Time through an interface{}
// parameter, having to unwrap it as a pointer is annoying and unneeded).
func castPtrToDate(data reflect.Value) (wireDate api.WireDate, ok bool) {
var timestamp *time.Time
if timestamp, ok = data.Interface().(*time.Time); !ok {
var val time.Time
if val, ok = data.Interface().(time.Time); ok {
timestamp = &val
}
}
if ok {
wireDate.Timestamp = timestamp.Format(time.RFC3339Nano)
}
return
}

func castValToRef(data reflect.Value) (ref api.ObjectRef, ok bool) {
if data.Kind() == reflect.Map {
for _, k := range data.MapKeys() {
Expand Down Expand Up @@ -234,13 +268,14 @@ func castValToRef(data reflect.Value) (ref api.ObjectRef, ok bool) {
}

// TODO: This should return a time.Time instead
func castValToDate(data reflect.Value) (date string, ok bool) {
func castValToDate(data reflect.Value) (date time.Time, ok bool) {
if data.Kind() == reflect.Map {
for _, k := range data.MapKeys() {
v := reflect.ValueOf(data.MapIndex(k).Interface())
if k.Kind() == reflect.String && k.String() == "$jsii.date" && v.Kind() == reflect.String {
date = v.String()
ok = true
var err error
date, err = time.Parse(time.RFC3339Nano, v.String())
ok = (err == nil)
break
}
}
Expand Down Expand Up @@ -282,7 +317,6 @@ func (c *Client) castValToMap(data reflect.Value, mapType reflect.Type) (m refle
if mapType.Kind() == reflect.Map && mapType.Key().Kind() != reflect.String {
return
}
anyType := reflect.TypeOf((*interface{})(nil)).Elem()
if mapType == anyType {
mapType = reflect.TypeOf((map[string]interface{})(nil))
}
Expand Down
2 changes: 1 addition & 1 deletion internal/kernel/version.generated.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package kernel

const version = "1.26.0"
const version = "1.27.0"
50 changes: 47 additions & 3 deletions runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"reflect"
"strings"
"time"

"github.com/aws/jsii-runtime-go/internal/api"
"github.com/aws/jsii-runtime-go/internal/kernel"
Expand Down Expand Up @@ -371,7 +372,50 @@ func Close() {
kernel.CloseClient()
}

// Helpers to store primitives and return pointers to them
func Bool(v bool) *bool { return &v }
// 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
}

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

// Numbers obtains a pointer to a slice of pointers to all the provided numbers.
func Numbers(v ...float64) *[]*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 }

// 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
}

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

// 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
}
44 changes: 44 additions & 0 deletions runtime_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package jsii

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestBool(t *testing.T) {
assert.Equal(t, true, *Bool(true))
assert.Equal(t, false, *Bool(false))
}

func TestBools(t *testing.T) {
assert.Equal(t, []*bool{Bool(true), Bool(false), Bool(false), Bool(true)}, *Bools(true, false, false, true))
}

func TestNumber(t *testing.T) {
assert.Equal(t, 123.45, *Number(123.45))
assert.Equal(t, 1337.0, *Number(1337))
}

func TestNumbers(t *testing.T) {
assert.Equal(t, []*float64{Number(42), Number(1337)}, *Numbers(42, 1337))
}

func TestString(t *testing.T) {
assert.Equal(t, "Hello", *String("Hello"))
}

func TestStrings(t *testing.T) {
assert.Equal(t, []*string{String("Hello"), String("World")}, *Strings("Hello", "World"))
}

func TestTime(t *testing.T) {
now := time.Now()
assert.Equal(t, now, *Time(now))
}

func TestTimes(t *testing.T) {
now := time.Now()
assert.Equal(t, []*time.Time{Time(now)}, *Times(now))
}
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.26.0
1.27.0

0 comments on commit 8c33a80

Please sign in to comment.