-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_copier.go
193 lines (175 loc) · 4.83 KB
/
build_copier.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
package deepcopy
import (
"fmt"
"reflect"
"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
// flagCopyBetweenStructFieldAndMethod indicates copying will be performed between `struct fields` and `functions`
flagCopyBetweenStructFieldAndMethod = 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.CopyBetweenStructFieldAndMethod {
ctx.flags |= 1 << flagCopyBetweenStructFieldAndMethod
}
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,
CopyBetweenStructFieldAndMethod: true,
UseGlobalCache: true,
}
}
// buildCopier build copier for handling copy from `srcType` to `dstType`
//
//nolint:gocognit,gocyclo
func buildCopier(ctx *Context, dstType, srcType reflect.Type) (copier, error) {
dstKind, srcKind := dstType.Kind(), srcType.Kind()
if dstKind == reflect.Interface {
return &toIfaceCopier{ctx: ctx}, nil
}
if srcKind == reflect.Interface {
return &fromIfaceCopier{ctx: ctx}, nil
}
//nolint:nestif
if srcKind == reflect.Pointer {
if dstKind == reflect.Pointer { // ptr -> ptr
copier := &ptr2PtrCopier{ctx: ctx}
if err := copier.init(dstType, srcType); err != nil {
return nil, err
}
return copier, nil
} else { // ptr -> value
if !ctx.CopyBetweenPtrAndValue {
return onNonCopyable(ctx, dstType, srcType)
}
copier := &ptr2ValueCopier{ctx: ctx}
if err := copier.init(dstType, srcType); err != nil {
return nil, err
}
return copier, nil
}
} else {
if dstKind == reflect.Pointer { // value -> ptr
if !ctx.CopyBetweenPtrAndValue {
return onNonCopyable(ctx, dstType, srcType)
}
copier := &value2PtrCopier{ctx: ctx}
if err := copier.init(dstType, srcType); err != nil {
return nil, err
}
return copier, nil
}
}
// Both are not Pointers
if srcKind == reflect.Slice || srcKind == reflect.Array {
if dstKind != reflect.Slice && dstKind != reflect.Array {
return onNonCopyable(ctx, dstType, srcType)
}
copier := &sliceCopier{ctx: ctx}
if err := copier.init(dstType, srcType); err != nil {
return nil, err
}
return copier, nil
}
if srcKind == reflect.Struct {
if dstKind != reflect.Struct {
return onNonCopyable(ctx, dstType, srcType)
}
copier := &structCopier{ctx: ctx}
if err := copier.init(dstType, srcType); err != nil {
return nil, err
}
return copier, nil
}
if srcKind == reflect.Map {
if dstKind != reflect.Map {
return onNonCopyable(ctx, dstType, srcType)
}
copier := &mapCopier{ctx: ctx}
if err := copier.init(dstType, srcType); err != nil {
return nil, err
}
return copier, nil
}
// Trivial case
if simpleKindMask&(1<<srcKind) > 0 {
if dstType == srcType {
return &directCopier{}, nil
}
if srcType.ConvertibleTo(dstType) {
return &convCopier{}, nil
}
}
return onNonCopyable(ctx, dstType, srcType)
}
func onNonCopyable(ctx *Context, dstType, srcType reflect.Type) (copier, error) {
if ctx.IgnoreNonCopyableTypes {
return &nopCopier{}, nil
}
return nil, fmt.Errorf("%w: %v -> %v", ErrTypeNonCopyable, srcType, dstType)
}