Skip to content

Commit 6d61d57

Browse files
committed
Modify StructMapToStruct
1 parent 9d759ff commit 6d61d57

File tree

3 files changed

+5
-28
lines changed

3 files changed

+5
-28
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ require (
66
github.com/LyricTian/captcha v1.1.0
77
github.com/LyricTian/gzip v0.1.1
88
github.com/LyricTian/queue v1.2.0
9-
github.com/LyricTian/structs v1.1.1
109
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
1110
github.com/bwmarrin/snowflake v0.3.0
1211
github.com/casbin/casbin/v2 v2.4.1
@@ -27,6 +26,7 @@ require (
2726
github.com/google/gops v0.3.8
2827
github.com/google/uuid v1.1.1
2928
github.com/google/wire v0.4.0
29+
github.com/jinzhu/copier v0.0.0-20190924061706-b57f9002281a
3030
github.com/jinzhu/gorm v1.9.12
3131
github.com/jinzhu/now v1.1.1 // indirect
3232
github.com/json-iterator/go v1.1.9

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ github.com/LyricTian/gzip v0.1.1 h1:R8lzUek+tFN4frAXNWOhIKoJaLyFLVOnogBJMBMj9xY=
1010
github.com/LyricTian/gzip v0.1.1/go.mod h1:JT7ISZwfIQvoUG2Z/RFjTQA7vBObrGAi9OLXTA+Vycs=
1111
github.com/LyricTian/queue v1.2.0 h1:OaEiS5D5dQOMFyvvrq11o0uJdAMXDf5nRJggVDFEonY=
1212
github.com/LyricTian/queue v1.2.0/go.mod h1:CMJcrjcVYLOqN2FnVpXqYJzITcDJNdlq7VLkPXyhduw=
13-
github.com/LyricTian/structs v1.1.1 h1:Bn+/o3WgFavzNxXAb+V2xrPsAghda/xXZ6epPkt9a/0=
14-
github.com/LyricTian/structs v1.1.1/go.mod h1:/flA4L8Ltg7IrugE2yV9F9umQrDe9aPBItaO95HNyP4=
1513
github.com/PuerkitoBio/purell v1.1.0/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
1614
github.com/PuerkitoBio/purell v1.1.1 h1:WEQqlqaGbrPkxLJWfBwQmfEAE1Z7ONdDLqrN38tNFfI=
1715
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
165163
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
166164
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
167165
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
166+
github.com/jinzhu/copier v0.0.0-20190924061706-b57f9002281a h1:zPPuIq2jAWWPTrGt70eK/BSch+gFAGrNzecsoENgu2o=
167+
github.com/jinzhu/copier v0.0.0-20190924061706-b57f9002281a/go.mod h1:yL958EeXv8Ylng6IfnvG4oflryUi3vgA3xPs9hmII1s=
168168
github.com/jinzhu/gorm v1.9.12 h1:Drgk1clyWT9t9ERbzHza6Mj/8FY/CqMyVzOiHviMo6Q=
169169
github.com/jinzhu/gorm v1.9.12/go.mod h1:vhTjlKSJUTWNtcbQtrMBFCxy7eXTzeCAzfL5fBZT/Qs=
170170
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=

pkg/util/util.go

+2-25
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,10 @@
11
package util
22

33
import (
4-
"reflect"
5-
6-
"github.com/LyricTian/structs"
4+
"github.com/jinzhu/copier"
75
)
86

97
// StructMapToStruct 结构体映射
108
func StructMapToStruct(s, ts interface{}) error {
11-
if !structs.IsStruct(s) || !structs.IsStruct(ts) {
12-
return nil
13-
}
14-
15-
ss, tss := structs.New(s), structs.New(ts)
16-
for _, tfield := range tss.Fields() {
17-
if !tfield.IsExported() {
18-
continue
19-
}
20-
21-
if tfield.IsEmbedded() && tfield.Kind() == reflect.Struct {
22-
for _, tefield := range tfield.Fields() {
23-
if f, ok := ss.FieldOk(tefield.Name()); ok {
24-
tefield.Set2(f.Value())
25-
}
26-
}
27-
} else if f, ok := ss.FieldOk(tfield.Name()); ok {
28-
tfield.Set2(f.Value())
29-
}
30-
}
31-
32-
return nil
9+
return copier.Copy(ts, s)
3310
}

0 commit comments

Comments
 (0)