From 179810dce0d7bfc5ed2ddd362581a5873c2688cb Mon Sep 17 00:00:00 2001 From: Joel Hendrix Date: Thu, 18 Mar 2021 15:17:19 -0700 Subject: [PATCH 1/2] Add helpers for arrays --- sdk/to/to.go | 63 ++++++++++++++++++++++++++++ sdk/to/to_test.go | 103 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 166 insertions(+) diff --git a/sdk/to/to.go b/sdk/to/to.go index 2d3df8104bc8..c125795a7436 100644 --- a/sdk/to/to.go +++ b/sdk/to/to.go @@ -41,3 +41,66 @@ func StringPtr(s string) *string { func TimePtr(t time.Time) *time.Time { return &t } + +// ArrayOfInt32Ptr returns an array of *int32 from the specified values. +func ArrayOfInt32Ptr(vals ...int32) []*int32 { + arr := make([]*int32, len(vals), len(vals)) + for i := range vals { + arr[i] = Int32Ptr(vals[i]) + } + return arr +} + +// ArrayOfInt64Ptr returns an array of *int64 from the specified values. +func ArrayOfInt64Ptr(vals ...int64) []*int64 { + arr := make([]*int64, len(vals), len(vals)) + for i := range vals { + arr[i] = Int64Ptr(vals[i]) + } + return arr +} + +// ArrayOfFloat32Ptr returns an array of *float32 from the specified values. +func ArrayOfFloat32Ptr(vals ...float32) []*float32 { + arr := make([]*float32, len(vals), len(vals)) + for i := range vals { + arr[i] = Float32Ptr(vals[i]) + } + return arr +} + +// ArrayOfFloat64Ptr returns an array of *float64 from the specified values. +func ArrayOfFloat64Ptr(vals ...float64) []*float64 { + arr := make([]*float64, len(vals), len(vals)) + for i := range vals { + arr[i] = Float64Ptr(vals[i]) + } + return arr +} + +// ArrayOfBoolPtr returns an array of *bool from the specified values. +func ArrayOfBoolPtr(vals ...bool) []*bool { + arr := make([]*bool, len(vals), len(vals)) + for i := range vals { + arr[i] = BoolPtr(vals[i]) + } + return arr +} + +// ArrayOfStringPtr returns an array of *string from the specified values. +func ArrayOfStringPtr(vals ...string) []*string { + arr := make([]*string, len(vals), len(vals)) + for i := range vals { + arr[i] = StringPtr(vals[i]) + } + return arr +} + +// ArrayOfTimePtr returns an array of *time.Time from the specified values. +func ArrayOfTimePtr(vals ...time.Time) []*time.Time { + arr := make([]*time.Time, len(vals), len(vals)) + for i := range vals { + arr[i] = TimePtr(vals[i]) + } + return arr +} diff --git a/sdk/to/to_test.go b/sdk/to/to_test.go index efd37b8f6c7f..8500315970fe 100644 --- a/sdk/to/to_test.go +++ b/sdk/to/to_test.go @@ -6,6 +6,9 @@ package to import ( + "fmt" + "reflect" + "strconv" "testing" "time" ) @@ -86,3 +89,103 @@ func TestTimePtr(t *testing.T) { t.Fatalf("got %v, want %v", *pt, tt) } } + +func TestArrayOfInt32Ptr(t *testing.T) { + arr := ArrayOfInt32Ptr() + if len(arr) != 0 { + t.Fatal("expected zero length") + } + arr = ArrayOfInt32Ptr(1, 2, 3, 4, 5) + for i, v := range arr { + if *v != int32(i+1) { + t.Fatal("values don't match") + } + } +} + +func TestArrayOfInt64Ptr(t *testing.T) { + arr := ArrayOfInt64Ptr() + if len(arr) != 0 { + t.Fatal("expected zero length") + } + arr = ArrayOfInt64Ptr(1, 2, 3, 4, 5) + for i, v := range arr { + if *v != int64(i+1) { + t.Fatal("values don't match") + } + } +} + +func TestArrayOfFloat32Ptr(t *testing.T) { + arr := ArrayOfFloat32Ptr() + if len(arr) != 0 { + t.Fatal("expected zero length") + } + arr = ArrayOfFloat32Ptr(1.1, 2.2, 3.3, 4.4, 5.5) + for i, v := range arr { + f, err := strconv.ParseFloat(fmt.Sprintf("%d.%d", i+1, i+1), 32) + if err != nil { + t.Fatal(err) + } + if *v != float32(f) { + t.Fatal("values don't match") + } + } +} + +func TestArrayOfFloat64Ptr(t *testing.T) { + arr := ArrayOfFloat64Ptr() + if len(arr) != 0 { + t.Fatal("expected zero length") + } + arr = ArrayOfFloat64Ptr(1.1, 2.2, 3.3, 4.4, 5.5) + for i, v := range arr { + f, err := strconv.ParseFloat(fmt.Sprintf("%d.%d", i+1, i+1), 64) + if err != nil { + t.Fatal(err) + } + if *v != f { + t.Fatal("values don't match") + } + } +} + +func TestArrayOfBoolPtr(t *testing.T) { + arr := ArrayOfBoolPtr() + if len(arr) != 0 { + t.Fatal("expected zero length") + } + arr = ArrayOfBoolPtr(true, false, true) + curr := true + for _, v := range arr { + if *v != curr { + t.Fatal("values don'p match") + } + curr = !curr + } +} + +func TestArrayOfStringPtr(t *testing.T) { + arr := ArrayOfStringPtr() + if len(arr) != 0 { + t.Fatal("expected zero length") + } + arr = ArrayOfStringPtr("one", "", "three") + if !reflect.DeepEqual(arr, []*string{StringPtr("one"), StringPtr(""), StringPtr("three")}) { + t.Fatal("values don't match") + } +} + +func TestArrayOfTimePtr(t *testing.T) { + arr := ArrayOfTimePtr() + if len(arr) != 0 { + t.Fatal("expected zero length") + } + t1 := time.Now() + t2 := time.Time{} + t3 := t1.Add(24 * time.Hour) + arr = ArrayOfTimePtr(t1, t2, t3) + if !reflect.DeepEqual(arr, []*time.Time{&t1, &t2, &t3}) { + t.Fatal("values don't match") + } +} From 11e911d792858f440c1ac1c47d0059ed93fbb115 Mon Sep 17 00:00:00 2001 From: Joel Hendrix Date: Mon, 22 Mar 2021 11:43:18 -0700 Subject: [PATCH 2/2] refactor names --- sdk/to/to.go | 28 ++++++++++++++-------------- sdk/to/to_test.go | 42 +++++++++++++++++++++--------------------- 2 files changed, 35 insertions(+), 35 deletions(-) diff --git a/sdk/to/to.go b/sdk/to/to.go index c125795a7436..bbc5af802db2 100644 --- a/sdk/to/to.go +++ b/sdk/to/to.go @@ -42,8 +42,8 @@ func TimePtr(t time.Time) *time.Time { return &t } -// ArrayOfInt32Ptr returns an array of *int32 from the specified values. -func ArrayOfInt32Ptr(vals ...int32) []*int32 { +// Int32PtrArray returns an array of *int32 from the specified values. +func Int32PtrArray(vals ...int32) []*int32 { arr := make([]*int32, len(vals), len(vals)) for i := range vals { arr[i] = Int32Ptr(vals[i]) @@ -51,8 +51,8 @@ func ArrayOfInt32Ptr(vals ...int32) []*int32 { return arr } -// ArrayOfInt64Ptr returns an array of *int64 from the specified values. -func ArrayOfInt64Ptr(vals ...int64) []*int64 { +// Int64PtrArray returns an array of *int64 from the specified values. +func Int64PtrArray(vals ...int64) []*int64 { arr := make([]*int64, len(vals), len(vals)) for i := range vals { arr[i] = Int64Ptr(vals[i]) @@ -60,8 +60,8 @@ func ArrayOfInt64Ptr(vals ...int64) []*int64 { return arr } -// ArrayOfFloat32Ptr returns an array of *float32 from the specified values. -func ArrayOfFloat32Ptr(vals ...float32) []*float32 { +// Float32PtrArray returns an array of *float32 from the specified values. +func Float32PtrArray(vals ...float32) []*float32 { arr := make([]*float32, len(vals), len(vals)) for i := range vals { arr[i] = Float32Ptr(vals[i]) @@ -69,8 +69,8 @@ func ArrayOfFloat32Ptr(vals ...float32) []*float32 { return arr } -// ArrayOfFloat64Ptr returns an array of *float64 from the specified values. -func ArrayOfFloat64Ptr(vals ...float64) []*float64 { +// Float64PtrArray returns an array of *float64 from the specified values. +func Float64PtrArray(vals ...float64) []*float64 { arr := make([]*float64, len(vals), len(vals)) for i := range vals { arr[i] = Float64Ptr(vals[i]) @@ -78,8 +78,8 @@ func ArrayOfFloat64Ptr(vals ...float64) []*float64 { return arr } -// ArrayOfBoolPtr returns an array of *bool from the specified values. -func ArrayOfBoolPtr(vals ...bool) []*bool { +// BoolPtrArray returns an array of *bool from the specified values. +func BoolPtrArray(vals ...bool) []*bool { arr := make([]*bool, len(vals), len(vals)) for i := range vals { arr[i] = BoolPtr(vals[i]) @@ -87,8 +87,8 @@ func ArrayOfBoolPtr(vals ...bool) []*bool { return arr } -// ArrayOfStringPtr returns an array of *string from the specified values. -func ArrayOfStringPtr(vals ...string) []*string { +// StringPtrArray returns an array of *string from the specified values. +func StringPtrArray(vals ...string) []*string { arr := make([]*string, len(vals), len(vals)) for i := range vals { arr[i] = StringPtr(vals[i]) @@ -96,8 +96,8 @@ func ArrayOfStringPtr(vals ...string) []*string { return arr } -// ArrayOfTimePtr returns an array of *time.Time from the specified values. -func ArrayOfTimePtr(vals ...time.Time) []*time.Time { +// TimePtrArray returns an array of *time.Time from the specified values. +func TimePtrArray(vals ...time.Time) []*time.Time { arr := make([]*time.Time, len(vals), len(vals)) for i := range vals { arr[i] = TimePtr(vals[i]) diff --git a/sdk/to/to_test.go b/sdk/to/to_test.go index 8500315970fe..d568cd7f2ee4 100644 --- a/sdk/to/to_test.go +++ b/sdk/to/to_test.go @@ -90,12 +90,12 @@ func TestTimePtr(t *testing.T) { } } -func TestArrayOfInt32Ptr(t *testing.T) { - arr := ArrayOfInt32Ptr() +func TestInt32PtrArray(t *testing.T) { + arr := Int32PtrArray() if len(arr) != 0 { t.Fatal("expected zero length") } - arr = ArrayOfInt32Ptr(1, 2, 3, 4, 5) + arr = Int32PtrArray(1, 2, 3, 4, 5) for i, v := range arr { if *v != int32(i+1) { t.Fatal("values don't match") @@ -103,12 +103,12 @@ func TestArrayOfInt32Ptr(t *testing.T) { } } -func TestArrayOfInt64Ptr(t *testing.T) { - arr := ArrayOfInt64Ptr() +func TestInt64PtrArray(t *testing.T) { + arr := Int64PtrArray() if len(arr) != 0 { t.Fatal("expected zero length") } - arr = ArrayOfInt64Ptr(1, 2, 3, 4, 5) + arr = Int64PtrArray(1, 2, 3, 4, 5) for i, v := range arr { if *v != int64(i+1) { t.Fatal("values don't match") @@ -116,12 +116,12 @@ func TestArrayOfInt64Ptr(t *testing.T) { } } -func TestArrayOfFloat32Ptr(t *testing.T) { - arr := ArrayOfFloat32Ptr() +func TestFloat32PtrArray(t *testing.T) { + arr := Float32PtrArray() if len(arr) != 0 { t.Fatal("expected zero length") } - arr = ArrayOfFloat32Ptr(1.1, 2.2, 3.3, 4.4, 5.5) + arr = Float32PtrArray(1.1, 2.2, 3.3, 4.4, 5.5) for i, v := range arr { f, err := strconv.ParseFloat(fmt.Sprintf("%d.%d", i+1, i+1), 32) if err != nil { @@ -133,12 +133,12 @@ func TestArrayOfFloat32Ptr(t *testing.T) { } } -func TestArrayOfFloat64Ptr(t *testing.T) { - arr := ArrayOfFloat64Ptr() +func TestFloat64PtrArray(t *testing.T) { + arr := Float64PtrArray() if len(arr) != 0 { t.Fatal("expected zero length") } - arr = ArrayOfFloat64Ptr(1.1, 2.2, 3.3, 4.4, 5.5) + arr = Float64PtrArray(1.1, 2.2, 3.3, 4.4, 5.5) for i, v := range arr { f, err := strconv.ParseFloat(fmt.Sprintf("%d.%d", i+1, i+1), 64) if err != nil { @@ -150,12 +150,12 @@ func TestArrayOfFloat64Ptr(t *testing.T) { } } -func TestArrayOfBoolPtr(t *testing.T) { - arr := ArrayOfBoolPtr() +func TestBoolPtrArray(t *testing.T) { + arr := BoolPtrArray() if len(arr) != 0 { t.Fatal("expected zero length") } - arr = ArrayOfBoolPtr(true, false, true) + arr = BoolPtrArray(true, false, true) curr := true for _, v := range arr { if *v != curr { @@ -165,26 +165,26 @@ func TestArrayOfBoolPtr(t *testing.T) { } } -func TestArrayOfStringPtr(t *testing.T) { - arr := ArrayOfStringPtr() +func TestStringPtrArray(t *testing.T) { + arr := StringPtrArray() if len(arr) != 0 { t.Fatal("expected zero length") } - arr = ArrayOfStringPtr("one", "", "three") + arr = StringPtrArray("one", "", "three") if !reflect.DeepEqual(arr, []*string{StringPtr("one"), StringPtr(""), StringPtr("three")}) { t.Fatal("values don't match") } } -func TestArrayOfTimePtr(t *testing.T) { - arr := ArrayOfTimePtr() +func TestTimePtrArray(t *testing.T) { + arr := TimePtrArray() if len(arr) != 0 { t.Fatal("expected zero length") } t1 := time.Now() t2 := time.Time{} t3 := t1.Add(24 * time.Hour) - arr = ArrayOfTimePtr(t1, t2, t3) + arr = TimePtrArray(t1, t2, t3) if !reflect.DeepEqual(arr, []*time.Time{&t1, &t2, &t3}) { t.Fatal("values don't match") }