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

Add helpers for arrays #14437

Merged
merged 2 commits into from
Mar 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
}

// ArrayOfInt32Ptr returns an array of *int32 from the specified values.
jhendrixMSFT marked this conversation as resolved.
Show resolved Hide resolved
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
}
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 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")
}
}