-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds slice utils and default value getter (#14)
- Loading branch information
1 parent
120b323
commit ae8fff4
Showing
6 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package utils | ||
|
||
// IndexInSlice returns whether an index exists within a given slice | ||
func IndexInSlice[T any](v []T, index int) bool { | ||
if index < 0 { | ||
// slices cannot be less than 0 in size | ||
return false | ||
} | ||
return len(v) > index | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package utils_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/aidenwallis/go-utils/utils" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestIndexInSlice(t *testing.T) { | ||
t.Parallel() | ||
v := []int{0, 1, 2, 3} | ||
assert.False(t, utils.IndexInSlice(v, -1)) | ||
assert.True(t, utils.IndexInSlice(v, 0)) | ||
assert.True(t, utils.IndexInSlice(v, 1)) | ||
assert.True(t, utils.IndexInSlice(v, 2)) | ||
assert.True(t, utils.IndexInSlice(v, 3)) | ||
assert.False(t, utils.IndexInSlice(v, 4)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package utils | ||
|
||
import "github.com/aidenwallis/go-utils/val" | ||
|
||
// ValueAtIndex returns the value at a given index in a slice | ||
func ValueAtIndex[T any](v []T, index int) (T, bool) { | ||
if IndexInSlice(v, index) { | ||
return v[index], true | ||
} | ||
return val.Default[T](), false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package utils_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/aidenwallis/go-utils/utils" | ||
"github.com/aidenwallis/go-utils/val" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestValueAtIndex(t *testing.T) { | ||
t.Parallel() | ||
|
||
// handles normal slices | ||
{ | ||
v := []int{0, 1, 2} | ||
|
||
{ | ||
r, ok := utils.ValueAtIndex(v, 0) | ||
assert.Equal(t, 0, r) | ||
assert.True(t, ok) | ||
} | ||
|
||
{ | ||
r, ok := utils.ValueAtIndex(v, 1) | ||
assert.Equal(t, 1, r) | ||
assert.True(t, ok) | ||
} | ||
|
||
{ | ||
r, ok := utils.ValueAtIndex(v, 2) | ||
assert.Equal(t, 2, r) | ||
assert.True(t, ok) | ||
} | ||
|
||
{ | ||
r, ok := utils.ValueAtIndex(v, 3) | ||
assert.Equal(t, 0, r) | ||
assert.False(t, ok) | ||
} | ||
} | ||
|
||
// handles pointer slices | ||
{ | ||
v := []*int{val.Pointer(0)} | ||
|
||
{ | ||
r, ok := utils.ValueAtIndex(v, 0) | ||
assert.Equal(t, 0, *r) | ||
assert.True(t, ok) | ||
} | ||
|
||
{ | ||
r, ok := utils.ValueAtIndex(v, 1) | ||
assert.Nil(t, r) | ||
assert.False(t, ok) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package val | ||
|
||
// Default returns the default value of any type (if pointer, it is nil). | ||
func Default[T any]() T { | ||
var r T | ||
return r | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package val_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/aidenwallis/go-utils/val" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestDefault(t *testing.T) { | ||
t.Parallel() | ||
assert.Equal(t, 0, val.Default[int]()) | ||
assert.Nil(t, val.Default[*int]()) | ||
} |