-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IsNil() checks if the underlying value of the interface is nil.
- Loading branch information
1 parent
e55aa34
commit eb1f2ca
Showing
3 changed files
with
57 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package utils | ||
|
||
import "reflect" | ||
|
||
// IsNil checks if the underlying value of the interface is nil. | ||
// | ||
// In Golang, an interface is boxed by an underlying type and value; both of them need to be nil for the interface to be | ||
// nil. See the following examples: | ||
// | ||
// var i interface{} | ||
// fmt.Println(i == nil) // true | ||
// | ||
// var p *int | ||
// var i interface{} = p | ||
// fmt.Println(i == nil) // false! | ||
// | ||
// A solution for this is to use i == nil || reflect.ValueOf(i).IsNil()), however, this can cause a panic as not all | ||
// reflect.Value has IsNil() defined. Therefore, default is to return false. For example, reflect.Array cannot be nil, | ||
// hence calling IsNil() will cause a panic. | ||
func IsNil(i interface{}) bool { | ||
if i == nil { | ||
return true | ||
} | ||
|
||
v := reflect.ValueOf(i) | ||
k := v.Kind() | ||
|
||
switch k { | ||
case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.UnsafePointer, reflect.Interface, reflect.Slice: | ||
return v.IsNil() | ||
default: | ||
return false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package utils | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestIsNil(t *testing.T) { | ||
t.Run("both interface's type and value are nil", func(t *testing.T) { | ||
var i interface{} | ||
// assert.Nil() is not used to avoid assert package from using reflection | ||
assert.True(t, IsNil(i)) | ||
}) | ||
t.Run("only interface's value is nil", func(t *testing.T) { | ||
var arr []int | ||
var i interface{} = arr | ||
// assert.Nil() is not used to avoid assert package from using reflection | ||
assert.True(t, IsNil(i)) | ||
}) | ||
} |