-
Notifications
You must be signed in to change notification settings - Fork 10
/
type.go
382 lines (324 loc) · 10.1 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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package ydb
import (
"bytes"
"fmt"
"io"
"time"
"github.com/yandex-cloud/ydb-go-sdk/v2/internal"
)
// Type describes YDB data type.
type Type interface {
internal.T
}
func List(T Type) Type {
return internal.ListType{T: T}
}
func Tuple(elems ...Type) Type {
es := make([]internal.T, len(elems))
for i, el := range elems {
es[i] = el
}
return internal.TupleType{
Elems: es,
}
}
type tStructType internal.StructType
type StructOption func(*tStructType)
func StructField(name string, typ Type) StructOption {
return func(s *tStructType) {
s.Fields = append(s.Fields, internal.StructField{
Name: name,
Type: typ,
})
}
}
func Struct(opts ...StructOption) Type {
var s tStructType
for _, opt := range opts {
opt(&s)
}
return internal.StructType(s)
}
func Variant(x Type) Type {
switch v := x.(type) {
case internal.TupleType:
return internal.VariantType{
T: v,
}
case internal.StructType:
return internal.VariantType{
S: v,
}
default:
panic(fmt.Sprintf("unsupported type for variant: %s", v))
}
}
func Void() Type {
return internal.VoidType{}
}
func Optional(T Type) Type {
return internal.OptionalType{T: T}
}
var DefaultDecimal = Decimal(22, 9)
func Decimal(precision, scale uint32) Type {
return internal.DecimalType{
Precision: precision,
Scale: scale,
}
}
// TODO(kamardin): rename types to consistent format like values: BoolType,
// IntType and so on. Do not forget about code generation.
// Primitive types known by YDB.
const (
TypeUnknown = internal.TypeUnknown
TypeBool = internal.TypeBool
TypeInt8 = internal.TypeInt8
TypeUint8 = internal.TypeUint8
TypeInt16 = internal.TypeInt16
TypeUint16 = internal.TypeUint16
TypeInt32 = internal.TypeInt32
TypeUint32 = internal.TypeUint32
TypeInt64 = internal.TypeInt64
TypeUint64 = internal.TypeUint64
TypeFloat = internal.TypeFloat
TypeDouble = internal.TypeDouble
TypeDate = internal.TypeDate
TypeDatetime = internal.TypeDatetime
TypeTimestamp = internal.TypeTimestamp
TypeInterval = internal.TypeInterval
TypeTzDate = internal.TypeTzDate
TypeTzDatetime = internal.TypeTzDatetime
TypeTzTimestamp = internal.TypeTzTimestamp
TypeString = internal.TypeString
TypeUTF8 = internal.TypeUTF8
TypeYSON = internal.TypeYSON
TypeJSON = internal.TypeJSON
TypeUUID = internal.TypeUUID
TypeJSONDocument = internal.TypeJSONDocument
TypeDyNumber = internal.TypeDyNumber
)
func WriteTypeStringTo(buf *bytes.Buffer, t Type) {
internal.WriteTypeStringTo(buf, t)
}
// deprecated: use ydb.Scanner
type CustomScanner interface {
UnmarshalYDB(res RawScanner) error
}
// deprecated: use RawValue
type RawScanner interface {
HasItems() bool
HasNextItem() bool
// NextItem selects next item to parse in the current row.
// It returns false if there are no more items in the row.
//
// Note that NextItem() differs from NextRow() and NextSet() – if it return
// false it fails the Result such that no further operations may be processed.
// That is, res.Err() becomes non-nil.
NextItem() (ok bool)
// SeekItem finds the column with given name in the result set and selects
// appropriate item to parse in the current row.
SeekItem(name string) bool
Path() string
WritePathTo(w io.Writer) (n int64, err error)
Type() Type
Bool() (v bool)
Int8() (v int8)
Uint8() (v uint8)
Int16() (v int16)
Uint16() (v uint16)
Int32() (v int32)
Uint32() (v uint32)
Int64() (v int64)
Uint64() (v uint64)
Float() (v float32)
Double() (v float64)
Date() (v time.Time)
Datetime() (v time.Time)
Timestamp() (v time.Time)
Interval() (v time.Duration)
TzDate() (v time.Time)
TzDatetime() (v time.Time)
TzTimestamp() (v time.Time)
String() (v string)
UTF8() (v string)
YSON() (v []byte)
JSON() (v []byte)
UUID() (v [16]byte)
JSONDocument() (v []byte)
DyNumber() (v string)
Value() Value
// ListIn interprets current item under scan as a ydb's list.
// It returns the size of the nested items.
// If current item under scan is not a list type, it returns -1.
ListIn() (size int)
// ListItem selects current item i-th element as an item to scan.
// ListIn() must be called before.
ListItem(i int)
// ListOut leaves list entered before by ListIn() call.
ListOut()
// TupleIn interprets current item under scan as a ydb's tuple.
// It returns the size of the nested items.
TupleIn() (size int)
// TupleItem selects current item i-th element as an item to scan.
// Note that TupleIn() must be called before.
// It panics if it is out of bounds.
TupleItem(i int)
// TupleOut leaves tuple entered before by TupleIn() call.
TupleOut()
// StructIn interprets current item under scan as a ydb's struct.
// It returns the size of the nested items – the struct fields values.
// If there is no current item under scan it returns -1.
StructIn() (size int)
// StructField selects current item i-th field value as an item to scan.
// Note that StructIn() must be called before.
// It panics if i is out of bounds.
StructField(i int) (name string)
// StructOut leaves struct entered before by StructIn() call.
StructOut()
// DictIn interprets current item under scan as a ydb's dict.
// It returns the size of the nested items pairs.
// If there is no current item under scan it returns -1.
DictIn() (size int)
// DictKey selects current item i-th pair key as an item to scan.
// Note that DictIn() must be called before.
// It panics if i is out of bounds.
DictKey(i int)
// DictPayload selects current item i-th pair value as an item to scan.
// Note that DictIn() must be called before.
// It panics if i is out of bounds.
DictPayload(i int)
// DictOut leaves dict entered before by DictIn() call.
DictOut()
// Variant unwraps current item under scan interpreting it as Variant<T> type.
// It returns non-empty name of a field that is filled for struct-based
// variant.
// It always returns an index of filled field of a T.
Variant() (name string, index uint32)
// Decimal returns decimal value represented by big-endian 128 bit signed integer.
Decimal(t Type) (v [16]byte)
ODecimal(t Type) (v [16]byte)
// UnwrapDecimal returns decimal value represented by big-endian 128 bit signed
// integer and its type information.
UnwrapDecimal() (v [16]byte, precision, scale uint32)
// Unwrap unwraps current item under scan interpreting it as Optional<T> type.
Unwrap()
IsNull() bool
IsOptional() bool
IsDecimal() bool
Err() error
}
// Scanner scanning non-primitive yql types
type Scanner interface {
UnmarshalYDB(res RawValue) error
}
// RawValue scanning non-primitive yql types or for own implementation scanner native API
type RawValue interface {
Path() string
WritePathTo(w io.Writer) (n int64, err error)
Type() Type
Bool() (v bool)
Int8() (v int8)
Uint8() (v uint8)
Int16() (v int16)
Uint16() (v uint16)
Int32() (v int32)
Uint32() (v uint32)
Int64() (v int64)
Uint64() (v uint64)
Float() (v float32)
Double() (v float64)
Date() (v time.Time)
Datetime() (v time.Time)
Timestamp() (v time.Time)
Interval() (v time.Duration)
TzDate() (v time.Time)
TzDatetime() (v time.Time)
TzTimestamp() (v time.Time)
String() (v string)
UTF8() (v string)
YSON() (v []byte)
JSON() (v []byte)
UUID() (v [16]byte)
JSONDocument() (v []byte)
DyNumber() (v string)
Value() Value
// Any returns any primitive or optional value.
// Currently, it may return one of these types:
//
// bool
// int8
// uint8
// int16
// uint16
// int32
// uint32
// int64
// uint64
// float32
// float64
// []byte
// string
// [16]byte
//
Any() interface{}
// Unwrap unwraps current item under scan interpreting it as Optional<T> type.
Unwrap()
AssertType(t Type) bool
IsNull() bool
IsOptional() bool
// ListIn interprets current item under scan as a ydb's list.
// It returns the size of the nested items.
// If current item under scan is not a list type, it returns -1.
ListIn() (size int)
// ListItem selects current item i-th element as an item to scan.
// ListIn() must be called before.
ListItem(i int)
// ListOut leaves list entered before by ListIn() call.
ListOut()
// TupleIn interprets current item under scan as a ydb's tuple.
// It returns the size of the nested items.
TupleIn() (size int)
// TupleItem selects current item i-th element as an item to scan.
// Note that TupleIn() must be called before.
// It panics if it is out of bounds.
TupleItem(i int)
// TupleOut leaves tuple entered before by TupleIn() call.
TupleOut()
// StructIn interprets current item under scan as a ydb's struct.
// It returns the size of the nested items – the struct fields values.
// If there is no current item under scan it returns -1.
StructIn() (size int)
// StructField selects current item i-th field value as an item to scan.
// Note that StructIn() must be called before.
// It panics if i is out of bounds.
StructField(i int) (name string)
// StructOut leaves struct entered before by StructIn() call.
StructOut()
// DictIn interprets current item under scan as a ydb's dict.
// It returns the size of the nested items pairs.
// If there is no current item under scan it returns -1.
DictIn() (size int)
// DictKey selects current item i-th pair key as an item to scan.
// Note that DictIn() must be called before.
// It panics if i is out of bounds.
DictKey(i int)
// DictPayload selects current item i-th pair value as an item to scan.
// Note that DictIn() must be called before.
// It panics if i is out of bounds.
DictPayload(i int)
// DictOut leaves dict entered before by DictIn() call.
DictOut()
// Variant unwraps current item under scan interpreting it as Variant<T> type.
// It returns non-empty name of a field that is filled for struct-based
// variant.
// It always returns an index of filled field of a T.
Variant() (name string, index uint32)
// Decimal returns decimal value represented by big-endian 128 bit signed integer.
Decimal(t Type) (v [16]byte)
ODecimal(t Type) (v [16]byte)
// UnwrapDecimal returns decimal value represented by big-endian 128 bit signed
// integer and its type information.
UnwrapDecimal() (v [16]byte, precision, scale uint32)
IsDecimal() bool
Err() error
}