-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbuild_copier.go
More file actions
272 lines (247 loc) · 6.75 KB
/
build_copier.go
File metadata and controls
272 lines (247 loc) · 6.75 KB
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 deepcopy
import (
"fmt"
"reflect"
"strings"
"sync"
)
// cacheKey key data structure of cached copiers
type cacheKey struct {
dstType reflect.Type
srcType reflect.Type
flags uint8
}
var (
// copierCacheMap global cache for any parsed type
copierCacheMap = make(map[cacheKey]copier, 10) //nolint:mnd
// mu read/write cache lock
mu sync.RWMutex
// simpleKindMask mask for checking basic kinds such as int, string, ...
simpleKindMask = func() uint32 {
n := uint32(0)
n |= 1 << reflect.Bool
n |= 1 << reflect.String
n |= 1 << reflect.Int
n |= 1 << reflect.Int8
n |= 1 << reflect.Int16
n |= 1 << reflect.Int32
n |= 1 << reflect.Int64
n |= 1 << reflect.Uint
n |= 1 << reflect.Uint8
n |= 1 << reflect.Uint16
n |= 1 << reflect.Uint32
n |= 1 << reflect.Uint64
n |= 1 << reflect.Float32
n |= 1 << reflect.Float64
n |= 1 << reflect.Complex64
n |= 1 << reflect.Complex128
n |= 1 << reflect.Uintptr
n |= 1 << reflect.Func
return n
}()
)
const (
// flagCopyBetweenPtrAndValue indicates copying will be performed between `pointers` and `values`
flagCopyBetweenPtrAndValue = 1
// flagCopyViaCopyingMethod indicates copying will be performed via copying methods of destination types
flagCopyViaCopyingMethod = 2
// flagIgnoreNonCopyableTypes indicates copying will skip copying non-copyable types without raising errors
flagIgnoreNonCopyableTypes = 3
)
// prepare prepares context for copiers
func (ctx *Context) prepare() {
if ctx.UseGlobalCache {
ctx.copierCacheMap = copierCacheMap
ctx.mu = &mu
} else {
ctx.copierCacheMap = make(map[cacheKey]copier, 5) //nolint:mnd
ctx.mu = &sync.RWMutex{}
}
// Recalculate the flags
ctx.flags = 0
if ctx.CopyBetweenPtrAndValue {
ctx.flags |= 1 << flagCopyBetweenPtrAndValue
}
if ctx.CopyViaCopyingMethod {
ctx.flags |= 1 << flagCopyViaCopyingMethod
}
if ctx.IgnoreNonCopyableTypes {
ctx.flags |= 1 << flagIgnoreNonCopyableTypes
}
}
// createCacheKey creates and returns key for caching a copier
func (ctx *Context) createCacheKey(dstType, srcType reflect.Type) *cacheKey {
return &cacheKey{
dstType: dstType,
srcType: srcType,
flags: ctx.flags,
}
}
// defaultContext creates a default context
func defaultContext() *Context {
return &Context{
CopyBetweenPtrAndValue: true,
CopyViaCopyingMethod: true,
UseGlobalCache: true,
}
}
// buildCopier build copier for handling copy from `srcType` to `dstType`
//
//nolint:gocognit,gocyclo,funlen
func buildCopier(ctx *Context, dstType, srcType reflect.Type) (copier copier, err error) {
// Finds cached copier, returns it if found
cacheKey := ctx.createCacheKey(dstType, srcType)
ctx.mu.RLock()
cachedCopier, cachedCopierFound := ctx.copierCacheMap[*cacheKey]
ctx.mu.RUnlock()
if cachedCopier != nil {
return cachedCopier, nil
}
dstKind, srcKind := dstType.Kind(), srcType.Kind()
// Trivial case
if simpleKindMask&(1<<srcKind) > 0 {
if dstType == srcType {
copier = defaultDirectCopier
goto OnComplete
}
if srcType.ConvertibleTo(dstType) {
copier = defaultConvCopier
goto OnComplete
}
}
if dstKind == reflect.Interface {
cp := &toIfaceCopier{ctx: ctx}
copier, err = cp, cp.init(dstType, srcType)
goto OnComplete
}
if srcKind == reflect.Interface {
cp := &fromIfaceCopier{ctx: ctx}
copier, err = cp, cp.init(dstType, srcType)
goto OnComplete
}
//nolint:nestif
if srcKind == reflect.Pointer {
if dstKind == reflect.Pointer { // ptr -> ptr
cp := &ptr2PtrCopier{ctx: ctx}
copier, err = cp, cp.init(dstType, srcType)
goto OnComplete
} else { // ptr -> value
if !ctx.CopyBetweenPtrAndValue {
goto OnNonCopyable
}
cp := &ptr2ValueCopier{ctx: ctx}
copier, err = cp, cp.init(dstType, srcType)
goto OnComplete
}
} else {
if dstKind == reflect.Pointer { // value -> ptr
if !ctx.CopyBetweenPtrAndValue {
goto OnNonCopyable
}
cp := &value2PtrCopier{ctx: ctx}
copier, err = cp, cp.init(dstType, srcType)
goto OnComplete
}
}
// Both are not Pointers
if srcKind == reflect.Slice || srcKind == reflect.Array {
if dstKind != reflect.Slice && dstKind != reflect.Array {
goto OnNonCopyable
}
cp := &sliceCopier{ctx: ctx}
copier, err = cp, cp.init(dstType, srcType)
goto OnComplete
}
//nolint:nestif
if srcKind == reflect.Struct {
if dstKind == reflect.Struct {
// Build a special copier for Go standard types such as time.Time, unique.Handle
copier = buildCopierForStandardStructs(dstType, srcType)
if copier != nil {
goto OnComplete
}
// At this point, cached copier should not exist in the cache map.
// If it exists, seems like a circular reference occurs, use an inline copier.
if cachedCopierFound {
return &inlineCopier{ctx: ctx, dstType: dstType, srcType: srcType}, nil
}
// Circular reference can happen via struct field reference.
// Put a `nil` copier to the cache to mark that the copier building for the struct types is in-progress.
setCachedCopier(ctx, cacheKey, nil)
cp := &structCopier{ctx: ctx}
copier, err = cp, cp.init(dstType, srcType)
if err != nil {
deleteCachedCopier(ctx, cacheKey)
}
goto OnComplete
}
if dstKind == reflect.Map {
cp := &structToMapCopier{ctx: ctx}
copier, err = cp, cp.init(dstType, srcType)
goto OnComplete
}
goto OnNonCopyable
}
if srcKind == reflect.Map {
if dstKind == reflect.Map {
cp := &mapCopier{ctx: ctx}
copier, err = cp, cp.init(dstType, srcType)
goto OnComplete
}
if dstKind == reflect.Struct {
cp := &mapToStructCopier{ctx: ctx}
copier, err = cp, cp.init(dstType, srcType)
goto OnComplete
}
goto OnNonCopyable
}
OnComplete:
if err == nil {
if copier != nil {
setCachedCopier(ctx, cacheKey, copier)
return copier, err
}
} else {
return nil, err
}
OnNonCopyable:
if ctx.IgnoreNonCopyableTypes {
return defaultNopCopier, nil
}
return nil, fmt.Errorf("%w: %v -> %v", ErrTypeNonCopyable, srcType, dstType)
}
func buildCopierForStandardStructs(dstType, srcType reflect.Type) copier {
switch srcType.PkgPath() {
// When copy time.Time -> time.Time or derived type
case "time":
if srcType.Name() == "Time" {
if dstType == srcType {
return defaultDirectCopier
}
if dstType.ConvertibleTo(srcType) {
return defaultConvCopier
}
}
// When copy unique.Handle[T] -> unique.Handle[T] or derived type
case "unique":
if strings.HasPrefix(srcType.Name(), "Handle[") {
if dstType == srcType {
return defaultDirectCopier
}
if dstType.ConvertibleTo(srcType) {
return defaultConvCopier
}
}
}
return nil
}
func setCachedCopier(ctx *Context, cacheKey *cacheKey, cp copier) {
ctx.mu.Lock()
ctx.copierCacheMap[*cacheKey] = cp
ctx.mu.Unlock()
}
func deleteCachedCopier(ctx *Context, cacheKey *cacheKey) {
ctx.mu.Lock()
delete(ctx.copierCacheMap, *cacheKey)
ctx.mu.Unlock()
}