From 04a08afd72abbe714d11307d54934068c462f4bd Mon Sep 17 00:00:00 2001 From: Inhere Date: Wed, 12 Jul 2023 15:02:19 +0800 Subject: [PATCH] :necktie: up: go,reflects - add alias func IsZero() for IsEmpty(). issues #107 --- check.go | 3 +++ check_test.go | 1 + reflects/check.go | 17 +++++++++++++---- reflects/check_test.go | 4 ++++ testutil/fakeobj/io.go | 6 ++++++ testutil/fakeobj/io_test.go | 4 ++++ 6 files changed, 31 insertions(+), 4 deletions(-) diff --git a/check.go b/check.go index a0b0a0fc3..4c69d5402 100644 --- a/check.go +++ b/check.go @@ -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 { diff --git a/check_test.go b/check_test.go index d0aa68ac4..4da492e9f 100644 --- a/check_test.go +++ b/check_test.go @@ -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")) } diff --git a/reflects/check.go b/reflects/check.go index 7ca257c77..b0eb5e93e 100644 --- a/reflects/check.go +++ b/reflects/check.go @@ -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() { @@ -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 @@ -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: diff --git a/reflects/check_test.go b/reflects/check_test.go index d414cb8bf..1d24c8dd6 100644 --- a/reflects/check_test.go +++ b/reflects/check_test.go @@ -4,6 +4,7 @@ import ( "reflect" "testing" + "github.com/gookit/goutil/comdef" "github.com/gookit/goutil/reflects" "github.com/gookit/goutil/testutil/assert" ) @@ -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(""))) @@ -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 @@ -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 diff --git a/testutil/fakeobj/io.go b/testutil/fakeobj/io.go index bd356fcc4..857d9d905 100644 --- a/testutil/fakeobj/io.go +++ b/testutil/fakeobj/io.go @@ -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 diff --git a/testutil/fakeobj/io_test.go b/testutil/fakeobj/io_test.go index 22f849d27..64c7d337d 100644 --- a/testutil/fakeobj/io_test.go +++ b/testutil/fakeobj/io_test.go @@ -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()) @@ -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()) }