Skip to content

Commit

Permalink
feat: add DerefOrEmpty func to the client
Browse files Browse the repository at this point in the history
  • Loading branch information
ikadix committed Jul 3, 2024
1 parent 51a03b1 commit d2b3b98
Show file tree
Hide file tree
Showing 2 changed files with 189 additions and 0 deletions.
22 changes: 22 additions & 0 deletions next/deref.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package next

import "reflect"

// DerefOrEmpty returns the dereferenced value of the input pointer, or the zero
// value of the type if the input is nil.
func DerefOrEmpty[T any](in *T) T {
if in == nil {
tType := reflect.TypeOf(*new(T))
switch tType.Kind() {

Check failure on line 10 in next/deref.go

View workflow job for this annotation

GitHub Actions / Lint

missing cases in switch of type reflect.Kind: reflect.Invalid, reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr, reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128, reflect.Chan, reflect.Func, reflect.Interface, reflect.Pointer|reflect.Ptr, reflect.String, reflect.Struct, reflect.UnsafePointer (exhaustive)
case reflect.Slice, reflect.Array:
return reflect.MakeSlice(tType, 0, 0).Interface().(T)
case reflect.Map:
return reflect.MakeMap(tType).Interface().(T)
default:
var empty T
return empty

Check failure on line 17 in next/deref.go

View workflow job for this annotation

GitHub Actions / Lint

return with no blank line before (nlreturn)
}
}

return *in
}
167 changes: 167 additions & 0 deletions next/deref_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package next_test

import (
"testing"

"github.com/krystal/go-katapult/next"
"github.com/stretchr/testify/require"
)

func ptr[T any](v T) *T {
return &v
}

func TestDerefOrEmpty_string(t *testing.T) {
tests := []struct {
name string
in *string
want string
}{
{
name: "Nil input",
in: nil,
want: "",
},
{
name: "Non-nil input",
in: ptr("hello"),
want: "hello",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.want, next.DerefOrEmpty(tt.in))
})
}
}

func TestDerefOrEmpty_int(t *testing.T) {
tests := []struct {
name string
in *int
want int
}{
{
name: "Nil input",
in: nil,
want: 0,
},
{
name: "Non-nil input",
in: ptr(22),
want: 22,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.want, next.DerefOrEmpty(tt.in))
})
}
}

func TestDerefOrEmpty_float64(t *testing.T) {
tests := []struct {
name string
in *float64
want float64
}{
{
name: "Nil input",
in: nil,
want: 0.0,
},
{
name: "Non-nil input",
in: ptr(3.14),
want: 3.14,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.want, next.DerefOrEmpty(tt.in))
})
}
}

func TestDerefOrEmpty_slice(t *testing.T) {
tests := []struct {
name string
in *[]string
want []string
}{
{
name: "Nil input",
in: nil,
want: []string{},
},
{
name: "Non-nil input",
in: ptr([]string{"hello", "world"}),
want: []string{"hello", "world"},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.want, next.DerefOrEmpty(tt.in))
})
}
}

func TestDerefOrEmpty_map(t *testing.T) {
tests := []struct {
name string
in *map[string]int
want map[string]int
}{
{
name: "Nil input",
in: nil,
want: map[string]int{},
},
{
name: "Non-nil input",
in: ptr(map[string]int{"hello": 32}),
want: map[string]int{"hello": 32},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.want, next.DerefOrEmpty(tt.in))
})
}
}

func TestDerefOrEmpty_struct(t *testing.T) {
type testStruct struct {
Name string
Age int
}

tests := []struct {
name string
in *testStruct
want testStruct
}{
{
name: "Nil input",
in: nil,
want: testStruct{},
},
{
name: "Non-nil input",
in: ptr(testStruct{Name: "Alice", Age: 22}),
want: testStruct{Name: "Alice", Age: 22},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.want, next.DerefOrEmpty(tt.in))
})
}
}

0 comments on commit d2b3b98

Please sign in to comment.