From 3ebc01bb648ebc0415db516216764b60d71f2d28 Mon Sep 17 00:00:00 2001 From: yamasaki Date: Sun, 18 Jun 2023 23:54:37 +0900 Subject: [PATCH] fix : isnil case array delete Signed-off-by: yamasaki --- utils/units.go | 4 ++-- utils/units_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/utils/units.go b/utils/units.go index 5ba4661f3..9e0b64964 100644 --- a/utils/units.go +++ b/utils/units.go @@ -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 diff --git a/utils/units_test.go b/utils/units_test.go index 2e35e11b2..b3a552169 100644 --- a/utils/units_test.go +++ b/utils/units_test.go @@ -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) + } +}