Files
gf/internal/empty/empty.go

244 lines
5.2 KiB
Go
Raw Normal View History

// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved.
//
// 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.
// Package empty provides functions for checking empty/nil variables.
package empty
import (
2019-06-05 18:40:26 +08:00
"reflect"
"time"
2022-03-11 10:24:42 +08:00
"github.com/gogf/gf/v2/internal/reflection"
)
// iString is used for type assert api for String().
type iString interface {
String() string
}
// iInterfaces is used for type assert api for Interfaces.
type iInterfaces interface {
Interfaces() []interface{}
}
// iMapStrAny is the interface support for converting struct parameter to map.
type iMapStrAny interface {
MapStrAny() map[string]interface{}
}
type iTime 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.
//
// The parameter `traceSource` is used for tracing to the source variable if given `value` is type of pointer
// that also points to a pointer. It returns true if the source is empty when `traceSource` is true.
// Note that it might use reflect feature which affects performance a little.
func IsEmpty(value interface{}, traceSource ...bool) bool {
2019-06-19 09:06:52 +08:00
if value == nil {
return true
}
// It firstly checks the variable as common types using assertion to enhance the performance,
// and then using reflection.
2021-10-06 19:52:19 +08:00
switch result := value.(type) {
2019-06-19 09:06:52 +08:00
case int:
2021-10-06 19:52:19 +08:00
return result == 0
2019-06-19 09:06:52 +08:00
case int8:
2021-10-06 19:52:19 +08:00
return result == 0
2019-06-19 09:06:52 +08:00
case int16:
2021-10-06 19:52:19 +08:00
return result == 0
2019-06-19 09:06:52 +08:00
case int32:
2021-10-06 19:52:19 +08:00
return result == 0
2019-06-19 09:06:52 +08:00
case int64:
2021-10-06 19:52:19 +08:00
return result == 0
2019-06-19 09:06:52 +08:00
case uint:
2021-10-06 19:52:19 +08:00
return result == 0
2019-06-19 09:06:52 +08:00
case uint8:
2021-10-06 19:52:19 +08:00
return result == 0
2019-06-19 09:06:52 +08:00
case uint16:
2021-10-06 19:52:19 +08:00
return result == 0
2019-06-19 09:06:52 +08:00
case uint32:
2021-10-06 19:52:19 +08:00
return result == 0
2019-06-19 09:06:52 +08:00
case uint64:
2021-10-06 19:52:19 +08:00
return result == 0
2019-06-19 09:06:52 +08:00
case float32:
2021-10-06 19:52:19 +08:00
return result == 0
2019-06-19 09:06:52 +08:00
case float64:
2021-10-06 19:52:19 +08:00
return result == 0
2019-06-19 09:06:52 +08:00
case bool:
return !result
2019-06-19 09:06:52 +08:00
case string:
2021-10-06 19:52:19 +08:00
return result == ""
2019-06-19 09:06:52 +08:00
case []byte:
2021-10-06 19:52:19 +08:00
return len(result) == 0
2020-04-13 23:44:43 +08:00
case []rune:
2021-10-06 19:52:19 +08:00
return len(result) == 0
case []int:
2021-10-06 19:52:19 +08:00
return len(result) == 0
case []string:
2021-10-06 19:52:19 +08:00
return len(result) == 0
case []float32:
2021-10-06 19:52:19 +08:00
return len(result) == 0
case []float64:
2021-10-06 19:52:19 +08:00
return len(result) == 0
case map[string]interface{}:
2021-10-06 19:52:19 +08:00
return len(result) == 0
2019-06-19 09:06:52 +08:00
default:
2021-10-06 19:52:19 +08:00
// 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.
// =========================
if f, ok := value.(iTime); ok {
if f == (*time.Time)(nil) {
return true
}
return f.IsZero()
}
if f, ok := value.(iString); ok {
if f == nil {
return true
}
return f.String() == ""
}
if f, ok := value.(iInterfaces); ok {
if f == nil {
return true
}
return len(f.Interfaces()) == 0
}
if f, ok := value.(iMapStrAny); ok {
if f == nil {
return true
}
return len(f.MapStrAny()) == 0
}
}
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()
2021-10-06 19:52:19 +08:00
case
reflect.Int,
2020-12-12 21:57:07 +08:00
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64:
return rv.Int() == 0
2021-10-06 19:52:19 +08:00
case
reflect.Uint,
2020-12-12 21:57:07 +08:00
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64,
reflect.Uintptr:
return rv.Uint() == 0
2021-10-06 19:52:19 +08:00
case
reflect.Float32,
2020-12-12 21:57:07 +08:00
reflect.Float64:
return rv.Float() == 0
2021-10-06 19:52:19 +08:00
2020-12-12 21:57:07 +08:00
case reflect.String:
return rv.Len() == 0
2021-10-06 19:52:19 +08:00
2020-12-12 21:57:07 +08:00
case reflect.Struct:
2022-03-11 10:24:42 +08:00
var fieldValueInterface interface{}
2020-12-12 21:57:07 +08:00
for i := 0; i < rv.NumField(); i++ {
2022-03-11 10:24:42 +08:00
fieldValueInterface, _ = reflection.ValueToInterface(rv.Field(i))
if !IsEmpty(fieldValueInterface) {
2020-12-12 21:57:07 +08:00
return false
}
}
return true
2021-10-06 19:52:19 +08:00
case
reflect.Chan,
2019-06-19 09:06:52 +08:00
reflect.Map,
reflect.Slice,
reflect.Array:
return rv.Len() == 0
2021-10-06 19:52:19 +08:00
case reflect.Ptr:
if len(traceSource) > 0 && traceSource[0] {
return IsEmpty(rv.Elem())
}
return rv.IsNil()
2021-10-06 19:52:19 +08:00
case
reflect.Func,
2019-06-19 09:06:52 +08:00
reflect.Interface,
reflect.UnsafePointer:
return rv.IsNil()
case reflect.Invalid:
return true
default:
return false
2019-06-19 09:06:52 +08:00
}
}
}
2021-12-20 00:32:23 +08:00
// IsNil checks whether given `value` is nil, especially for interface{} type value.
// Parameter `traceSource` is used for tracing to the source variable if given `value` is type of pointer
// that also points to a pointer. It returns nil if the source is nil when `traceSource` is true.
// Note that it might use reflect feature which affects performance a little.
func IsNil(value interface{}, traceSource ...bool) bool {
if value == nil {
return true
}
var rv reflect.Value
if v, ok := value.(reflect.Value); ok {
rv = v
} else {
rv = reflect.ValueOf(value)
}
switch rv.Kind() {
case reflect.Chan,
reflect.Map,
reflect.Slice,
reflect.Func,
reflect.Interface,
reflect.UnsafePointer:
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()
}
default:
return false
}
return false
}