Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Removed arrays in the case statement of the isNil function #2253

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions utils/units.go
Original file line number Diff line number Diff line change
@@ -53,12 +53,12 @@ func (m *MemBytes) Value() int64 {
return int64(*m)
}

func isNil(i interface{}) bool {
func isNil(i any) bool {
if i == nil {
return true
}
switch reflect.TypeOf(i).Kind() {
case reflect.Ptr, reflect.Map, reflect.Array, reflect.Chan, reflect.Slice:
case reflect.Ptr, reflect.Map, reflect.Chan, reflect.Slice:
return reflect.ValueOf(i).IsNil()
}
return false
26 changes: 26 additions & 0 deletions utils/units_test.go
Original file line number Diff line number Diff line change
@@ -30,6 +30,10 @@ const (
gb = 1024 * mb
)

var (
ch chan bool
)

func TestMemBytes(t *testing.T) {
var m MemBytes
assert.Assert(t, cmp.Nil(m.Set("42")))
@@ -58,3 +62,25 @@ func TestMemBytes(t *testing.T) {

assert.Error(t, m.Set("###"), "invalid size: '###'")
}

func TestIsNil(t *testing.T) {
testCases := []struct {
input any
expected bool
}{
{nil, true},
{(*int)(nil), true},
{map[string]int(nil), true},
{[5]int{}, false},
{ch, true},
{[]int(nil), true},
{make(chan int), false},
{10, false},
{struct{ Name string }{"Test"}, false},
}

for _, tc := range testCases {
actual := isNil(tc.input)
assert.Equal(t, tc.expected, actual)
}
}