This repository has been archived by the owner on Jan 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathtype.go
321 lines (312 loc) · 6.05 KB
/
type.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package orient
import (
"log"
"reflect"
"time"
"unsafe"
)
// OType is an enum for the various data types supported by OrientDB.
type OType byte
// in alignment with: http://orientdb.com/docs/last/Types.html
const (
BOOLEAN OType = 0
INTEGER OType = 1
SHORT OType = 2
LONG OType = 3
FLOAT OType = 4
DOUBLE OType = 5
DATETIME OType = 6
STRING OType = 7
BINARY OType = 8 // means []byte
EMBEDDED OType = 9
EMBEDDEDLIST OType = 10
EMBEDDEDSET OType = 11
EMBEDDEDMAP OType = 12
LINK OType = 13
LINKLIST OType = 14
LINKSET OType = 15
LINKMAP OType = 16
BYTE OType = 17
TRANSIENT OType = 18
DATE OType = 19
CUSTOM OType = 20
DECIMAL OType = 21
LINKBAG OType = 22
ANY OType = 23
UNKNOWN OType = 255 // driver addition
)
// detect the int size (4 or 8 bytes)
const intSize = unsafe.Sizeof(int(0))
func (t OType) String() string { // do not change - it may be used as field type for SQL queries
switch t {
case BOOLEAN:
return "BOOLEAN"
case INTEGER:
return "INTEGER"
case LONG:
return "LONG"
case FLOAT:
return "FLOAT"
case DOUBLE:
return "DOUBLE"
case DATETIME:
return "DATETIME"
case STRING:
return "STRING"
case BINARY:
return "BINARY"
case EMBEDDED:
return "EMBEDDED"
case EMBEDDEDLIST:
return "EMBEDDEDLIST"
case EMBEDDEDSET:
return "EMBEDDEDSET"
case EMBEDDEDMAP:
return "EMBEDDEDMAP"
case LINK:
return "LINK"
case LINKLIST:
return "LINKLIST"
case LINKSET:
return "LINKSET"
case LINKMAP:
return "LINKMAP"
case BYTE:
return "BYTE"
case TRANSIENT:
return "TRANSIENT"
case DATE:
return "DATE"
case CUSTOM:
return "CUSTOM"
case DECIMAL:
return "DECIMAL"
case LINKBAG:
return "LINKBAG"
case ANY:
return "ANY"
default:
return "UNKNOWN"
}
}
func (t OType) ReflectKind() reflect.Kind {
switch t {
case BOOLEAN:
return reflect.Bool
case BYTE:
return reflect.Uint8
case SHORT:
return reflect.Int16
case INTEGER:
return reflect.Int32
case LONG:
return reflect.Int64
case FLOAT:
return reflect.Float32
case DOUBLE:
return reflect.Float64
case STRING:
return reflect.String
case EMBEDDEDLIST, EMBEDDEDSET:
return reflect.Slice
case EMBEDDEDMAP:
return reflect.Map
case LINKLIST, LINKSET:
return reflect.Slice
case LINKMAP:
return reflect.Map
default:
return reflect.Invalid
}
}
func (t OType) ReflectType() reflect.Type {
switch t {
case BOOLEAN:
return reflect.TypeOf(bool(false))
case INTEGER:
return reflect.TypeOf(int32(0))
case LONG:
return reflect.TypeOf(int64(0))
case FLOAT:
return reflect.TypeOf(float32(0))
case DOUBLE:
return reflect.TypeOf(float64(0))
case DATETIME, DATE:
return reflect.TypeOf(time.Time{})
case STRING:
return reflect.TypeOf(string(""))
case BINARY:
return reflect.TypeOf([]byte{})
case BYTE:
return reflect.TypeOf(byte(0))
// case EMBEDDED:
// return "EMBEDDED"
// case EMBEDDEDLIST:
// return "EMBEDDEDLIST"
// case EMBEDDEDSET:
// return "EMBEDDEDSET"
// case EMBEDDEDMAP:
// return "EMBEDDEDMAP"
// case LINK:
// return "LINK"
// case LINKLIST:
// return "LINKLIST"
// case LINKSET:
// return "LINKSET"
// case LINKMAP:
// return "LINKMAP"
// case CUSTOM:
// return "CUSTOM"
// case DECIMAL:
// return "DECIMAL"
// case LINKBAG:
// return "LINKBAG"
default: // and ANY, TRANSIENT
return reflect.TypeOf((*interface{})(nil)).Elem()
}
}
func OTypeForValue(val interface{}) (ftype OType) {
ftype = UNKNOWN
// TODO: need to add more types: LINKSET, LINKLIST, etc. ...
switch val.(type) {
case string:
ftype = STRING
case bool:
ftype = BOOLEAN
case int32:
ftype = INTEGER
case int64:
ftype = LONG
case int16:
ftype = SHORT
case int:
if intSize == 4 {
ftype = INTEGER
} else {
ftype = LONG
}
case byte, int8:
ftype = BYTE
case *Document, DocumentSerializable:
ftype = EMBEDDED
case float32:
ftype = FLOAT
case float64:
ftype = DOUBLE
case []byte:
ftype = BINARY
case OIdentifiable:
ftype = LINK
case []OIdentifiable, []RID:
ftype = LINKLIST
case *RidBag:
ftype = LINKBAG
case time.Time:
ftype = DATETIME
// TODO: more types need to be added
default:
if isDecimal(val) {
ftype = DECIMAL
return
}
rt := reflect.TypeOf(val)
if rt.Kind() == reflect.Ptr {
rt = rt.Elem()
}
switch rt.Kind() {
case reflect.Map:
ftype = EMBEDDEDMAP
case reflect.Slice, reflect.Array:
if reflect.TypeOf(val).Elem() == reflect.TypeOf(byte(0)) {
ftype = BINARY
} else {
ftype = EMBEDDEDLIST
}
case reflect.Bool:
ftype = BOOLEAN
case reflect.Uint8:
ftype = BYTE
case reflect.Int16:
ftype = SHORT
case reflect.Int32:
ftype = INTEGER
case reflect.Int64:
ftype = LONG
case reflect.Int:
if intSize == 4 {
ftype = INTEGER
} else {
ftype = LONG
}
case reflect.Uint:
if intSize == 4 {
ftype = INTEGER
} else {
ftype = LONG
}
case reflect.Uint64:
ftype = LONG
case reflect.String:
ftype = STRING
case reflect.Struct:
ftype = EMBEDDED
default:
log.Printf("unknown type in serialization: %T, kind: %v", val, reflect.TypeOf(val).Kind())
}
}
return
}
func OTypeFromString(typ string) OType {
switch typ {
case "BOOLEAN":
return BOOLEAN
case "INTEGER":
return INTEGER
case "SHORT":
return SHORT
case "LONG":
return LONG
case "FLOAT":
return FLOAT
case "DOUBLE":
return DOUBLE
case "DATETIME":
return DATETIME
case "STRING":
return STRING
case "BINARY":
return BINARY
case "EMBEDDED":
return EMBEDDED
case "EMBEDDEDLIST":
return EMBEDDEDLIST
case "EMBEDDEDSET":
return EMBEDDEDSET
case "EMBEDDEDMAP":
return EMBEDDEDMAP
case "LINK":
return LINK
case "LINKLIST":
return LINKLIST
case "LINKSET":
return LINKSET
case "LINKMAP":
return LINKMAP
case "BYTE":
return BYTE
case "TRANSIENT":
return TRANSIENT
case "DATE":
return DATE
case "CUSTOM":
return CUSTOM
case "DECIMAL":
return DECIMAL
case "LINKBAG":
return LINKBAG
case "ANY":
return ANY
default:
panic("Unkwown type: " + typ)
}
}