-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcopyStruct2Map.go
More file actions
355 lines (328 loc) · 9.64 KB
/
Copy pathcopyStruct2Map.go
File metadata and controls
355 lines (328 loc) · 9.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
* File: copyStruct2Map.go
* Created Date: 2022-02-16 12:54:13
* Author: ysj
* Description:
*/
package gocopy
import (
"reflect"
"time"
"github.com/iancoleman/strcase"
)
var caseFunc = map[string]func(s string) string{
"Camel": strcase.ToCamel,
"LowerCamel": strcase.ToLowerCamel,
"Snake": strcase.ToSnake,
"ScreamingSnake": strcase.ToScreamingSnake,
"Kebab": strcase.ToKebab,
"ScreamingKebab": strcase.ToScreamingKebab,
}
// copyStruct2Map copy struct to map
func copyStruct2Map(toValue, fromValue reflect.Value, opt *Option) {
fromValue = indirectValue(fromValue)
toValue = indirectValue(toValue)
// type
fromType, _ := indirectType(fromValue.Type())
toType, _ := indirectType(toValue.Type())
fromFields := deepFields(fromType)
// avoid tomap is nil or not append
if toValue.IsNil() || !opt.Append {
toNewMap := reflect.MakeMapWithSize(toType, len(fromFields))
toValue.Set(toNewMap)
}
for i := 0; i < len(fromFields); i++ {
fromField := fromFields[i]
// ignore field to skip copy
if level, ok := opt.ignoreFields[fromField.Name]; ok {
opt.ignoreFields[fromField.Name]++
if level <= opt.IgnoreLevel {
continue
}
}
// field case
if opt.ToCase == "" {
opt.ToCase = "LowerCamel"
}
toFieldName := caseFunc[opt.ToCase](fromField.Name)
// the specified toName has the highest priority
toName, ok := opt.NameFromTo[fromField.Name]
if ok {
toFieldName = toName
}
// target field name
toKey := reflect.ValueOf(toFieldName)
// whether ignore zero value
fromFV := fromValue.FieldByName(fromField.Name)
if fromFV.IsZero() && opt.IgnoreZero {
continue
}
// ignore invalid source field
fromFieldValue := indirectValue(fromFV)
if !fromFieldValue.IsValid() {
continue
}
fromFieldType, fromVIsPtr := indirectType(fromFV.Type())
toV := indirectValue(toValue.MapIndex(toKey))
toVType := fromFieldType
toVIsPtr := fromVIsPtr
if toV.IsValid() {
toVType = reflect.TypeOf(toV.Interface())
toVType, toVIsPtr = indirectType(toVType)
}
// convert field value by customized func
if convertFunc, ok := opt.Converters[fromField.Name]; ok {
convertValue := convertFunc(fromFV.Interface())
if toVIsPtr && toV.IsValid() {
toFV := indirectValue(reflect.New(toVType))
toFV.Set(reflect.ValueOf(convertValue))
toValue.SetMapIndex(toKey, toFV.Addr())
} else {
// sometimes convertValue maybe nil
if convertValue == nil {
toValue.SetMapIndex(toKey, reflect.Zero(toValue.Type()))
} else {
toValue.SetMapIndex(toKey, reflect.ValueOf(convertValue))
}
}
continue
}
// specially handle time.Time to string and vice versa
if timeFieldMap, ok := opt.TimeToString[fromField.Name]; ok {
timeString := ""
if timeFieldMap == nil {
location, err := time.LoadLocation(defaultTimeLoc)
if err != nil {
panic(err)
}
timeString = fromFieldValue.Interface().(time.Time).In(location).Format(defaultTimeLayout)
} else {
loc, ok := timeFieldMap["loc"]
if !ok {
loc = defaultTimeLoc
}
layout, ok := timeFieldMap["layout"]
if !ok {
layout = defaultTimeLayout
}
location, err := time.LoadLocation(loc)
if err != nil {
panic(err)
}
timeString = fromFieldValue.Interface().(time.Time).In(location).Format(layout)
}
if toVIsPtr {
toFV := indirectValue(reflect.New(toVType))
toFV.Set(reflect.ValueOf(timeString))
toValue.SetMapIndex(toKey, toFV.Addr())
} else {
toValue.SetMapIndex(toKey, reflect.ValueOf(timeString))
}
continue
}
if stringFieldMap, ok := opt.StringToTime[fromField.Name]; ok {
if fromFieldValue.IsZero() { // ""
continue
}
var timeTime = time.Now()
if stringFieldMap == nil {
location, err := time.LoadLocation(defaultTimeLoc)
if err != nil {
panic(err)
}
timeTime, err = time.ParseInLocation(defaultTimeLayout, fromFieldValue.Interface().(string), location)
if err != nil {
panic(err)
}
} else {
loc, ok := stringFieldMap["loc"]
if !ok {
loc = defaultTimeLoc
}
layout, ok := stringFieldMap["layout"]
if !ok {
layout = defaultTimeLayout
}
location, err := time.LoadLocation(loc)
if err != nil {
panic(err)
}
timeTime, err = time.ParseInLocation(layout, fromFieldValue.Interface().(string), location)
if err != nil {
panic(err)
}
}
if toVIsPtr {
toFV := indirectValue(reflect.New(toVType))
toFV.Set(reflect.ValueOf(timeTime))
toValue.SetMapIndex(toKey, toFV.Addr())
} else {
toValue.SetMapIndex(toKey, reflect.ValueOf(timeTime))
}
continue
}
switch fromFieldValue.Kind() {
// slice to slice, need to avoid zero slice
case reflect.Slice:
fromElemType, _ := indirectType(fromFieldType.Elem())
toElemType, elemIsPtr := indirectType(toVType.Elem())
fromElemIsStruct := fromElemType.Kind() == reflect.Struct
toElemIsStruct := toElemType.Kind() == reflect.Struct
targetType := toVType
if fromElemIsStruct || toElemIsStruct {
if elemIsPtr {
targetType = reflect.SliceOf(reflect.New(toType).Type())
// slice of struct to slice of map
} else {
targetType = reflect.SliceOf(toType)
}
}
if !toV.IsValid() { // zero slice
toV = indirectValue(reflect.New(targetType))
// toVType is slice of struct, need to transfrom every elem to map/*map
} else if toElemIsStruct {
toSliceOfMap := indirectValue(reflect.New(targetType))
toV = reflect.ValueOf(toV.Interface())
for i := 0; i < toV.Len(); i++ {
if !toV.Index(i).IsValid() {
continue
}
toElemV := reflect.New(toType) // map
copyStruct2Map(toElemV, toV.Index(i), opt)
if !elemIsPtr {
toElemV = indirectValue(toElemV)
}
toSliceOfMap.Set(reflect.Append(toSliceOfMap, toElemV))
}
toV = toSliceOfMap
}
dest := indirectValue(reflect.New(targetType)) // slice
dest.Set(indirectValue(reflect.ValueOf(toV.Interface()))) // slice append slice
if !fromElemIsStruct {
copySlice(dest, fromFieldValue, opt)
} else {
for i := 0; i < fromFieldValue.Len(); i++ {
if !fromFieldValue.Index(i).IsValid() {
continue
}
toElemValue := reflect.New(toType) // map
copyStruct2Map(toElemValue, fromFieldValue.Index(i), opt)
if !elemIsPtr {
toElemValue = indirectValue(toElemValue)
}
dest.Set(reflect.Append(dest, toElemValue))
}
}
if toVIsPtr {
dest = dest.Addr()
}
toValue.SetMapIndex(toKey, dest)
// map set kv, need to avoid nil map
case reflect.Map:
fromElemType, _ := indirectType(fromFieldType.Elem())
toElemType, elemIsPtr := indirectType(toVType.Elem())
fromElemIsStruct := fromElemType.Kind() == reflect.Struct
toElemIsStruct := toElemType.Kind() == reflect.Struct
if !toV.IsValid() { // zero map
toV = indirectValue(reflect.New(toType))
if toV.IsNil() { // nil map
toNewV := reflect.MakeMapWithSize(toType, toV.Len())
toV.Set(toNewV)
}
} else if toElemIsStruct {
toMap := indirectValue(reflect.New(toType))
toV = reflect.ValueOf(toV.Interface())
if toMap.IsNil() {
toNewMap := reflect.MakeMapWithSize(toType, toV.Len())
toMap.Set(toNewMap)
}
toVIter := toV.MapRange()
for toVIter.Next() {
toVK := toVIter.Key()
toVV := toVIter.Value() // struct
toElemValue := reflect.New(toType) // map
copyStruct2Map(toElemValue, toVV, opt)
if !elemIsPtr {
toElemValue = indirectValue(toElemValue)
}
toMap.SetMapIndex(toVK, toElemValue)
}
toV = toMap
}
dest := indirectValue(reflect.New(toType))
// avoid tomap is nil
if dest.IsNil() {
toV = reflect.ValueOf(toV.Interface())
toNewDest := reflect.MakeMapWithSize(toType, (toV.Len() + fromFieldValue.Len()))
dest.Set(toNewDest)
}
toVIter := toV.MapRange()
for toVIter.Next() {
toK := toVIter.Key()
toV := toVIter.Value()
dest.SetMapIndex(toK, toV)
}
if !fromElemIsStruct {
copyMap(dest, fromFieldValue, opt)
} else {
fromKVIter := fromFieldValue.MapRange()
for fromKVIter.Next() {
fromK := fromKVIter.Key()
fromV := fromKVIter.Value() // struct
toElemValue := reflect.New(toType) // map
copyStruct2Map(toElemValue, fromV, opt)
if !elemIsPtr {
toElemValue = indirectValue(toElemValue)
}
dest.SetMapIndex(fromK, toElemValue)
}
}
if toVIsPtr {
dest = dest.Addr()
}
toValue.SetMapIndex(toKey, dest)
case reflect.Struct:
// if time.Time field
_, ok := fromFieldValue.Interface().(time.Time)
if ok {
if toVIsPtr {
toV = indirectValue(reflect.New(toVType))
toV.Set(fromFieldValue)
toV = toV.Addr()
toValue.SetMapIndex(toKey, toV)
} else {
toValue.SetMapIndex(toKey, fromFieldValue)
}
continue
}
// other struct field
if !toV.IsValid() { // zero map
toV = indirectValue(reflect.New(toType))
} else if toVType.Kind() == reflect.Struct {
toNewV := reflect.New(toType) // map
copyStruct2Map(toNewV, reflect.ValueOf(toV.Interface()), opt)
toV = toNewV
} else if toVType.Kind() == reflect.Map {
toNewV := reflect.New(toType) // map
copyMap(toNewV, reflect.ValueOf(toV.Interface()), opt)
toV = toNewV
}
dest := indirectValue(reflect.New(toType))
dest.Set(indirectValue(reflect.ValueOf(toV.Interface())))
copyStruct2Map(dest, fromFieldValue, opt)
if toVIsPtr {
dest = dest.Addr()
}
toValue.SetMapIndex(toKey, dest)
default:
if toVIsPtr {
toV = indirectValue(reflect.New(toVType))
toV.Set(fromFieldValue)
toV = toV.Addr()
toValue.SetMapIndex(toKey, toV)
} else {
toValue.SetMapIndex(toKey, fromFieldValue)
}
}
}
}