-
Notifications
You must be signed in to change notification settings - Fork 0
/
deepcopy.go
289 lines (246 loc) · 6.72 KB
/
deepcopy.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// package deepcopy makes deep copies of somethings: unexported field values are not copied.
package deepcopy
import (
"fmt"
"reflect"
"sync"
"time"
)
// DeepClone returns a deep copy of whatever is passed to it and returns the copy
// in an any. The returned value will need to be asserted to the correct type.
//
// DeepClone calls one of methods "Clone() *T" or "Clone() T"
// to delegating copy process to type.
func DeepClone(src any) any {
srcType := reflect.TypeOf(src)
if reflect.Pointer == srcType.Kind() {
srcType = srcType.Elem()
}
dst := reflect.New(srcType)
switch dst.Elem().Kind() {
case reflect.Slice:
dst.Elem().Set(reflect.MakeSlice(dst.Type().Elem(), 0, 0))
case reflect.Map:
dst.Elem().Set(reflect.MakeMap(dst.Elem().Type()))
}
DeepCopy(dst.Interface(), src)
if timeType == dst.Elem().Type() {
return dst.Elem().Interface()
}
switch dst.Elem().Kind() {
case reflect.Interface:
fallthrough
case reflect.Struct:
return dst.Interface()
case reflect.Slice:
fallthrough
case reflect.Map:
fallthrough
default:
return dst.Elem().Interface()
}
}
// DeepCopy copies the contents of src into dst
// See DeepClone function's documentation for more information.
func DeepCopy(dst, src any) {
dstv := reflect.ValueOf(dst)
srcv := reflect.ValueOf(src)
if reflect.Pointer != dstv.Kind() || dstv.IsNil() {
panic("dst not non-nil pointer")
}
if reflect.Pointer == srcv.Kind() && srcv.IsNil() {
panic("src is nil pointer")
}
if reflect.Pointer == srcv.Kind() {
if dt, st := dstv.Type().Elem(), srcv.Type().Elem(); dt != st {
panic(fmt.Sprintf("type mistmatch %s != %s", dt, st))
}
if srcv.Interface() == dstv.Interface() {
return
}
srcv = srcv.Elem()
} else if dt, st := dstv.Type().Elem(), srcv.Type(); dt != st {
panic(fmt.Sprintf("type mistmatch %s != %s", dt, st))
}
c, put := newCopyState()
defer put()
c.deepValueCopy(dstv.Elem(), srcv)
}
type copyState struct {
// Keep track of what pointers we've seen in the current recursive call
// path, to avoid cycles that could lead to a stack overflow. Only do
// the relatively expensive map operations if ptrLevel is larger than
// startDetectingCyclesAfter, so that we skip the work if we're within a
// reasonable amount of nested pointers deep.
ptrLevel uint
ptrSeen map[any]struct{}
}
const startDetectingCyclesAfter = 1000
var copyStatePool sync.Pool
func newCopyState() (c *copyState, put func()) {
if v := copyStatePool.Get(); v != nil {
c = v.(*copyState)
if len(c.ptrSeen) > 0 {
panic("ptrEncoder.encode should have emptied ptrSeen via defers")
}
} else {
c = ©State{ptrSeen: make(map[any]struct{})}
}
put = func() { copyStatePool.Put(c) }
return c, put
}
type methodType struct {
method reflect.Method
indirect bool
}
func (m methodType) IsValid() bool {
return m.method.Func.IsValid()
}
func typeMethod(t reflect.Type) methodType {
method, ok := t.MethodByName("Clone")
if !ok {
return methodType{}
}
mt := method.Type
if mt.NumIn() == 1 && mt.NumOut() == 1 {
switch retType := mt.Out(0); t {
case retType:
fallthrough
case reflect.PointerTo(retType):
return methodType{method: method}
}
switch retType := mt.Out(0); retType {
case t:
fallthrough
case reflect.PointerTo(t):
return methodType{method: method}
}
}
return methodType{}
}
var methodCache sync.Map // map[reflect.Type]methodType
// cachedTypeMethod is like typeMethod but uses a cache to avoid repeated work.
func cachedTypeMethod(t reflect.Type) methodType {
if f, ok := methodCache.Load(t); ok {
return f.(methodType)
}
f, _ := methodCache.LoadOrStore(t, typeMethod(t))
return f.(methodType)
}
func tryInvokeCloneMethod(dst, src reflect.Value) bool {
method := cachedTypeMethod(src.Type())
if method.IsValid() {
ret := method.method.Func.Call([]reflect.Value{src})[0]
switch dst.Type() {
case ret.Type():
case reflect.PointerTo(ret.Type()):
newRet := reflect.New(ret.Type()).Elem()
newRet.Set(ret)
ret = newRet.Addr()
}
switch ret.Type() {
case dst.Type():
case reflect.PointerTo(dst.Type()):
ret = ret.Elem()
}
dst.Set(ret)
return true
}
return false
}
var timeType = reflect.TypeOf(time.Time{})
func (c *copyState) deepValueCopy(dst, src reflect.Value) {
if dst.Type() != src.Type() {
panic(fmt.Sprintf("type mistmatch %s != %s", dst.Type(), src.Type()))
}
if src.Kind() != reflect.Pointer && src.CanAddr() {
if tryInvokeCloneMethod(dst, src.Addr()) {
return
}
}
if tryInvokeCloneMethod(dst, src) {
return
}
switch sk := src.Kind(); sk {
case reflect.Pointer, reflect.Slice, reflect.Map:
if src.IsNil() {
dst.SetZero()
return
}
if c.ptrLevel++; c.ptrLevel > startDetectingCyclesAfter {
var ptr any
switch sk {
case reflect.Pointer:
// We're a large number of nested ptrEncoder.encode calls deep;
// start checking if we've run into a pointer cycle.
ptr = src.Interface()
case reflect.Slice:
// We're a large number of nested ptrEncoder.encode calls deep;
// start checking if we've run into a pointer cycle.
// Here we use a struct to memorize the pointer to the first element of the slice
// and its length.
ptr = struct {
ptr any // always an unsafe.Pointer, but avoids a dependency on package unsafe
len int
}{src.UnsafePointer(), src.Len()}
case reflect.Map:
// We're a large number of nested ptrEncoder.encode calls deep;
// start checking if we've run into a pointer cycle.
ptr = src.UnsafePointer()
}
if _, ok := c.ptrSeen[ptr]; ok {
return
}
c.ptrSeen[ptr] = struct{}{}
defer delete(c.ptrSeen, ptr)
}
}
switch sk := src.Kind(); sk {
case reflect.Interface:
if src.IsNil() {
dst.SetZero()
return
}
srcElem := src.Elem()
dstElem := reflect.New(srcElem.Type()).Elem()
c.deepValueCopy(dstElem, srcElem)
dst.Set(dstElem)
case reflect.Struct:
if timeType == src.Type() {
dst.Set(src)
return
}
for i := 0; i < src.NumField(); i++ {
if !dst.Field(i).CanSet() {
continue
}
c.deepValueCopy(dst.Field(i), src.Field(i))
}
case reflect.Map:
dst.Set(reflect.MakeMapWithSize(src.Type(), src.Len()))
var mapElem reflect.Value
for mi := src.MapRange(); mi.Next(); {
if !mapElem.IsValid() {
mapElem = reflect.New(src.Type().Elem()).Elem()
} else {
mapElem.SetZero()
}
c.deepValueCopy(mapElem, mi.Value())
dst.SetMapIndex(mi.Key(), mapElem)
}
case reflect.Slice, reflect.Array:
if reflect.Slice == sk {
dst.Set(reflect.MakeSlice(src.Type(), src.Len(), src.Len()))
}
for i := 0; i < src.Len(); i++ {
c.deepValueCopy(dst.Index(i), src.Index(i))
}
case reflect.Pointer:
if dst.IsNil() {
dst.Set(reflect.New(src.Type().Elem()))
}
c.deepValueCopy(dst.Elem(), src.Elem())
default:
dst.Set(src)
}
}