gf/internal/empty/empty.go
2020-04-13 23:44:43 +08:00

105 lines
2.1 KiB
Go

// Copyright 2019 gf Author(https://github.com/gogf/gf). 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 checks for empty variables.
package empty
import (
"reflect"
)
// IsEmpty checks whether given <value> empty.
// It returns true if <value> is in: 0, nil, false, "", len(slice/map/chan) == 0,
// or else it returns false.
func IsEmpty(value interface{}) bool {
if value == nil {
return true
}
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
case []rune:
return len(value) == 0
default:
// Finally using reflect.
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.Array:
return rv.Len() == 0
case reflect.Func,
reflect.Ptr,
reflect.Interface,
reflect.UnsafePointer:
if rv.IsNil() {
return true
}
}
}
return false
}
// IsNil checks whether given <value> is nil.
// Note that it might use reflect feature which affects performance a little bit.
func IsNil(value interface{}) 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.Ptr,
reflect.Interface,
reflect.UnsafePointer:
return rv.IsNil()
}
return false
}