forked from tendermint/go-amino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deep_copy.go
254 lines (234 loc) · 5.33 KB
/
deep_copy.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
package amino
import (
"fmt"
"reflect"
)
//----------------------------------------
// DeepCopy
// Deeply copies an object.
// If anything implements `.DeepCopy() <any>` along the way,
// the result of that function will be used.
// Otherwise, if it implements `.MarshalAmino() (<any>, error)` and
// `.UnmarshalAmino(<any>) error`, the pair will be used to copy.
// If .MarshalAmino() or .UnmarshalAmino() returns an error, this
// function will panic.
func DeepCopy(o interface{}) (r interface{}) {
if o == nil {
return nil
}
src := reflect.ValueOf(o)
dst := reflect.New(src.Type()).Elem()
deepCopy(src, dst)
return dst.Interface()
}
func deepCopy(src, dst reflect.Value) {
if isNil(src) {
return
}
if callDeepCopy(src, dst) {
return
}
if callAminoCopy(src, dst) {
return
}
_deepCopy(src, dst)
}
func _deepCopy(src, dst reflect.Value) {
switch src.Kind() {
case reflect.Ptr:
cpy := reflect.New(src.Type().Elem())
_deepCopy(src.Elem(), cpy.Elem())
dst.Set(cpy)
return
case reflect.Interface:
cpy := reflect.New(src.Elem().Type())
deepCopy(src.Elem(), cpy.Elem())
dst.Set(cpy.Elem())
return
case reflect.Array:
switch src.Type().Elem().Kind() {
case reflect.Int64, reflect.Int32, reflect.Int16,
reflect.Int8, reflect.Int, reflect.Uint64,
reflect.Uint32, reflect.Uint16, reflect.Uint8,
reflect.Uint, reflect.Bool, reflect.Float64,
reflect.Float32, reflect.String:
reflect.Copy(dst, src)
return
default:
for i := 0; i < src.Type().Len(); i++ {
esrc := src.Index(i)
edst := dst.Index(i)
deepCopy(esrc, edst)
}
return
}
case reflect.Slice:
switch src.Type().Elem().Kind() {
case reflect.Int64, reflect.Int32, reflect.Int16,
reflect.Int8, reflect.Int, reflect.Uint64,
reflect.Uint32, reflect.Uint16, reflect.Uint8,
reflect.Uint, reflect.Bool, reflect.Float64,
reflect.Float32, reflect.String:
cpy := reflect.MakeSlice(
src.Type(), src.Len(), src.Len())
reflect.Copy(cpy, src)
dst.Set(src)
return
default:
cpy := reflect.MakeSlice(
src.Type(), src.Len(), src.Len())
for i := 0; i < src.Len(); i++ {
esrc := src.Index(i)
ecpy := cpy.Index(i)
deepCopy(esrc, ecpy)
}
dst.Set(src)
return
}
case reflect.Struct:
switch src.Type() {
case timeType:
dst.Set(src)
return
default:
for i := 0; i < src.NumField(); i++ {
if !isExported(src.Type().Field(i)) {
continue // field is unexported
}
srcf := src.Field(i)
dstf := dst.Field(i)
deepCopy(srcf, dstf)
}
return
}
case reflect.Map:
cpy := reflect.MakeMapWithSize(src.Type(), src.Len())
keys := src.MapKeys()
for _, key := range keys {
val := src.MapIndex(key)
cpy.SetMapIndex(key, val)
}
dst.Set(cpy)
return
// Primitive types
case reflect.Int64, reflect.Int32, reflect.Int16,
reflect.Int8, reflect.Int:
dst.SetInt(src.Int())
case reflect.Uint64, reflect.Uint32, reflect.Uint16,
reflect.Uint8, reflect.Uint:
dst.SetUint(src.Uint())
case reflect.Bool:
dst.SetBool(src.Bool())
case reflect.Float64, reflect.Float32:
dst.SetFloat(src.Float())
case reflect.String:
dst.SetString(src.String())
default:
panic(fmt.Sprintf("unsupported type %v", src.Kind()))
}
return
}
//----------------------------------------
// misc.
// Call .DeepCopy() method if possible.
func callDeepCopy(src, dst reflect.Value) bool {
dc := src.MethodByName("DeepCopy")
if !dc.IsValid() {
return false
}
if dc.Type().NumIn() != 0 {
return false
}
if dc.Type().NumOut() != 1 {
return false
}
otype := dc.Type().Out(0)
if dst.Kind() == reflect.Ptr &&
dst.Type().Elem() == otype {
cpy := reflect.New(dst.Type().Elem())
out := dc.Call(nil)[0]
cpy.Elem().Set(out)
dst.Set(cpy)
return true
}
if dst.Type() == otype {
out := dc.Call(nil)[0]
dst.Set(out)
return true
}
return false
}
// Call .MarshalAmino() and .UnmarshalAmino to copy if possible.
// Panics if .MarshalAmino() or .UnmarshalAmino() return an error.
// CONTRACT: src and dst are of equal types.
func callAminoCopy(src, dst reflect.Value) bool {
if src.Type() != dst.Type() {
panic("should not happen")
}
switch {
case src.Kind() == reflect.Ptr:
cpy := reflect.New(src.Type().Elem())
dst.Set(cpy)
case src.CanAddr():
if !dst.CanAddr() {
panic("should not happen")
}
src = src.Addr()
dst = dst.Addr()
default:
return false
}
if !canAminoCopy(src) {
return false
}
cpy := reflect.New(src.Type().Elem())
dst.Set(cpy)
ma := src.MethodByName("MarshalAmino")
ua := dst.MethodByName("UnmarshalAmino")
outs := ma.Call(nil)
repr, err := outs[0], outs[1]
if !err.IsNil() {
panic(err.Interface())
}
outs = ua.Call([]reflect.Value{repr})
err = outs[0]
if !err.IsNil() {
panic(err.Interface())
}
return true
}
func canAminoCopy(rv reflect.Value) bool {
if !rv.MethodByName("UnmarshalAmino").IsValid() {
return false
}
ua := rv.MethodByName("UnmarshalAmino")
if !ua.IsValid() {
return false
}
if ua.Type().NumIn() != 1 {
return false
}
if ua.Type().NumOut() != 1 {
return false
}
if ua.Type().Out(0) != errorType {
return false
}
ma := rv.MethodByName("MarshalAmino")
if !ma.IsValid() {
return false
}
if ma.Type().NumIn() != 0 {
return false
}
if ma.Type().NumOut() != 2 {
return false
}
if ma.Type().Out(1) != errorType {
return false
}
if ua.Type().In(0) != ma.Type().Out(0) {
return false
}
return true
}