forked from wI2L/jettison
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
185 lines (172 loc) · 4.57 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
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
package jettison
import (
"encoding"
"encoding/json"
"reflect"
"sync"
"time"
"unsafe"
)
var (
timeTimeType = reflect.TypeOf(time.Time{})
timeDurationType = reflect.TypeOf(time.Duration(0))
syncMapType = reflect.TypeOf((*sync.Map)(nil)).Elem()
jsonNumberType = reflect.TypeOf(json.Number(""))
jsonRawMessageType = reflect.TypeOf(json.RawMessage(nil))
jsonMarshalerType = reflect.TypeOf((*json.Marshaler)(nil)).Elem()
textMarshalerType = reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem()
appendMarshalerType = reflect.TypeOf((*AppendMarshaler)(nil)).Elem()
appendMarshalerCtxType = reflect.TypeOf((*AppendMarshalerCtx)(nil)).Elem()
)
var emptyFnCache sync.Map // map[reflect.Type]emptyFunc
// emptyFunc is a function that returns whether a
// value pointed by an unsafe.Pointer represents the
// zero value of its type.
type emptyFunc func(unsafe.Pointer) bool
// marshalerEncodeFunc is a function that appends
// the result of a marshaler method call to dst.
type marshalerEncodeFunc func(interface{}, []byte, encOpts, reflect.Type) ([]byte, error)
func isBasicType(t reflect.Type) bool {
return isBoolean(t) || isString(t) || isFloatingPoint(t) || isInteger(t)
}
func isBoolean(t reflect.Type) bool { return t.Kind() == reflect.Bool }
func isString(t reflect.Type) bool { return t.Kind() == reflect.String }
func isFloatingPoint(t reflect.Type) bool {
kind := t.Kind()
if kind == reflect.Float32 || kind == reflect.Float64 {
return true
}
return false
}
func isInteger(t reflect.Type) bool {
switch t.Kind() {
case reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64,
reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64,
reflect.Uintptr:
return true
default:
return false
}
}
func isInlined(t reflect.Type) bool {
switch t.Kind() {
case reflect.Ptr, reflect.Map:
return true
case reflect.Struct:
return t.NumField() == 1 && isInlined(t.Field(0).Type)
default:
return false
}
}
func isNilable(t reflect.Type) bool {
switch t.Kind() {
case reflect.Ptr, reflect.Interface, reflect.Slice, reflect.Map:
return true
}
return false
}
// cachedEmptyFuncOf is similar to emptyFuncOf, but
// returns a cached function, to avoid duplicates.
func cachedEmptyFuncOf(t reflect.Type) emptyFunc {
if fn, ok := emptyFnCache.Load(t); ok {
return fn.(emptyFunc)
}
fn, _ := emptyFnCache.LoadOrStore(t, emptyFuncOf(t))
return fn.(emptyFunc)
}
// emptyFuncOf returns a function that can be used to
// determine if a value pointed by an unsafe,Pointer
// represents the zero-value of type t.
func emptyFuncOf(t reflect.Type) emptyFunc {
switch t.Kind() {
case reflect.Bool:
return func(p unsafe.Pointer) bool {
return !*(*bool)(p)
}
case reflect.String:
return func(p unsafe.Pointer) bool {
return (*stringHeader)(p).Len == 0
}
case reflect.Int:
return func(p unsafe.Pointer) bool {
return *(*int)(p) == 0
}
case reflect.Int8:
return func(p unsafe.Pointer) bool {
return *(*int8)(p) == 0
}
case reflect.Int16:
return func(p unsafe.Pointer) bool {
return *(*int16)(p) == 0
}
case reflect.Int32:
return func(p unsafe.Pointer) bool {
return *(*int32)(p) == 0
}
case reflect.Int64:
return func(p unsafe.Pointer) bool {
return *(*int64)(p) == 0
}
case reflect.Uint:
return func(p unsafe.Pointer) bool {
return *(*uint)(p) == 0
}
case reflect.Uint8:
return func(p unsafe.Pointer) bool {
return *(*uint8)(p) == 0
}
case reflect.Uint16:
return func(p unsafe.Pointer) bool {
return *(*uint16)(p) == 0
}
case reflect.Uint32:
return func(p unsafe.Pointer) bool {
return *(*uint32)(p) == 0
}
case reflect.Uint64:
return func(p unsafe.Pointer) bool {
return *(*uint64)(p) == 0
}
case reflect.Uintptr:
return func(p unsafe.Pointer) bool {
return *(*uintptr)(p) == 0
}
case reflect.Float32:
return func(p unsafe.Pointer) bool {
return *(*float32)(p) == 0
}
case reflect.Float64:
return func(p unsafe.Pointer) bool {
return *(*float64)(p) == 0
}
case reflect.Map:
return func(p unsafe.Pointer) bool {
return maplen(*(*unsafe.Pointer)(p)) == 0
}
case reflect.Ptr:
return func(p unsafe.Pointer) bool {
return *(*unsafe.Pointer)(p) == nil
}
case reflect.Interface:
return func(p unsafe.Pointer) bool {
return *(*unsafe.Pointer)(p) == nil
}
case reflect.Slice:
return func(p unsafe.Pointer) bool {
return (*sliceHeader)(p).Len == 0
}
case reflect.Array:
if t.Len() == 0 {
return func(unsafe.Pointer) bool { return true }
}
}
return func(unsafe.Pointer) bool { return false }
}