Skip to content

Commit

Permalink
👔 up: go,reflects - add alias func IsZero() for IsEmpty(). issues #107
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jul 12, 2023
1 parent 5832f25 commit 04a08af
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 4 deletions.
3 changes: 3 additions & 0 deletions check.go
Expand Up @@ -15,6 +15,9 @@ func IsNil(v any) bool {
return reflects.IsNil(reflect.ValueOf(v))
}

// IsZero value check, alias of the IsEmpty()
var IsZero = IsEmpty

// IsEmpty value check
func IsEmpty(v any) bool {
if v == nil {
Expand Down
1 change: 1 addition & 0 deletions check_test.go
Expand Up @@ -11,6 +11,7 @@ func TestIsEmpty(t *testing.T) {
is := assert.New(t)

is.True(goutil.IsEmpty(nil))
is.False(goutil.IsZero("abc"))
is.False(goutil.IsEmpty("abc"))
}

Expand Down
17 changes: 13 additions & 4 deletions reflects/check.go
Expand Up @@ -84,6 +84,9 @@ func IsEqual(src, dst any) bool {
return bytes.Equal(bs1, bs2)
}

// IsZero reflect value check, alias of the IsEmpty()
var IsZero = IsEmpty

// IsEmpty reflect value check
func IsEmpty(v reflect.Value) bool {
switch v.Kind() {
Expand All @@ -108,11 +111,17 @@ func IsEmpty(v reflect.Value) bool {
return reflect.DeepEqual(v.Interface(), reflect.Zero(v.Type()).Interface())
}

// IsEmptyValue reflect value check.
// Difference the IsEmpty(), if value is ptr, will check real elem.
// IsEmptyValue reflect value check, alias of the IsEmptyReal()
var IsEmptyValue = IsEmptyReal

// IsEmptyReal reflect value check.
//
// Note:
//
// Difference the IsEmpty(), if value is ptr or interface, will check real elem.
//
// From src/pkg/encoding/json/encode.go.
func IsEmptyValue(v reflect.Value) bool {
func IsEmptyReal(v reflect.Value) bool {
switch v.Kind() {
case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
return v.Len() == 0
Expand All @@ -128,7 +137,7 @@ func IsEmptyValue(v reflect.Value) bool {
if v.IsNil() {
return true
}
return IsEmptyValue(v.Elem())
return IsEmptyReal(v.Elem())
case reflect.Func:
return v.IsNil()
case reflect.Invalid:
Expand Down
4 changes: 4 additions & 0 deletions reflects/check_test.go
Expand Up @@ -4,6 +4,7 @@ import (
"reflect"
"testing"

"github.com/gookit/goutil/comdef"
"github.com/gookit/goutil/reflects"
"github.com/gookit/goutil/testutil/assert"
)
Expand Down Expand Up @@ -42,6 +43,7 @@ func TestIsEqual(t *testing.T) {
func TestIsEmpty(t *testing.T) {
is := assert.New(t)

is.True(reflects.IsZero(reflect.ValueOf(nil)))
is.True(reflects.IsEmpty(reflect.ValueOf(nil)))
is.True(reflects.IsEmpty(reflect.ValueOf(false)))
is.True(reflects.IsEmpty(reflect.ValueOf("")))
Expand All @@ -50,6 +52,7 @@ func TestIsEmpty(t *testing.T) {
is.True(reflects.IsEmpty(reflect.ValueOf(0)))
is.True(reflects.IsEmpty(reflect.ValueOf(uint(0))))
is.True(reflects.IsEmpty(reflect.ValueOf(float32(0))))
is.True(reflects.IsEmpty(reflect.ValueOf(comdef.StringMatchFunc(nil))))

type T struct {
v any //lint:ignore U1000 for test
Expand All @@ -69,6 +72,7 @@ func TestIsEmptyValue(t *testing.T) {
is.True(reflects.IsEmptyValue(reflect.ValueOf(0)))
is.True(reflects.IsEmptyValue(reflect.ValueOf(uint(0))))
is.True(reflects.IsEmptyValue(reflect.ValueOf(float32(0))))
is.True(reflects.IsEmptyReal(reflect.ValueOf(comdef.StringMatchFunc(nil))))

type T struct {
v any //lint:ignore U1000 for test
Expand Down
6 changes: 6 additions & 0 deletions testutil/fakeobj/io.go
Expand Up @@ -41,6 +41,12 @@ func (w *Writer) SetErrOnFlush() *Writer {
return w
}

// SetErrOnSync method
func (w *Writer) SetErrOnSync() *Writer {
w.ErrOnSync = true
return w
}

// SetErrOnClose method
func (w *Writer) SetErrOnClose() *Writer {
w.ErrOnClose = true
Expand Down
4 changes: 4 additions & 0 deletions testutil/fakeobj/io_test.go
Expand Up @@ -13,6 +13,7 @@ func TestNewWriter(t *testing.T) {
assert.NoErr(t, err)
assert.Eq(t, "hello", tw.String())
assert.NoErr(t, tw.Flush())
assert.NoErr(t, tw.Sync())
assert.Eq(t, "", tw.String())
assert.NoErr(t, tw.Close())

Expand All @@ -29,6 +30,9 @@ func TestNewWriter(t *testing.T) {
tw.SetErrOnFlush()
assert.Err(t, tw.Flush())

tw.SetErrOnSync()
assert.Err(t, tw.Sync())

tw.SetErrOnClose()
assert.Err(t, tw.Close())
}

0 comments on commit 04a08af

Please sign in to comment.