-
Notifications
You must be signed in to change notification settings - Fork 67
/
type.go
345 lines (316 loc) · 7.92 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
// Package zng implements a data typing system based on the zeek type system.
// All zeek types are defined here and implement the Type interface while instances
// of values implement the Value interface. All values conform to exactly one type.
// The package provides a fast-path for comparing a value to a byte slice
// without having to create a zeek value from the byte slice. To exploit this,
// all values include a Comparison method that returns a Predicate function that
// takes a byte slice and a Type and returns a boolean indicating whether the
// the byte slice with the indicated Type matches the value. The package also
// provides mechanism for coercing values in well-defined and natural ways.
package zed
import (
"errors"
"github.com/brimdata/zed/zcode"
)
var (
ErrLenUnset = errors.New("len(unset) is undefined")
ErrNotArray = errors.New("cannot index a non-array")
ErrIndex = errors.New("array index out of bounds")
ErrUnionSelector = errors.New("union selector out of bounds")
ErrEnumIndex = errors.New("enum index out of bounds")
)
// A Type is an interface presented by a zeek type.
// Types can be used to infer type compatibility and create new values
// of the underlying type.
type Type interface {
Marshal(zcode.Bytes) (interface{}, error)
// ID returns a unique (per Context) identifier that
// represents this type. For an aliased type, this identifier
// represents the actual underlying type and not the alias itself.
// Callers that care about the underlying type of a Value for
// example should prefer to use this instead of using the go
// .(type) operator on a Type instance.
ID() int
String() string
Format(zv zcode.Bytes) string
}
var (
TypeUint8 = &TypeOfUint8{}
TypeUint16 = &TypeOfUint16{}
TypeUint32 = &TypeOfUint32{}
TypeUint64 = &TypeOfUint64{}
TypeInt8 = &TypeOfInt8{}
TypeInt16 = &TypeOfInt16{}
TypeInt32 = &TypeOfInt32{}
TypeInt64 = &TypeOfInt64{}
TypeDuration = &TypeOfDuration{}
TypeTime = &TypeOfTime{}
// XXX add TypeFloat16
TypeFloat32 = &TypeOfFloat32{}
TypeFloat64 = &TypeOfFloat64{}
// XXX add TypeDecimal
TypeBool = &TypeOfBool{}
TypeBytes = &TypeOfBytes{}
TypeString = &TypeOfString{}
TypeBstring = &TypeOfBstring{}
TypeIP = &TypeOfIP{}
TypeNet = &TypeOfNet{}
TypeType = &TypeOfType{}
TypeError = &TypeOfError{}
TypeNull = &TypeOfNull{}
)
const (
IDUint8 = 0
IDUint16 = 1
IDUint32 = 2
IDUint64 = 3
IDInt8 = 4
IDInt16 = 5
IDInt32 = 6
IDInt64 = 7
IDDuration = 8
IDTime = 9
IDFloat16 = 10
IDFloat32 = 11
IDFloat64 = 12
IDDecimal = 13
IDBool = 14
IDBytes = 15
IDString = 16
IDBstring = 17
IDIP = 18
IDNet = 19
IDType = 20
IDError = 21
IDNull = 22
IDTypeDef = 23 // 0x17
IDTypeName = 24 // 0x18
IDTypeRecord = 25 // 0x19
IDTypeArray = 26 // 0x20
IDTypeSet = 27 // 0x21
IDTypeUnion = 28 // 0x22
IDTypeEnum = 29 // 0x23
IDTypeMap = 30 // 0x24
)
var promote = []int{
IDInt8, // IDUint8 = 0
IDInt16, // IDUint16 = 1
IDInt32, // IDUint32 = 2
IDInt64, // IDUint64 = 3
IDInt8, // IDInt8 = 4
IDInt16, // IDInt16 = 5
IDInt32, // IDInt32 = 6
IDInt64, // IDInt64 = 7
IDInt64, // IDDuration = 8
IDInt64, // IDTime = 9
IDFloat16, // IDFloat16 = 10
IDFloat32, // IDFloat32 = 11
IDFloat64, // IDFloat64 = 12
IDDecimal, // IDDecimal = 13
}
// Promote type to the largest signed type where the IDs must both
// satisfy IsNumber.
func PromoteInt(aid, bid int) int {
id := promote[aid]
if bid := promote[bid]; bid > id {
id = bid
}
return id
}
// True iff the type id is encoded as a zng signed or unsigened integer zcode.Bytes.
func IsInteger(id int) bool {
return id <= IDInt64
}
// True iff the type id is encoded as a zng signed or unsigned integer zcode.Bytes,
// float32 zcode.Bytes, or float64 zcode.Bytes.
func IsNumber(id int) bool {
return id <= IDDecimal
}
// True iff the type id is encoded as a float encoding.
// XXX add IDDecimal here when we implement coercible math with it.
func IsFloat(id int) bool {
return id >= IDFloat16 && id <= IDFloat64
}
// True iff the type id is encoded as a number encoding and is signed.
func IsSigned(id int) bool {
return id >= IDInt8 && id <= IDTime
}
// True iff the type id is encoded as a string zcode.Bytes.
func IsStringy(id int) bool {
return id == IDString || id == IDBstring || id == IDError
}
const (
CtrlValueEscape = 0xf5
TypeDefRecord = 0xf6
TypeDefArray = 0xf7
TypeDefSet = 0xf8
TypeDefUnion = 0xf9
TypeDefEnum = 0xfa
TypeDefMap = 0xfb
TypeDefAlias = 0xfc
CtrlCompressed = 0xfd
CtrlAppMessage = 0xfe
CtrlEOS = 0xff
AppEncodingZNG = 0
AppEncodingJSON = 1
AppEncodingZSON = 2
AppEncodingString = 3
AppEncodingBinary = 4
)
type CompressionFormat int
const CompressionFormatLZ4 CompressionFormat = 0x00
func LookupPrimitive(name string) Type {
switch name {
case "uint8":
return TypeUint8
case "uint16":
return TypeUint16
case "uint32":
return TypeUint32
case "uint64":
return TypeUint64
case "int8":
return TypeInt8
case "int16":
return TypeInt16
case "int32":
return TypeInt32
case "int64":
return TypeInt64
case "duration":
return TypeDuration
case "time":
return TypeTime
case "float32":
return TypeFloat32
case "float64":
return TypeFloat64
case "bool":
return TypeBool
case "bytes":
return TypeBytes
case "string":
return TypeString
case "bstring":
return TypeBstring
case "ip":
return TypeIP
case "net":
return TypeNet
case "type":
return TypeType
case "error":
return TypeError
case "null":
return TypeNull
}
return nil
}
func LookupPrimitiveByID(id int) Type {
switch id {
case IDBool:
return TypeBool
case IDInt8:
return TypeInt8
case IDUint8:
return TypeUint8
case IDInt16:
return TypeInt16
case IDUint16:
return TypeUint16
case IDInt32:
return TypeInt32
case IDUint32:
return TypeUint32
case IDInt64:
return TypeInt64
case IDUint64:
return TypeUint64
case IDFloat32:
return TypeFloat32
case IDFloat64:
return TypeFloat64
case IDBytes:
return TypeBytes
case IDString:
return TypeString
case IDBstring:
return TypeBstring
case IDIP:
return TypeIP
case IDNet:
return TypeNet
case IDTime:
return TypeTime
case IDDuration:
return TypeDuration
case IDType:
return TypeType
case IDError:
return TypeError
case IDNull:
return TypeNull
}
return nil
}
// Utilities shared by complex types (ie, set and array)
// InnerType returns the element type for the underlying set or array type or
// nil if the underlying type is not a set or array.
func InnerType(typ Type) Type {
switch typ := AliasOf(typ).(type) {
case *TypeSet:
return typ.Type
case *TypeArray:
return typ.Type
default:
return nil
}
}
// ContainedType returns the inner type for set and array types in the first
// return value and the columns of its of type for record types in the second
// return value. ContainedType returns nil for both return values if the
// type is not a set, array, or record.
func ContainedType(typ Type) (Type, []Column) {
switch typ := typ.(type) {
case *TypeSet:
return typ.Type, nil
case *TypeArray:
return typ.Type, nil
case *TypeRecord:
return nil, typ.Columns
// XXX enum?
default:
return nil, nil
}
}
func IsUnionType(typ Type) bool {
_, ok := typ.(*TypeUnion)
return ok
}
func IsRecordType(typ Type) bool {
_, ok := AliasOf(typ).(*TypeRecord)
return ok
}
func TypeRecordOf(typ Type) *TypeRecord {
t, _ := AliasOf(typ).(*TypeRecord)
return t
}
func IsContainerType(typ Type) bool {
switch typ := typ.(type) {
case *TypeAlias:
return IsContainerType(typ.Type)
case *TypeSet, *TypeArray, *TypeRecord, *TypeUnion, *TypeMap:
return true
default:
return false
}
}
func IsPrimitiveType(typ Type) bool {
return !IsContainerType(typ)
}
func TypeID(typ Type) int {
if alias, ok := typ.(*TypeAlias); ok {
return alias.id
}
return typ.ID()
}