forked from brunoga/deep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeep.go
228 lines (188 loc) · 5.83 KB
/
deep.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
package deep
import (
"fmt"
"reflect"
)
// Copy creates a deep copy of src. It returns the copy and a nil error in case
// of success and the zero value for the type and a non-nil error on failure.
func Copy[T any](src T) (T, error) {
return copy(src, false)
}
// CopySkipUnsupported creates a deep copy of src. It returns the copy and a nil
// errorin case of success and the zero value for the type and a non-nil error
// on failure. Unsupported types are skipped (the copy will have the zero value
// for the type) instead of returning an error.
func CopySkipUnsupported[T any](src T) (T, error) {
return copy(src, true)
}
// MustCopy creates a deep copy of src. It returns the copy on success or panics
// in case of any failure.
func MustCopy[T any](src T) T {
dst, err := copy(src, false)
if err != nil {
panic(err)
}
return dst
}
func copy[T any](src T, skipUnsupported bool) (T, error) {
v := reflect.ValueOf(src)
// We might have a zero value, so we check for this here otherwise
// calling interface below will panic.
if v.Kind() == reflect.Invalid {
// This amounts to returning the zero value for T.
var t T
return t, nil
}
dst, err := recursiveCopy(v, make(map[uintptr]reflect.Value),
skipUnsupported)
if err != nil {
var t T
return t, err
}
return dst.Interface().(T), nil
}
func recursiveCopy(v reflect.Value, pointers map[uintptr]reflect.Value,
skipUnsupported bool) (reflect.Value, error) {
switch v.Kind() {
case reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32,
reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32,
reflect.Uint64, reflect.Uintptr, reflect.Float32, reflect.Float64,
reflect.Complex64, reflect.Complex128, reflect.String:
// Direct type, just copy it.
return v, nil
case reflect.Array:
return recursiveCopyArray(v, pointers, skipUnsupported)
case reflect.Interface:
return recursiveCopyInterface(v, pointers, skipUnsupported)
case reflect.Map:
return recursiveCopyMap(v, pointers, skipUnsupported)
case reflect.Ptr:
return recursiveCopyPtr(v, pointers, skipUnsupported)
case reflect.Slice:
return recursiveCopySlice(v, pointers, skipUnsupported)
case reflect.Struct:
return recursiveCopyStruct(v, pointers, skipUnsupported)
case reflect.Func, reflect.Chan, reflect.UnsafePointer:
if v.IsNil() {
// If we have a nil function, unsafe pointer or channel, then we
// can copy it.
return v, nil
} else {
if skipUnsupported {
return reflect.Zero(v.Type()), nil
} else {
return reflect.Value{}, fmt.Errorf("unsuported non-nil value for type: %s", v.Type())
}
}
default:
if skipUnsupported {
return reflect.Zero(v.Type()), nil
} else {
return reflect.Value{}, fmt.Errorf("unsuported type: %s", v.Type())
}
}
}
func recursiveCopyArray(v reflect.Value, pointers map[uintptr]reflect.Value,
skipUnsupported bool) (reflect.Value, error) {
dst := reflect.New(v.Type()).Elem()
for i := 0; i < v.Len(); i++ {
elem := v.Index(i)
elemDst, err := recursiveCopy(elem, pointers, skipUnsupported)
if err != nil {
return reflect.Value{}, err
}
dst.Index(i).Set(elemDst)
}
return dst, nil
}
func recursiveCopyInterface(v reflect.Value, pointers map[uintptr]reflect.Value,
skipUnsupported bool) (reflect.Value, error) {
if v.IsNil() {
// If the interface is nil, just return it.
return v, nil
}
return recursiveCopy(v.Elem(), pointers, skipUnsupported)
}
func recursiveCopyMap(v reflect.Value, pointers map[uintptr]reflect.Value,
skipUnsupported bool) (reflect.Value, error) {
if v.IsNil() {
// If the slice is nil, just return it.
return v, nil
}
dst := reflect.MakeMap(v.Type())
for _, key := range v.MapKeys() {
elem := v.MapIndex(key)
elemDst, err := recursiveCopy(elem, pointers,
skipUnsupported)
if err != nil {
return reflect.Value{}, err
}
dst.SetMapIndex(key, elemDst)
}
return dst, nil
}
func recursiveCopyPtr(v reflect.Value, pointers map[uintptr]reflect.Value,
skipUnsupported bool) (reflect.Value, error) {
// If the pointer is nil, just return it.
if v.IsNil() {
return v, nil
}
// If the pointer is already in the pointers map, return it.
ptr := v.Pointer()
if dst, ok := pointers[ptr]; ok {
return dst, nil
}
// Otherwise, create a new pointer and add it to the pointers map.
dst := reflect.New(v.Type().Elem())
pointers[ptr] = dst
// Proceed with the copy.
elem := v.Elem()
elemDst, err := recursiveCopy(elem, pointers, skipUnsupported)
if err != nil {
return reflect.Value{}, err
}
dst.Elem().Set(elemDst)
return dst, nil
}
func recursiveCopySlice(v reflect.Value, pointers map[uintptr]reflect.Value,
skipUnsupported bool) (reflect.Value, error) {
if v.IsNil() {
// If the slice is nil, just return it.
return v, nil
}
dst := reflect.MakeSlice(v.Type(), v.Len(), v.Cap())
for i := 0; i < v.Len(); i++ {
elem := v.Index(i)
elemDst, err := recursiveCopy(elem, pointers,
skipUnsupported)
if err != nil {
return reflect.Value{}, err
}
dst.Index(i).Set(elemDst)
}
return dst, nil
}
func recursiveCopyStruct(v reflect.Value, pointers map[uintptr]reflect.Value,
skipUnsupported bool) (reflect.Value, error) {
dst := reflect.New(v.Type()).Elem()
for i := 0; i < v.NumField(); i++ {
elem := v.Field(i)
// If the field is unexported, we need to disable read-only mode. If it
// is exported, doing this changes nothing so we just do it. We need to
// do this here not because we are writting to the field (this is the
// source), but because Interface() does not work if the read-only bits
// are set.
disableRO(&elem)
elemDst, err := recursiveCopy(elem, pointers,
skipUnsupported)
if err != nil {
return reflect.Value{}, err
}
dstField := dst.Field(i)
// If the field is unexported, we need to disable read-only mode so we
// can actually write to it.
disableRO(&dstField)
dstField.Set(elemDst)
}
return dst, nil
}