forked from go-pg/pg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
302 lines (246 loc) · 5.47 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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package pg
import (
"database/sql"
"database/sql/driver"
"reflect"
"sync"
"gopkg.in/pg.v3/pgutil"
)
const (
nullEmpty = 1 << 0
)
var (
appenderType = reflect.TypeOf(new(QueryAppender)).Elem()
scannerType = reflect.TypeOf(new(sql.Scanner)).Elem()
driverValuerType = reflect.TypeOf(new(driver.Valuer)).Elem()
)
var structs = newStructCache()
func appendIfaceValue(dst []byte, v reflect.Value, quote bool) []byte {
return appendIface(dst, v.Interface(), quote)
}
func getAppender(typ reflect.Type) valueAppender {
switch typ {
case timeType:
return appendTimeValue
}
if typ.Implements(appenderType) {
return appendAppenderValue
}
if typ.Implements(driverValuerType) {
return appendDriverValuerValue
}
kind := typ.Kind()
if appender := valueAppenders[kind]; appender != nil {
return appender
}
return appendIfaceValue
}
func getDecoder(typ reflect.Type) valueDecoder {
switch typ {
case timeType:
return decodeTimeValue
}
if reflect.PtrTo(typ).Implements(scannerType) {
return decodeScannerAddrValue
}
if typ.Implements(scannerType) {
return decodeScannerValue
}
kind := typ.Kind()
if dec := valueDecoders[kind]; dec != nil {
return dec
}
return nil
}
type field struct {
Name string
PGName string
index []int
flags int8
appender valueAppender
decoder valueDecoder
}
func (f *field) Is(flag int8) bool {
return f.flags&flag != 0
}
func (f *field) IsEmpty(v reflect.Value) bool {
fv := v.FieldByIndex(f.index)
return isEmptyValue(fv)
}
func (f *field) AppendValue(dst []byte, v reflect.Value, quote bool) []byte {
fv := v.FieldByIndex(f.index)
if f.Is(nullEmpty) && isEmptyValue(fv) {
return appendNull(dst, quote)
}
return f.appender(dst, fv, quote)
}
func fieldByIndex(v reflect.Value, index []int) reflect.Value {
if len(index) == 1 {
return v.Field(index[0])
}
for i, x := range index {
if i > 0 && v.Kind() == reflect.Ptr {
if v.IsNil() {
v.Set(reflect.New(v.Type().Elem()))
}
v = v.Elem()
}
v = v.Field(x)
}
return v
}
func (f *field) DecodeValue(v reflect.Value, b []byte) error {
v = fieldByIndex(v, f.index)
if b == nil {
return decodeNullValue(v)
}
return f.decoder(v, b)
}
//------------------------------------------------------------------------------
type structCache struct {
fields map[reflect.Type]fields
fieldsMtx sync.RWMutex
methods map[reflect.Type]map[string]*method
methodsMtx sync.RWMutex
}
func newStructCache() *structCache {
return &structCache{
fields: make(map[reflect.Type]fields),
methods: make(map[reflect.Type]map[string]*method),
}
}
func (c *structCache) Fields(typ reflect.Type) fields {
c.fieldsMtx.RLock()
fs, ok := c.fields[typ]
c.fieldsMtx.RUnlock()
if ok {
return fs
}
c.fieldsMtx.Lock()
fs, ok = c.fields[typ]
if !ok {
fs = getFields(typ)
c.fields[typ] = fs
}
c.fieldsMtx.Unlock()
return fs
}
func (c *structCache) Methods(typ reflect.Type) map[string]*method {
c.methodsMtx.RLock()
ms, ok := c.methods[typ]
c.methodsMtx.RUnlock()
if ok {
return ms
}
c.methodsMtx.Lock()
ms, ok = c.methods[typ]
if !ok {
ms = getMethods(typ)
c.methods[typ] = ms
}
c.methodsMtx.Unlock()
return ms
}
//------------------------------------------------------------------------------
type fields struct {
List []*field
Table map[string]*field
}
func newFields(numField int) fields {
return fields{
List: make([]*field, 0, numField),
Table: make(map[string]*field, numField),
}
}
func (fs fields) Len() int {
return len(fs.List)
}
func (fs *fields) Add(field *field) {
fs.List = append(fs.List, field)
fs.Table[field.Name] = field
}
func getFields(typ reflect.Type) fields {
num := typ.NumField()
fs := newFields(num)
for i := 0; i < num; i++ {
f := typ.Field(i)
if f.Anonymous {
typ := f.Type
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
}
for _, ff := range getFields(typ).List {
ff.index = append(f.Index, ff.index...)
fs.Add(ff)
}
continue
}
if f.PkgPath != "" {
continue
}
name, opts := parseTag(f.Tag.Get("pg"))
if name == "-" {
continue
}
if name == "" {
name = pgutil.Underscore(f.Name)
}
fieldType := indirectType(f.Type)
if fieldType.Kind() == reflect.Struct {
for _, ff := range getFields(fieldType).List {
ff.PGName = name + "." + ff.Name
ff.Name = name + "__" + ff.Name
ff.index = append(f.Index, ff.index...)
fs.Add(ff)
}
}
var flags int8
if opts.Contains("nullempty") {
flags |= nullEmpty
}
field := &field{
Name: name,
index: f.Index,
flags: flags,
appender: getAppender(fieldType),
decoder: getDecoder(fieldType),
}
fs.Add(field)
}
return fs
}
//------------------------------------------------------------------------------
type method struct {
Index int
appender valueAppender
}
func (m *method) AppendValue(dst []byte, v reflect.Value, quote bool) []byte {
mv := v.Method(m.Index).Call(nil)[0]
return m.appender(dst, mv, quote)
}
func getMethods(typ reflect.Type) map[string]*method {
num := typ.NumMethod()
methods := make(map[string]*method, num)
for i := 0; i < num; i++ {
m := typ.Method(i)
if m.Type.NumIn() > 1 {
continue
}
if m.Type.NumOut() != 1 {
continue
}
method := &method{
Index: m.Index,
appender: getAppender(m.Type.Out(0)),
}
methods[m.Name] = method
}
return methods
}
//------------------------------------------------------------------------------
func indirectType(t reflect.Type) reflect.Type {
for t.Kind() == reflect.Ptr {
t = t.Elem()
}
return t
}