mirror of
https://github.com/charlienet/go-mixed.git
synced 2025-07-18 08:32:40 +08:00
isZero
This commit is contained in:
2
go.mod
2
go.mod
@ -26,10 +26,12 @@ require (
|
|||||||
github.com/coocood/freecache v1.2.1
|
github.com/coocood/freecache v1.2.1
|
||||||
github.com/go-playground/assert/v2 v2.0.1
|
github.com/go-playground/assert/v2 v2.0.1
|
||||||
github.com/go-redis/redis/v8 v8.11.5
|
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/sirupsen/logrus v1.8.1
|
||||||
github.com/stretchr/testify v1.7.1
|
github.com/stretchr/testify v1.7.1
|
||||||
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 // indirect
|
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 // indirect
|
||||||
golang.org/x/exp v0.0.0-20220414153411-bcd21879b8fd
|
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
|
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect
|
||||||
gopkg.in/natefinch/lumberjack.v2 v2.0.0
|
gopkg.in/natefinch/lumberjack.v2 v2.0.0
|
||||||
|
|
||||||
|
4
go.sum
4
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/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
|
||||||
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
|
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
|
||||||
github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE=
|
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 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
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=
|
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-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-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-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-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-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
@ -41,9 +41,7 @@ func DeepCopy() optionFunc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func SkipField(field string) optionFunc {
|
func SkipField(field string) optionFunc {
|
||||||
return func(o *option) {
|
return SkipFields([]string{field})
|
||||||
o.SkipFields = append(o.SkipFields, field)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func SkipFields(fields []string) optionFunc {
|
func SkipFields(fields []string) optionFunc {
|
||||||
|
@ -5,7 +5,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/charlienet/go-mixed/structs"
|
"github.com/charlienet/go-mixed/structs"
|
||||||
"github.com/go-playground/assert/v2"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestNew(t *testing.T) {
|
func TestNew(t *testing.T) {
|
||||||
@ -19,3 +19,22 @@ func TestNew(t *testing.T) {
|
|||||||
t.Log(s.Names())
|
t.Log(s.Names())
|
||||||
t.Log(s.Values())
|
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))
|
||||||
|
}
|
||||||
|
@ -53,6 +53,17 @@ func (s *Struct) Values() []any {
|
|||||||
return values
|
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 {
|
func (s *Struct) ToMap() map[string]any {
|
||||||
m := make(map[string]any, len(s.fields))
|
m := make(map[string]any, len(s.fields))
|
||||||
for _, fi := range 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 {
|
func Struct2Map(o any, opts ...optionFunc) map[string]any {
|
||||||
return New(o, opts...).ToMap()
|
return New(o, opts...).ToMap()
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user