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

Adds slice utils and val.Default() #14

Merged
merged 1 commit into from
Aug 30, 2022
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
10 changes: 10 additions & 0 deletions utils/index_in_slice.go
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
}
19 changes: 19 additions & 0 deletions utils/index_in_slice_test.go
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))
}
11 changes: 11 additions & 0 deletions utils/value_at_index.go
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
}
59 changes: 59 additions & 0 deletions utils/value_at_index_test.go
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)
}
}
}
7 changes: 7 additions & 0 deletions val/default.go
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
}
14 changes: 14 additions & 0 deletions val/default_test.go
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]())
}