From 6d61d57b92ee1a22c1d756bd0ff0d40da2a5bd2e Mon Sep 17 00:00:00 2001 From: tiannianshou Date: Thu, 2 Jul 2020 15:03:22 +0800 Subject: [PATCH] Modify StructMapToStruct --- go.mod | 2 +- go.sum | 4 ++-- pkg/util/util.go | 27 ++------------------------- 3 files changed, 5 insertions(+), 28 deletions(-) diff --git a/go.mod b/go.mod index 6fd10ba8..db539b2e 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,6 @@ require ( github.com/LyricTian/captcha v1.1.0 github.com/LyricTian/gzip v0.1.1 github.com/LyricTian/queue v1.2.0 - github.com/LyricTian/structs v1.1.1 github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 github.com/bwmarrin/snowflake v0.3.0 github.com/casbin/casbin/v2 v2.4.1 @@ -27,6 +26,7 @@ require ( github.com/google/gops v0.3.8 github.com/google/uuid v1.1.1 github.com/google/wire v0.4.0 + github.com/jinzhu/copier v0.0.0-20190924061706-b57f9002281a github.com/jinzhu/gorm v1.9.12 github.com/jinzhu/now v1.1.1 // indirect github.com/json-iterator/go v1.1.9 diff --git a/go.sum b/go.sum index ac4bd4d7..8f5d3a0e 100644 --- a/go.sum +++ b/go.sum @@ -10,8 +10,6 @@ github.com/LyricTian/gzip v0.1.1 h1:R8lzUek+tFN4frAXNWOhIKoJaLyFLVOnogBJMBMj9xY= github.com/LyricTian/gzip v0.1.1/go.mod h1:JT7ISZwfIQvoUG2Z/RFjTQA7vBObrGAi9OLXTA+Vycs= github.com/LyricTian/queue v1.2.0 h1:OaEiS5D5dQOMFyvvrq11o0uJdAMXDf5nRJggVDFEonY= github.com/LyricTian/queue v1.2.0/go.mod h1:CMJcrjcVYLOqN2FnVpXqYJzITcDJNdlq7VLkPXyhduw= -github.com/LyricTian/structs v1.1.1 h1:Bn+/o3WgFavzNxXAb+V2xrPsAghda/xXZ6epPkt9a/0= -github.com/LyricTian/structs v1.1.1/go.mod h1:/flA4L8Ltg7IrugE2yV9F9umQrDe9aPBItaO95HNyP4= github.com/PuerkitoBio/purell v1.1.0/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0= github.com/PuerkitoBio/purell v1.1.1 h1:WEQqlqaGbrPkxLJWfBwQmfEAE1Z7ONdDLqrN38tNFfI= github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0= @@ -165,6 +163,8 @@ github.com/google/wire v0.4.0/go.mod h1:ngWDr9Qvq3yZA10YrxfyGELY/AFWGVpy9c1LTRi1 github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= +github.com/jinzhu/copier v0.0.0-20190924061706-b57f9002281a h1:zPPuIq2jAWWPTrGt70eK/BSch+gFAGrNzecsoENgu2o= +github.com/jinzhu/copier v0.0.0-20190924061706-b57f9002281a/go.mod h1:yL958EeXv8Ylng6IfnvG4oflryUi3vgA3xPs9hmII1s= github.com/jinzhu/gorm v1.9.12 h1:Drgk1clyWT9t9ERbzHza6Mj/8FY/CqMyVzOiHviMo6Q= github.com/jinzhu/gorm v1.9.12/go.mod h1:vhTjlKSJUTWNtcbQtrMBFCxy7eXTzeCAzfL5fBZT/Qs= github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= diff --git a/pkg/util/util.go b/pkg/util/util.go index c755b0d3..729f4d7f 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -1,33 +1,10 @@ package util import ( - "reflect" - - "github.com/LyricTian/structs" + "github.com/jinzhu/copier" ) // StructMapToStruct 结构体映射 func StructMapToStruct(s, ts interface{}) error { - if !structs.IsStruct(s) || !structs.IsStruct(ts) { - return nil - } - - ss, tss := structs.New(s), structs.New(ts) - for _, tfield := range tss.Fields() { - if !tfield.IsExported() { - continue - } - - if tfield.IsEmbedded() && tfield.Kind() == reflect.Struct { - for _, tefield := range tfield.Fields() { - if f, ok := ss.FieldOk(tefield.Name()); ok { - tefield.Set2(f.Value()) - } - } - } else if f, ok := ss.FieldOk(tfield.Name()); ok { - tfield.Set2(f.Value()) - } - } - - return nil + return copier.Copy(ts, s) }