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

feat: add IsZeroValue function

This commit is contained in:
dudaodong
2022-08-27 18:57:45 +08:00
parent 9239bcfdc3
commit fc10689b25
2 changed files with 140 additions and 1 deletions

View File

@@ -8,6 +8,7 @@ import (
"encoding/json"
"net"
"net/url"
"reflect"
"regexp"
"strconv"
"strings"
@@ -159,7 +160,7 @@ func IsEmail(email string) bool {
return isEmailRegexMatcher.MatchString(email)
}
var isChineseMobileRegexMatcher *regexp.Regexp = regexp.MustCompile("^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\\d{8}$")
var isChineseMobileRegexMatcher *regexp.Regexp = regexp.MustCompile(`^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\\d{8}$`)
// IsChineseMobile check if the string is chinese mobile number.
func IsChineseMobile(mobileNum string) bool {
@@ -253,3 +254,32 @@ func IsWeakPassword(password string) bool {
return (num || letter) && !special
}
// IsZeroValue checks if value is a zero value
func IsZeroValue(value any) 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())
}