-
Notifications
You must be signed in to change notification settings - Fork 3
/
copyMap.go
212 lines (202 loc) · 5.86 KB
/
copyMap.go
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
/*
* File: copyMap.go
* Created Date: 2022-01-26 06:14:44
* Author: ysj
* Description: copy map to map
*/
package gocopy
import (
"reflect"
)
// copyMap copy map to map
func copyMap(toValue, fromValue reflect.Value, opt *Option) {
fromValue = indirectValue(fromValue)
toValue = indirectValue(toValue)
// type
fromType, _ := indirectType(fromValue.Type())
toType, _ := indirectType(toValue.Type())
// element type, may be ptr
fromElemType := fromType.Elem()
toElemType := toType.Elem()
// avoid tomap is nil or not append
if toValue.IsNil() || !opt.Append {
toNewMap := reflect.MakeMapWithSize(toType, fromValue.Len())
// if toValue.CanSet() {
toValue.Set(toNewMap)
// }
}
// map can't directly assign or convert when append is true:
// because sometimes slice value type of destination also need to keep own elements
// eg. map[type][]int -> map[type][]int{1,2}
// handle respectively every key-value pairs:
// 1. if element can directly assign
if fromElemType.AssignableTo(toElemType) {
kvIter := fromValue.MapRange()
for kvIter.Next() {
fromK := kvIter.Key()
fromV := kvIter.Value()
if !opt.Append {
// not append, just directly set
toValue.SetMapIndex(fromK, fromV)
} else {
// append mode
toVType, elemIsPtr := indirectType(toElemType)
toVKind := toVType.Kind()
switch toVKind {
// slice append slice, need to avoid zero slice
case reflect.Slice:
toV := indirectValue(toValue.MapIndex(fromK))
if !toV.IsValid() { // zero slice
toV = indirectValue(reflect.New(toVType))
}
dest := indirectValue(reflect.New(toVType))
dest.Set(toV)
copySlice(dest, fromV, opt)
if elemIsPtr {
dest = dest.Addr()
}
toValue.SetMapIndex(fromK, dest)
// map set kv, need to avoid nil map
case reflect.Map:
toV := indirectValue(toValue.MapIndex(fromK))
if !toV.IsValid() { // zero map
toV = indirectValue(reflect.New(toVType))
}
dest := indirectValue(reflect.New(toVType))
dest.Set(toV)
copyMap(dest, fromV, opt)
if elemIsPtr {
dest = dest.Addr()
}
toValue.SetMapIndex(fromK, dest)
case reflect.Interface: // like bson.M
toV := indirectValue(toValue.MapIndex(fromK))
if !toV.IsValid() { // zero value
toV = indirectValue(reflect.New(toVType))
}
if toV.IsNil() {
toValue.SetMapIndex(fromK, fromV)
} else {
CopyWithOption(toV.Interface(), fromV.Interface(), opt)
}
default:
toValue.SetMapIndex(fromK, fromV)
}
}
}
// 2. if can not directly assign but can convert
} else if fromElemType.ConvertibleTo(toElemType) {
kvIter := fromValue.MapRange()
for kvIter.Next() {
fromK := kvIter.Key()
fromV := kvIter.Value().Convert(toElemType)
if !opt.Append {
// not append, just directly set converted value
toValue.SetMapIndex(fromK, fromV)
} else {
// append mode
toVType, elemIsPtr := indirectType(toElemType)
toVKind := toVType.Kind()
switch toVKind {
// slice append slice, need to avoid zero slice
case reflect.Slice:
toV := toValue.MapIndex(fromK)
if !toV.IsValid() { // zero slice
toV = indirectValue(reflect.New(fromV.Type()))
}
dest := indirectValue(reflect.New(fromV.Type()))
dest.Set(toV)
copySlice(dest, fromV, opt)
if elemIsPtr {
dest = dest.Addr()
}
toValue.SetMapIndex(fromK, dest)
// map set kv, need to avoid nil map
case reflect.Map:
toV := indirectValue(toValue.MapIndex(fromK))
if !toV.IsValid() { // zero slice
toV = indirectValue(reflect.New(toVType))
}
dest := indirectValue(reflect.New(toVType))
dest.Set(toV)
copyMap(dest, fromV, opt)
if elemIsPtr {
dest = dest.Addr()
}
toValue.SetMapIndex(fromK, dest)
case reflect.Interface: // like bson.M
toV := indirectValue(toValue.MapIndex(fromK))
if !toV.IsValid() { // zero value
toV = indirectValue(reflect.New(toVType))
}
if toV.IsNil() {
toValue.SetMapIndex(fromK, fromV)
} else {
CopyWithOption(toV.Interface(), fromV.Interface(), opt)
}
default:
toValue.SetMapIndex(fromK, fromV)
}
}
}
} else {
// 3. value kind - if can not directly assign or convert
fromElemType, _ := indirectType(fromElemType)
toElemType, elemIsPtr := indirectType(toElemType)
fromElemKind := fromElemType.Kind()
toElemKind := toElemType.Kind()
// a. slice to slice
if toElemKind == reflect.Slice && fromElemKind == reflect.Slice {
kvIter := fromValue.MapRange()
for kvIter.Next() {
fromK := kvIter.Key()
fromV := kvIter.Value() //slice
toV := toValue.MapIndex(fromK)
if !toV.IsValid() {
toV = reflect.New(toElemType)
}
if !elemIsPtr { //toV can't set
dest := indirectValue(reflect.New(toElemType))
dest.Set(indirectValue(toV))
copySlice(dest, fromV, opt)
toValue.SetMapIndex(fromK, dest)
} else { // toV is ptr
copySlice(toV, fromV, opt)
toValue.SetMapIndex(fromK, toV)
}
}
// b. map to map
} else if toElemKind == reflect.Map && fromElemKind == reflect.Map {
kvIter := fromValue.MapRange()
for kvIter.Next() {
fromK := kvIter.Key()
fromV := kvIter.Value() // map
toV := toValue.MapIndex(fromK)
if !toV.IsValid() {
toV = reflect.New(toElemType)
}
copyMap(toV, fromV, opt)
if !elemIsPtr {
toV = indirectValue(toV)
}
toValue.SetMapIndex(fromK, toV)
}
// c. struct to struct
} else if toElemKind == reflect.Struct && fromElemKind == reflect.Struct {
kvIter := fromValue.MapRange()
for kvIter.Next() {
fromK := kvIter.Key()
fromV := kvIter.Value() // struct
toV := toValue.MapIndex(fromK)
if !toV.IsValid() {
toV = reflect.New(toElemType)
}
copyStruct(toV, fromV, opt)
if !elemIsPtr {
toV = indirectValue(toV)
}
toValue.SetMapIndex(fromK, toV)
}
}
}
}