Skip to content

Commit

Permalink
Modify StructMapToStruct
Browse files Browse the repository at this point in the history
  • Loading branch information
LyricTian committed Jul 2, 2020
1 parent 9d759ff commit 6d61d57
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 28 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
27 changes: 2 additions & 25 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 6d61d57

Please sign in to comment.