Skip to content

Commit

Permalink
Add helpers for arrays (#14437)
Browse files Browse the repository at this point in the history
* Add helpers for arrays

* refactor names
  • Loading branch information
jhendrixMSFT authored Mar 22, 2021
1 parent e3f5fda commit 9e0fae7
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 0 deletions.
63 changes: 63 additions & 0 deletions sdk/to/to.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
103 changes: 103 additions & 0 deletions sdk/to/to_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
package to

import (
"fmt"
"reflect"
"strconv"
"testing"
"time"
)
Expand Down Expand Up @@ -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")
}
}

0 comments on commit 9e0fae7

Please sign in to comment.