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

Feat/add utils #18

Merged
merged 4 commits into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
51 changes: 51 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,54 @@ package utils
func Bool(val bool) *bool {
return &val
}

// CvtToAnysWithOW converts the given number of values to a slice of pointers of given type with the given one overwrite.
func CvtToAnysWithOW[T any](i int, ow *T) []interface{} {
res := make([]interface{}, i)
for k := 0; k < i; k++ {
if ow != nil {
res[k] = ow
continue
}

var v T
res[k] = &v
}

return res
}

// CvtToAnysWithOWs converts the given number of values to a slice of pointers to the given type with the given multiple overwrites.
func CvtToAnysWithOWs[T any](i int, ows ...*T) []interface{} {
res := make([]interface{}, i)
for k := 0; k < i; k++ {
var v T
res[k] = &v

if ows != nil && k < len(ows) {
res[k] = ows[k]
}
}

return res
}

// CvtToAnys converts the given slice of any type to a slice of values of the given type. Note that the given slice must be a slice of pointers.
func CvtToT[T any](vals []interface{}) []T {
res := make([]T, len(vals))
for k, v := range vals {
res[k] = *v.(*T)
}

return res
}

// CvtToPointerT converts the given slice of any type to a slice of pointers to the given type. Note that the given slice must be a slice of pointers.
func CvtToPointerT[T any](vals []interface{}) []*T {
res := make([]*T, len(vals))
for k, v := range vals {
res[k] = v.(*T)
}

return res
}
224 changes: 224 additions & 0 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
package utils

import "testing"

type Person struct {
ID int
Name string
}

func TestCvtToAnysWithOW(t *testing.T) {
tests := []struct {
desc string
i int
ow *Person
want []interface{}
}{
{
desc: "i is 1 and ow is nil",
i: 1,
ow: nil,
want: []interface{}{&Person{}},
},
{
desc: "i is 1 and ow is not nil",
i: 1,
ow: &Person{ID: 1, Name: "foo"},
want: []interface{}{&Person{ID: 1, Name: "foo"}},
},
{
desc: "i is 3 and ow is nil",
i: 3,
ow: nil,
want: []interface{}{&Person{}, &Person{}, &Person{}},
},
{
desc: "i is 3 and ow is not nil",
i: 3,
ow: &Person{ID: 1, Name: "foo"},
want: []interface{}{&Person{ID: 1, Name: "foo"}, &Person{ID: 1, Name: "foo"}, &Person{ID: 1, Name: "foo"}},
},
{
desc: "i is 0 and ow is nil",
i: 0,
ow: nil,
want: []interface{}{},
},
{
desc: "i is 0 and ow is not nil",
i: 0,
ow: &Person{ID: 1, Name: "foo"},
want: []interface{}{},
},
}

for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
res := CvtToAnysWithOW(test.i, test.ow)

if len(res) != len(test.want) {
t.Errorf("expected length of %d, but got %d", len(test.want), len(res))
}

for k, v := range res {
if *v.(*Person) != *test.want[k].(*Person) {
t.Errorf("expected %v, but got %v", *test.want[k].(*Person), *v.(*Person))
}
}
})
}
}

func TestCvtToAnysWithOWs(t *testing.T) {
tests := []struct {
desc string
i int
ows []*Person
want []interface{}
}{
{
desc: "i is 1 and ows is nil",
i: 1,
ows: nil,
want: []interface{}{&Person{}},
},
{
desc: "i is 1 and ows is not nil",
i: 1,
ows: []*Person{{ID: 1, Name: "foo"}},
want: []interface{}{&Person{ID: 1, Name: "foo"}},
},
{
desc: "i is 3 and ows is nil",
i: 3,
ows: nil,
want: []interface{}{&Person{}, &Person{}, &Person{}},
},
{
desc: "i is 3 and ows is not nil",
i: 3,
ows: []*Person{{ID: 1, Name: "foo"}, {ID: 2, Name: "bar"}, {ID: 3, Name: "baz"}},
want: []interface{}{&Person{ID: 1, Name: "foo"}, &Person{ID: 2, Name: "bar"}, &Person{ID: 3, Name: "baz"}},
},
{
desc: "i is greater than len(ows)",
i: 3,
ows: []*Person{{ID: 1, Name: "foo"}},
want: []interface{}{&Person{ID: 1, Name: "foo"}, &Person{}, &Person{}},
},
{
desc: "i is less than len(ows)",
i: 1,
ows: []*Person{{ID: 1, Name: "foo"}, {ID: 2, Name: "bar"}},
want: []interface{}{&Person{ID: 1, Name: "foo"}},
},
{
desc: "i is 0 and ows is nil",
i: 0,
ows: nil,
want: []interface{}{},
},
{
desc: "i is 0 and ows is not nil",
i: 0,
ows: []*Person{{ID: 1, Name: "foo"}},
want: []interface{}{},
},
}

for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
res := CvtToAnysWithOWs(test.i, test.ows...)

if len(res) != len(test.want) {
t.Errorf("expected length of %d, but got %d", len(test.want), len(res))
}

for k, v := range res {
if *v.(*Person) != *test.want[k].(*Person) {
t.Errorf("expected %v, but got %v", *test.want[k].(*Person), *v.(*Person))
}
}
})
}
}

func TestCvtToT(t *testing.T) {
tests := []struct {
desc string
vals []interface{}
want []Person
}{
{
desc: "vals is nil",
vals: nil,
want: nil,
},
{
desc: "vals is empty",
vals: []interface{}{},
want: []Person{},
},
{
desc: "vals is not empty",
vals: []interface{}{&Person{ID: 1, Name: "foo"}, &Person{ID: 2, Name: "bar"}, &Person{ID: 3, Name: "baz"}},
want: []Person{{ID: 1, Name: "foo"}, {ID: 2, Name: "bar"}, {ID: 3, Name: "baz"}},
},
}

for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
res := CvtToT[Person](test.vals)

if len(res) != len(test.want) {
t.Errorf("expected length of %d, but got %d", len(test.want), len(res))
}

for k, v := range res {
if v != test.want[k] {
t.Errorf("expected %v, but got %v", test.want[k], v)
}
}
})
}
}

func TestCvtToPointerT(t *testing.T) {
tests := []struct {
desc string
vals []interface{}
want []*Person
}{
{
desc: "vals is nil",
vals: nil,
want: nil,
},
{
desc: "vals is empty",
vals: []interface{}{},
want: []*Person{},
},
{
desc: "vals is not empty",
vals: []interface{}{&Person{ID: 1, Name: "foo"}, &Person{ID: 2, Name: "bar"}, &Person{ID: 3, Name: "baz"}},
want: []*Person{{ID: 1, Name: "foo"}, {ID: 2, Name: "bar"}, {ID: 3, Name: "baz"}},
},
}

for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
res := CvtToPointerT[Person](test.vals)

if len(res) != len(test.want) {
t.Errorf("expected length of %d, but got %d", len(test.want), len(res))
}

for k, v := range res {
if *v != *test.want[k] {
t.Errorf("expected %v, but got %v", *test.want[k], *v)
}
}
})
}
}