diff --git a/internal/empty/empty.go b/internal/empty/empty.go index 07fee1e2415..eed7a114b81 100644 --- a/internal/empty/empty.go +++ b/internal/empty/empty.go @@ -92,11 +92,17 @@ func IsEmpty(value interface{}, traceSource ...bool) bool { return len(result) == 0 default: + // Finally, using reflect. var rv reflect.Value if v, ok := value.(reflect.Value); ok { rv = v } else { + rv = reflect.ValueOf(value) + if IsNil(rv) { + return true + } + // ========================= // Common interfaces checks. // ========================= @@ -124,8 +130,6 @@ func IsEmpty(value interface{}, traceSource ...bool) bool { } return len(f.MapStrAny()) == 0 } - - rv = reflect.ValueOf(value) } switch rv.Kind() { @@ -188,9 +192,11 @@ func IsEmpty(value interface{}, traceSource ...bool) bool { case reflect.Invalid: return true + + default: + return false } } - return false } // IsNil checks whether given `value` is nil, especially for interface{} type value. @@ -230,6 +236,9 @@ func IsNil(value interface{}, traceSource ...bool) bool { } else { return !rv.IsValid() || rv.IsNil() } + + default: + return false } return false } diff --git a/internal/empty/empty_z_unit_test.go b/internal/empty/empty_z_unit_test.go index 6b9efed1e58..ed14be13455 100644 --- a/internal/empty/empty_z_unit_test.go +++ b/internal/empty/empty_z_unit_test.go @@ -7,7 +7,9 @@ package empty_test import ( + "github.com/gogf/gf/v2/container/gvar" "testing" + "time" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/internal/empty" @@ -128,3 +130,36 @@ func TestIsNil(t *testing.T) { t.Assert(empty.IsNil(&i, true), true) }) } + +type Issue3362St struct { + time.Time +} + +func Test_Issue3362(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + type A struct { + Issue3362 *Issue3362St `json:"issue,omitempty"` + } + m := gvar.New( + &A{}, + ).Map( + gvar.MapOption{ + OmitEmpty: true, + }, + ) + t.Assert(m, nil) + }) + gtest.C(t, func(t *gtest.T) { + var i int + t.Assert(empty.IsNil(i), false) + }) + gtest.C(t, func(t *gtest.T) { + var i *int + t.Assert(empty.IsNil(i), true) + }) + gtest.C(t, func(t *gtest.T) { + var i *int + t.Assert(empty.IsNil(&i), false) + t.Assert(empty.IsNil(&i, true), true) + }) +}