-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclone.go
272 lines (231 loc) · 5.28 KB
/
clone.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
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
package genh
import (
"math"
"reflect"
)
var (
structCache SLMap[bool]
cloneCache SLMap[int]
)
type Cloner[T any] interface {
Clone() T
}
func Clone[T any](v T, keepPrivateFields bool) (cp T) {
if v, ok := any(v).(Cloner[T]); ok {
return v.Clone()
}
src, dst := reflect.ValueOf(v), reflect.ValueOf(&cp).Elem()
reflectClone(dst, src, keepPrivateFields, false, false)
return
}
func ReflectClone(dst, src reflect.Value, keepPrivateFields bool) {
reflectClone(dst, src, keepPrivateFields, true, false)
}
func reflectClone(dst, src reflect.Value, keepPrivateFields, checkClone, noMake bool) {
if src.IsZero() {
return
}
if src.Kind() == reflect.Interface {
src = src.Elem()
}
styp := src.Type()
if checkClone {
if idx := isCloner(styp); idx != math.MaxInt {
cloneVal(dst, src, idx)
return
}
}
switch styp.Kind() {
case reflect.Slice:
if src.IsNil() {
return
}
if !noMake {
dst.Set(reflect.MakeSlice(styp, src.Len(), src.Cap()))
}
fallthrough
case reflect.Array:
isIface := styp.Elem().Kind() == reflect.Interface
isPtr := styp.Elem().Kind() == reflect.Pointer
simple := isSimple(styp.Elem().Kind())
hasClone := isPtr && isCloner(styp.Elem().Elem()) != math.MaxInt
for i := 0; i < src.Len(); i++ {
dst, src := dst.Index(i), src.Index(i)
if simple {
dst.Set(src)
continue
}
if isPtr {
if src.IsNil() {
continue
}
src = src.Elem()
ndst := reflect.New(src.Type())
reflectClone(ndst.Elem(), src, keepPrivateFields, hasClone, false)
dst.Set(ndst)
continue
}
if !isIface {
reflectClone(dst, src, keepPrivateFields, true, false)
continue
}
src = src.Elem()
if isSimple(src.Kind()) {
dst.Set(src)
continue
}
dst.Set(maybeCopy(src, keepPrivateFields))
}
case reflect.Map:
if src.IsNil() {
return
}
simpleKey := isSimple(styp.Key().Kind())
simpleValue := isSimple(styp.Elem().Kind())
if !noMake {
dst.Set(reflect.MakeMapWithSize(styp, src.Len()))
}
for it := src.MapRange(); it.Next(); {
var mk, mv reflect.Value
if simpleKey {
mk = it.Key()
} else {
mk = maybeCopy(it.Key(), keepPrivateFields)
}
if simpleValue {
mv = it.Value()
} else {
mv = maybeCopy(it.Value(), keepPrivateFields)
}
dst.SetMapIndex(mk, mv)
}
case reflect.Struct:
if keepPrivateFields {
dst.Set(src) // copy private fields
} else if isSimpleStruct(styp) {
dst.Set(src)
return
} else if dst.IsZero() {
dst.Set(reflect.New(styp).Elem())
}
for i := 0; i < styp.NumField(); i++ {
if f := dst.Field(i); f.CanSet() {
if isSimple(f.Kind()) {
if !keepPrivateFields {
f.Set(src.Field(i))
}
continue
}
f.Set(maybeCopy(src.Field(i), keepPrivateFields))
}
}
case reflect.Ptr:
if src.IsNil() {
return
}
ndst := reflect.New(styp.Elem())
if nde := ndst.Elem(); isSimple(nde.Kind()) {
nde.Set(src.Elem())
} else {
nde.Set(maybeCopy(src.Elem(), keepPrivateFields))
}
dst.Set(ndst)
default:
dst.Set(src)
}
}
func isSimpleStruct(t reflect.Type) bool {
key := t.Name()
return structCache.MustGet(key, func() bool {
for i := 0; i < t.NumField(); i++ {
if !isSimple(t.Field(i).Type.Kind()) {
return false
}
}
return true
})
}
func isSimple(k reflect.Kind) bool {
switch k {
case reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128,
reflect.String:
return true
default:
return false
}
}
func maybeCopy(src reflect.Value, copyPrivate bool) reflect.Value {
if src.Kind() == reflect.Invalid {
var a any = nil
return reflect.Zero(reflect.TypeOf(&a))
}
if src.IsZero() {
return src
}
switch src.Kind() {
case reflect.Slice:
if src.Type().Elem().Kind() == reflect.Uint8 {
b := append([]byte(nil), src.Bytes()...)
return reflect.ValueOf(b)
}
nv := reflect.MakeSlice(src.Type(), src.Len(), src.Cap())
reflectClone(nv, src, copyPrivate, false, true)
return nv
case reflect.Map:
nv := reflect.MakeMapWithSize(src.Type(), src.Len())
reflectClone(nv, src, copyPrivate, false, true)
return nv
case reflect.Ptr, reflect.Array, reflect.Struct:
nv := reflect.New(src.Type()).Elem()
reflectClone(nv, src, copyPrivate, true, false)
return nv
case reflect.Interface:
return maybeCopy(src.Elem(), copyPrivate)
default:
return src
}
}
func isCloner(t reflect.Type) int {
key := t.Name()
return cloneCache.MustGet(key, func() int {
v := math.MaxInt
if idx := cloneIdx(t); idx != math.MaxInt {
v = idx + 1
} else if idx := cloneIdx(reflect.PtrTo(t)); idx != math.MaxInt {
v = -(idx + 1)
}
return v
})
}
func cloneIdx(t reflect.Type) int {
m, ok := t.MethodByName("Clone")
if !ok {
return math.MaxInt
}
if m.Type.NumOut() != 1 {
return math.MaxInt
}
if m.Type.Out(0) != m.Type.In(0) {
return math.MaxInt
}
return m.Index
}
func cloneVal(dst, src reflect.Value, idx int) bool {
if idx == math.MaxInt {
return false
}
var m reflect.Value
if idx > 0 {
m = src.Method(idx - 1)
} else {
m = src.Addr().Method(-idx - 1)
}
v := m.Call(nil)[0]
if v.Kind() == reflect.Ptr && dst.Kind() != reflect.Ptr {
v = v.Elem()
}
dst.Set(v)
return true
}