-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtypes.go
44 lines (40 loc) · 1.03 KB
/
types.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
package deecpy
import (
"reflect"
"sync"
)
var isValueTypeCache sync.Map
var ConfigCopyString = false
func isValueType(t reflect.Type) bool {
// Fast path
ivC, ok := isValueTypeCache.Load(t)
if ok {
return ivC.(bool)
}
// Slow path
switch t.Kind() {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128,
reflect.Bool,
reflect.Uintptr, reflect.UnsafePointer: // DO NOT DUPLICATE
isValueTypeCache.Store(t, true)
return true
case reflect.String: // String: immutable
return !ConfigCopyString
case reflect.Array:
isValueTypeCache.Store(t, isValueType(t.Elem()))
return isValueType(t.Elem())
case reflect.Struct:
for i := 0; i < t.NumField(); i++ {
if !isValueType(t.Field(i).Type) {
isValueTypeCache.Store(t, false)
return false
}
}
isValueTypeCache.Store(t, true)
return true
}
isValueTypeCache.Store(t, false)
return false
}