2021-01-09 21:05:47 +08:00
|
|
|
// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved.
|
2019-03-04 22:59:29 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
2021-01-09 21:05:47 +08:00
|
|
|
// Package empty provides functions for checking empty/nil variables.
|
2019-03-04 22:59:29 +08:00
|
|
|
package empty
|
|
|
|
|
|
|
|
import (
|
2019-06-05 18:40:26 +08:00
|
|
|
"reflect"
|
2021-04-26 20:37:36 +08:00
|
|
|
"time"
|
2019-03-04 22:59:29 +08:00
|
|
|
)
|
|
|
|
|
2020-05-25 14:26:08 +08:00
|
|
|
// apiString is used for type assert api for String().
|
|
|
|
type apiString interface {
|
|
|
|
String() string
|
|
|
|
}
|
|
|
|
|
|
|
|
// apiInterfaces is used for type assert api for Interfaces.
|
|
|
|
type apiInterfaces interface {
|
|
|
|
Interfaces() []interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// apiMapStrAny is the interface support for converting struct parameter to map.
|
|
|
|
type apiMapStrAny interface {
|
|
|
|
MapStrAny() map[string]interface{}
|
|
|
|
}
|
|
|
|
|
2021-04-26 20:37:36 +08:00
|
|
|
type apiTime interface {
|
|
|
|
Date() (year int, month time.Month, day int)
|
|
|
|
IsZero() bool
|
|
|
|
}
|
|
|
|
|
2021-02-08 17:57:21 +08:00
|
|
|
// IsEmpty checks whether given `value` empty.
|
|
|
|
// It returns true if `value` is in: 0, nil, false, "", len(slice/map/chan) == 0,
|
2020-04-13 23:44:43 +08:00
|
|
|
// or else it returns false.
|
2019-03-04 22:59:29 +08:00
|
|
|
func IsEmpty(value interface{}) bool {
|
2019-06-19 09:06:52 +08:00
|
|
|
if value == nil {
|
|
|
|
return true
|
|
|
|
}
|
2020-12-25 01:44:07 +08:00
|
|
|
// It firstly checks the variable as common types using assertion to enhance the performance,
|
|
|
|
// and then using reflection.
|
2019-06-19 09:06:52 +08:00
|
|
|
switch value := value.(type) {
|
|
|
|
case int:
|
|
|
|
return value == 0
|
|
|
|
case int8:
|
|
|
|
return value == 0
|
|
|
|
case int16:
|
|
|
|
return value == 0
|
|
|
|
case int32:
|
|
|
|
return value == 0
|
|
|
|
case int64:
|
|
|
|
return value == 0
|
|
|
|
case uint:
|
|
|
|
return value == 0
|
|
|
|
case uint8:
|
|
|
|
return value == 0
|
|
|
|
case uint16:
|
|
|
|
return value == 0
|
|
|
|
case uint32:
|
|
|
|
return value == 0
|
|
|
|
case uint64:
|
|
|
|
return value == 0
|
|
|
|
case float32:
|
|
|
|
return value == 0
|
|
|
|
case float64:
|
|
|
|
return value == 0
|
|
|
|
case bool:
|
|
|
|
return value == false
|
|
|
|
case string:
|
|
|
|
return value == ""
|
|
|
|
case []byte:
|
|
|
|
return len(value) == 0
|
2020-04-13 23:44:43 +08:00
|
|
|
case []rune:
|
|
|
|
return len(value) == 0
|
2020-11-05 20:40:34 +08:00
|
|
|
case []int:
|
|
|
|
return len(value) == 0
|
|
|
|
case []string:
|
|
|
|
return len(value) == 0
|
|
|
|
case []float32:
|
|
|
|
return len(value) == 0
|
|
|
|
case []float64:
|
|
|
|
return len(value) == 0
|
|
|
|
case map[string]interface{}:
|
|
|
|
return len(value) == 0
|
2019-06-19 09:06:52 +08:00
|
|
|
default:
|
2020-05-25 14:26:08 +08:00
|
|
|
// Common interfaces checks.
|
2021-04-26 20:37:36 +08:00
|
|
|
if f, ok := value.(apiTime); ok {
|
|
|
|
if f == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return f.IsZero()
|
|
|
|
}
|
2020-05-25 14:26:08 +08:00
|
|
|
if f, ok := value.(apiString); ok {
|
2020-10-20 14:07:01 +08:00
|
|
|
if f == nil {
|
|
|
|
return true
|
|
|
|
}
|
2020-05-25 14:26:08 +08:00
|
|
|
return f.String() == ""
|
|
|
|
}
|
|
|
|
if f, ok := value.(apiInterfaces); ok {
|
2020-10-20 14:07:01 +08:00
|
|
|
if f == nil {
|
|
|
|
return true
|
|
|
|
}
|
2020-05-25 14:26:08 +08:00
|
|
|
return len(f.Interfaces()) == 0
|
|
|
|
}
|
|
|
|
if f, ok := value.(apiMapStrAny); ok {
|
2020-10-20 14:07:01 +08:00
|
|
|
if f == nil {
|
|
|
|
return true
|
|
|
|
}
|
2020-05-25 14:26:08 +08:00
|
|
|
return len(f.MapStrAny()) == 0
|
|
|
|
}
|
2019-06-19 09:06:52 +08:00
|
|
|
// Finally using reflect.
|
2020-03-13 17:21:30 +08:00
|
|
|
var rv reflect.Value
|
|
|
|
if v, ok := value.(reflect.Value); ok {
|
|
|
|
rv = v
|
|
|
|
} else {
|
|
|
|
rv = reflect.ValueOf(value)
|
|
|
|
}
|
2020-12-12 21:57:07 +08:00
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
switch rv.Kind() {
|
2020-12-12 21:57:07 +08:00
|
|
|
case reflect.Bool:
|
|
|
|
return !rv.Bool()
|
|
|
|
case reflect.Int,
|
|
|
|
reflect.Int8,
|
|
|
|
reflect.Int16,
|
|
|
|
reflect.Int32,
|
|
|
|
reflect.Int64:
|
|
|
|
return rv.Int() == 0
|
|
|
|
case reflect.Uint,
|
|
|
|
reflect.Uint8,
|
|
|
|
reflect.Uint16,
|
|
|
|
reflect.Uint32,
|
|
|
|
reflect.Uint64,
|
|
|
|
reflect.Uintptr:
|
|
|
|
return rv.Uint() == 0
|
|
|
|
case reflect.Float32,
|
|
|
|
reflect.Float64:
|
|
|
|
return rv.Float() == 0
|
|
|
|
case reflect.String:
|
|
|
|
return rv.Len() == 0
|
|
|
|
case reflect.Struct:
|
|
|
|
for i := 0; i < rv.NumField(); i++ {
|
|
|
|
if !IsEmpty(rv) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
2019-06-19 09:06:52 +08:00
|
|
|
case reflect.Chan,
|
|
|
|
reflect.Map,
|
|
|
|
reflect.Slice,
|
|
|
|
reflect.Array:
|
|
|
|
return rv.Len() == 0
|
|
|
|
case reflect.Func,
|
|
|
|
reflect.Ptr,
|
|
|
|
reflect.Interface,
|
|
|
|
reflect.UnsafePointer:
|
|
|
|
if rv.IsNil() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2019-11-21 21:49:00 +08:00
|
|
|
|
2021-02-08 17:57:21 +08:00
|
|
|
// IsNil checks whether given `value` is nil.
|
|
|
|
// Parameter `traceSource` is used for tracing to the source variable if given `value` is type
|
|
|
|
// of a pinter that also points to a pointer. It returns nil if the source is nil when `traceSource`
|
2021-01-09 21:05:47 +08:00
|
|
|
// is true.
|
2020-02-23 20:25:55 +08:00
|
|
|
// Note that it might use reflect feature which affects performance a little bit.
|
2021-01-09 21:05:47 +08:00
|
|
|
func IsNil(value interface{}, traceSource ...bool) bool {
|
2019-11-21 21:49:00 +08:00
|
|
|
if value == nil {
|
|
|
|
return true
|
|
|
|
}
|
2020-03-13 17:21:30 +08:00
|
|
|
var rv reflect.Value
|
|
|
|
if v, ok := value.(reflect.Value); ok {
|
|
|
|
rv = v
|
|
|
|
} else {
|
|
|
|
rv = reflect.ValueOf(value)
|
|
|
|
}
|
2019-11-21 21:49:00 +08:00
|
|
|
switch rv.Kind() {
|
|
|
|
case reflect.Chan,
|
|
|
|
reflect.Map,
|
|
|
|
reflect.Slice,
|
|
|
|
reflect.Func,
|
|
|
|
reflect.Interface,
|
|
|
|
reflect.UnsafePointer:
|
2021-01-09 21:05:47 +08:00
|
|
|
return !rv.IsValid() || rv.IsNil()
|
|
|
|
|
|
|
|
case reflect.Ptr:
|
|
|
|
if len(traceSource) > 0 && traceSource[0] {
|
|
|
|
for rv.Kind() == reflect.Ptr {
|
|
|
|
rv = rv.Elem()
|
|
|
|
}
|
|
|
|
if !rv.IsValid() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if rv.Kind() == reflect.Ptr {
|
|
|
|
return rv.IsNil()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return !rv.IsValid() || rv.IsNil()
|
|
|
|
}
|
2019-11-21 21:49:00 +08:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|