From d62284e9a6b31d9812431d75413611e67607fb12 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Mon, 29 Aug 2022 14:59:36 +0800 Subject: [PATCH] feat: add IsZeroValue function --- docs/validator.md | 32 +++++++++++ docs/validator_zh-CN.md | 28 +++++++++ validator/validator.go | 30 ++++++++++ validator/validator_test.go | 109 ++++++++++++++++++++++++++++++++++++ 4 files changed, 199 insertions(+) diff --git a/docs/validator.md b/docs/validator.md index 333e2e5..dc67edf 100644 --- a/docs/validator.md +++ b/docs/validator.md @@ -47,6 +47,7 @@ import ( - [IsStrongPassword](#IsStrongPassword) - [IsUrl](#IsUrl) - [IsWeakPassword](#IsWeakPassword) +- [IsZeroValue](#IsZeroValue)
@@ -789,6 +790,37 @@ func main() { } ``` +### IsZeroValue +

Checks if passed value is a zero value.

+ +Signature: + +```go +func IsZeroValue(value any) bool +``` +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/validator" +) + +func main() { + fmt.Println(validator.IsZeroValue(nil)) //true + fmt.Println(validator.IsZeroValue(0)) //true + fmt.Println(validator.IsZeroValue("")) //true + fmt.Println(validator.IsZeroValue([]int)) //true + fmt.Println(validator.IsZeroValue(interface{})) //true + + fmt.Println(validator.IsZeroValue("0")) //false + fmt.Println(validator.IsZeroValue("nil")) //false +} +``` + + + + diff --git a/docs/validator_zh-CN.md b/docs/validator_zh-CN.md index de01a0d..868f687 100644 --- a/docs/validator_zh-CN.md +++ b/docs/validator_zh-CN.md @@ -47,6 +47,7 @@ import ( - [IsStrongPassword](#IsStrongPassword) - [IsUrl](#IsUrl) - [IsWeakPassword](#IsWeakPassword) +- [IsZeroValue](#IsZeroValue)
@@ -791,6 +792,33 @@ func main() { +### IsZeroValue +

判断传入的参数值是否为零值

+ +函数签名: + +```go +func IsZeroValue(value any) bool +``` +例子: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/validator" +) + +func main() { + fmt.Println(validator.IsZeroValue(nil)) //true + fmt.Println(validator.IsZeroValue(0)) //true + fmt.Println(validator.IsZeroValue("")) //true + fmt.Println(validator.IsZeroValue([]int)) //true + fmt.Println(validator.IsZeroValue(interface{})) //true + + fmt.Println(validator.IsZeroValue("0")) //false + fmt.Println(validator.IsZeroValue("nil")) //false +} +``` diff --git a/validator/validator.go b/validator/validator.go index 362d8ec..467bc63 100644 --- a/validator/validator.go +++ b/validator/validator.go @@ -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()) +} diff --git a/validator/validator_test.go b/validator/validator_test.go index f9f41f9..5ef7242 100644 --- a/validator/validator_test.go +++ b/validator/validator_test.go @@ -1,7 +1,9 @@ package validator import ( + "fmt" "testing" + "time" "github.com/duke-git/lancet/internal" ) @@ -279,3 +281,110 @@ func TestIsWeakPassword(t *testing.T) { assert.Equal(true, IsWeakPassword("abcABC123")) assert.Equal(false, IsWeakPassword("abc123@#$")) } + +func TestIsZeroValue(t *testing.T) { + assert := internal.NewAssert(t, "TestIsZeroValue") + + var ( + zeroPtr *string + zeroSlice []int + zeroFunc func() string + zeroMap map[string]string + nilIface interface{} + zeroIface fmt.Formatter + ) + zeroValues := []interface{}{ + nil, + false, + 0, + int8(0), + int16(0), + int32(0), + int64(0), + uint(0), + uint8(0), + uint16(0), + uint32(0), + uint64(0), + + 0.0, + float32(0.0), + float64(0.0), + + "", + + // func + zeroFunc, + + // array / slice + [0]int{}, + zeroSlice, + + // map + zeroMap, + + // interface + nilIface, + zeroIface, + + // pointer + zeroPtr, + + // struct + time.Time{}, + } + + for _, value := range zeroValues { + assert.Equal(true, IsZeroValue(value)) + } + + var nonZeroIface fmt.Stringer = time.Now() + + nonZeroValues := []interface{}{ + // bool + true, + + // int + 1, + int8(1), + int16(1), + int32(1), + int64(1), + uint8(1), + uint16(1), + uint32(1), + uint64(1), + + // float + 1.0, + float32(1.0), + float64(1.0), + + // string + "test", + + // func + time.Now, + + // array / slice + []int{}, + []int{42}, + [1]int{42}, + + // map + make(map[string]string, 1), + + // interface + nonZeroIface, + + // pointer + &nonZeroIface, + + // struct + time.Now(), + } + + for _, value := range nonZeroValues { + assert.Equal(false, IsZeroValue(value)) + } +}