1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-10 07:42:27 +08:00

feat: add IsZeroValue function

This commit is contained in:
dudaodong
2022-08-29 14:59:36 +08:00
parent b6815224fd
commit d62284e9a6
4 changed files with 199 additions and 0 deletions

View File

@@ -8,6 +8,7 @@ import (
"encoding/json"
"net"
"net/url"
"reflect"
"regexp"
"strconv"
"strings"
@@ -258,3 +259,32 @@ func IsWeakPassword(password string) bool {
return (num || letter) && !special
}
// IsZeroValue checks if value is a zero value
func IsZeroValue(value interface{}) bool {
if value == nil {
return true
}
rv := reflect.ValueOf(value)
if !rv.IsValid() {
return true
}
switch rv.Kind() {
case reflect.String:
return rv.Len() == 0
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.Ptr, reflect.Chan, reflect.Func, reflect.Interface, reflect.Slice, reflect.Map:
return rv.IsNil()
}
return reflect.DeepEqual(rv.Interface(), reflect.Zero(rv.Type()).Interface())
}