From 9e0fae7cd8f8521604c142317665e58b94a189b2 Mon Sep 17 00:00:00 2001 From: Joel Hendrix Date: Mon, 22 Mar 2021 12:05:55 -0700 Subject: [PATCH] Add helpers for arrays (#14437) * Add helpers for arrays * refactor names --- 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..bbc5af802db2 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 } + +// 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]) + } + return arr +} + +// 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]) + } + return arr +} + +// 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]) + } + return arr +} + +// 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]) + } + return arr +} + +// 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]) + } + return arr +} + +// 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]) + } + return arr +} + +// 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]) + } + return arr +} diff --git a/sdk/to/to_test.go b/sdk/to/to_test.go index efd37b8f6c7f..d568cd7f2ee4 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 TestInt32PtrArray(t *testing.T) { + arr := Int32PtrArray() + if len(arr) != 0 { + t.Fatal("expected zero length") + } + arr = Int32PtrArray(1, 2, 3, 4, 5) + for i, v := range arr { + if *v != int32(i+1) { + t.Fatal("values don't match") + } + } +} + +func TestInt64PtrArray(t *testing.T) { + arr := Int64PtrArray() + if len(arr) != 0 { + t.Fatal("expected zero length") + } + arr = Int64PtrArray(1, 2, 3, 4, 5) + for i, v := range arr { + if *v != int64(i+1) { + t.Fatal("values don't match") + } + } +} + +func TestFloat32PtrArray(t *testing.T) { + arr := Float32PtrArray() + if len(arr) != 0 { + t.Fatal("expected zero length") + } + 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 { + t.Fatal(err) + } + if *v != float32(f) { + t.Fatal("values don't match") + } + } +} + +func TestFloat64PtrArray(t *testing.T) { + arr := Float64PtrArray() + if len(arr) != 0 { + t.Fatal("expected zero length") + } + 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 { + t.Fatal(err) + } + if *v != f { + t.Fatal("values don't match") + } + } +} + +func TestBoolPtrArray(t *testing.T) { + arr := BoolPtrArray() + if len(arr) != 0 { + t.Fatal("expected zero length") + } + arr = BoolPtrArray(true, false, true) + curr := true + for _, v := range arr { + if *v != curr { + t.Fatal("values don'p match") + } + curr = !curr + } +} + +func TestStringPtrArray(t *testing.T) { + arr := StringPtrArray() + if len(arr) != 0 { + t.Fatal("expected zero length") + } + arr = StringPtrArray("one", "", "three") + if !reflect.DeepEqual(arr, []*string{StringPtr("one"), StringPtr(""), StringPtr("three")}) { + t.Fatal("values don't match") + } +} + +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 = TimePtrArray(t1, t2, t3) + if !reflect.DeepEqual(arr, []*time.Time{&t1, &t2, &t3}) { + t.Fatal("values don't match") + } +}