parent
b22b0f0919
commit
9fb3094b87
@ -1,23 +0,0 @@ |
||||
package utils |
||||
|
||||
import ( |
||||
"unsafe" |
||||
) |
||||
|
||||
// IsNil function evaluates the interface value to nil
|
||||
func IsNil(i interface{}) (isNil bool) { |
||||
|
||||
type iface struct { |
||||
_ unsafe.Pointer |
||||
ptr unsafe.Pointer |
||||
} |
||||
|
||||
unpacked := (*iface)(unsafe.Pointer(&i)) |
||||
if unpacked.ptr == nil { |
||||
isNil = true |
||||
return |
||||
} |
||||
|
||||
isNil = *(*unsafe.Pointer)(unpacked.ptr) == nil |
||||
return |
||||
} |
@ -1,32 +0,0 @@ |
||||
package utils |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/stretchr/testify/assert" |
||||
) |
||||
|
||||
func TestIsNil(t *testing.T) { |
||||
|
||||
testvalues := []struct { |
||||
i interface{} |
||||
isnil bool |
||||
}{ |
||||
{nil, true}, |
||||
{(*string)(nil), true}, |
||||
{([]string)(nil), true}, |
||||
{(map[string]string)(nil), true}, |
||||
{(func() bool)(nil), true}, |
||||
{(chan func() bool)(nil), true}, |
||||
{"", true}, |
||||
{0, true}, |
||||
{append(([]string)(nil), ""), false}, |
||||
{[]string{}, false}, |
||||
{1, false}, |
||||
{"1", false}, |
||||
} |
||||
|
||||
for _, tt := range testvalues { |
||||
assert.Equal(t, tt.isnil, IsNil(tt.i)) |
||||
} |
||||
} |
Loading…
Reference in new issue