From d4d68dc263acddf8a2902f236737fe6e7e118832 Mon Sep 17 00:00:00 2001 From: charlie <3140647@qq.com> Date: Fri, 13 May 2022 14:19:44 +0800 Subject: [PATCH] isZero --- go.mod | 2 ++ go.sum | 4 ++++ structs/options.go | 4 +--- structs/struct_test.go | 21 ++++++++++++++++++++- structs/structs.go | 21 +++++++++++++++++++++ 5 files changed, 48 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index 52c441b..6f53c25 100644 --- a/go.mod +++ b/go.mod @@ -26,10 +26,12 @@ require ( github.com/coocood/freecache v1.2.1 github.com/go-playground/assert/v2 v2.0.1 github.com/go-redis/redis/v8 v8.11.5 + github.com/pkg/errors v0.9.1 github.com/sirupsen/logrus v1.8.1 github.com/stretchr/testify v1.7.1 golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 // indirect golang.org/x/exp v0.0.0-20220414153411-bcd21879b8fd + golang.org/x/sync v0.0.0-20210220032951-036812b2e83c golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect gopkg.in/natefinch/lumberjack.v2 v2.0.0 diff --git a/go.sum b/go.sum index c23d8e9..47053d0 100644 --- a/go.sum +++ b/go.sum @@ -53,6 +53,8 @@ github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjY github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE= github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE= github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= @@ -91,6 +93,8 @@ golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAG golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/structs/options.go b/structs/options.go index 372e84b..6b31504 100644 --- a/structs/options.go +++ b/structs/options.go @@ -41,9 +41,7 @@ func DeepCopy() optionFunc { } func SkipField(field string) optionFunc { - return func(o *option) { - o.SkipFields = append(o.SkipFields, field) - } + return SkipFields([]string{field}) } func SkipFields(fields []string) optionFunc { diff --git a/structs/struct_test.go b/structs/struct_test.go index eccc435..8bdca4c 100644 --- a/structs/struct_test.go +++ b/structs/struct_test.go @@ -5,7 +5,7 @@ import ( "testing" "github.com/charlienet/go-mixed/structs" - "github.com/go-playground/assert/v2" + "github.com/stretchr/testify/assert" ) func TestNew(t *testing.T) { @@ -19,3 +19,22 @@ func TestNew(t *testing.T) { t.Log(s.Names()) t.Log(s.Values()) } + +func TestIsZero(t *testing.T) { + var v1 int + assert.True(t, structs.IsZero(v1)) + + var v2 = struct { + Msg string + }{} + assert.True(t, structs.IsZero(v2)) + + var v3 = struct { + VV int + Msg string + }{Msg: "abc"} + assert.False(t, structs.IsZero(v3)) + + v3.Msg = "" + assert.True(t, structs.IsZero(v3)) +} diff --git a/structs/structs.go b/structs/structs.go index 2c29966..dc8632f 100644 --- a/structs/structs.go +++ b/structs/structs.go @@ -53,6 +53,17 @@ func (s *Struct) Values() []any { return values } +func (s *Struct) IsZero() bool { + for _, fi := range s.fields { + source := s.value.FieldByName(fi.name) + if !source.IsZero() { + return false + } + } + + return true +} + func (s *Struct) ToMap() map[string]any { m := make(map[string]any, len(s.fields)) for _, fi := range s.fields { @@ -112,6 +123,16 @@ func Map2Struct(o map[string]any, dst any) { } +func IsZero(o any) bool { + v := reflect.ValueOf(o) + if v.IsZero() { + return true + } + + s := New(o) + return s.IsZero() +} + func Struct2Map(o any, opts ...optionFunc) map[string]any { return New(o, opts...).ToMap() }