-
Notifications
You must be signed in to change notification settings - Fork 67
/
recordval.go
278 lines (251 loc) · 6.67 KB
/
recordval.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
package zed
import (
"bytes"
"errors"
"math"
"net"
"github.com/brimdata/zed/field"
"github.com/brimdata/zed/pkg/nano"
"github.com/brimdata/zed/zcode"
)
var (
ErrMissingField = errors.New("record missing a field")
ErrExtraField = errors.New("record with extra field")
ErrNotContainer = errors.New("expected container type, got primitive")
ErrNotPrimitive = errors.New("expected primitive type, got container")
ErrTypeIDExists = errors.New("zng type ID exists")
ErrTypeIDInvalid = errors.New("zng type ID out of range")
ErrBadValue = errors.New("malformed zng value")
ErrBadFormat = errors.New("malformed zng record")
ErrTypeMismatch = errors.New("type/value mismatch")
ErrColumnMismatch = errors.New("zng record mismatch between columns in type and columns in value")
ErrCorruptColumns = errors.New("wrong number of columns in zng record value")
)
type RecordTypeError struct {
Name string
Type string
Err error
}
func (r *RecordTypeError) Error() string { return r.Name + " (" + r.Type + "): " + r.Err.Error() }
func (r *RecordTypeError) Unwrap() error { return r.Err }
// FieldIter returns a fieldIter iterator over the receiver's values.
func (r *Value) FieldIter() fieldIter {
return fieldIter{
stack: []iterInfo{{
iter: r.Bytes.Iter(),
typ: TypeRecordOf(r.Type),
}},
}
}
func (r *Value) Keep() *Value {
bytes := make(zcode.Bytes, len(r.Bytes))
copy(bytes, r.Bytes)
return NewValue(r.Type, bytes)
}
func (r *Value) CopyBytes() {
bytes := make(zcode.Bytes, len(r.Bytes))
copy(bytes, r.Bytes)
r.Bytes = bytes
}
func (r *Value) HasField(field string) bool {
return TypeRecordOf(r.Type).HasField(field)
}
// Walk traverses a record in depth-first order, calling a
// RecordVisitor on the way.
func (r *Value) Walk(rv Visitor) error {
return walkRecord(TypeRecordOf(r.Type), r.Bytes, rv)
}
// TypeCheck checks that the Bytes field is structurally consistent
// with this value's Type. It does not check that the actual leaf
// values when parsed are type compatible with the leaf types.
func (r *Value) TypeCheck() error {
return r.Walk(func(typ Type, body zcode.Bytes) error {
if typset, ok := typ.(*TypeSet); ok {
if err := checkSet(typset, body); err != nil {
return err
}
return SkipContainer
}
if typ, ok := typ.(*TypeEnum); ok {
if err := checkEnum(typ, body); err != nil {
return err
}
return SkipContainer
}
return nil
})
}
func checkSet(typ *TypeSet, body zcode.Bytes) error {
if body == nil {
return nil
}
it := body.Iter()
var prev zcode.Bytes
for !it.Done() {
tagAndBody, _, err := it.NextTagAndBody()
if err != nil {
return err
}
if prev != nil {
switch bytes.Compare(prev, tagAndBody) {
case 0:
err := errors.New("duplicate element")
return &RecordTypeError{Name: "<set element>", Type: typ.String(), Err: err}
case 1:
err := errors.New("elements not sorted")
return &RecordTypeError{Name: "<set element>", Type: typ.String(), Err: err}
}
}
prev = tagAndBody
}
return nil
}
func checkEnum(typ *TypeEnum, body zcode.Bytes) error {
if body == nil {
return nil
}
selector, err := DecodeUint(body)
if err != nil {
return err
}
if int(selector) >= len(typ.Symbols) {
return errors.New("enum selector out of range")
}
return nil
}
// Slice returns the encoded zcode.Bytes corresponding to the indicated
// column or an error if a problem was encountered. If the encoded bytes
// result is nil without error, then that columnn is unset in this record value.
func (r *Value) Slice(column int) (zcode.Bytes, error) {
var zv zcode.Bytes
for i, it := 0, r.Bytes.Iter(); i <= column; i++ {
if it.Done() {
return nil, ErrMissing
}
var err error
zv, _, err = it.Next()
if err != nil {
return nil, err
}
}
return zv, nil
}
func (r *Value) Columns() []Column {
return TypeRecordOf(r.Type).Columns
}
// Value returns the indicated column as a Value. If the column doesn't
// exist or another error occurs, the nil Value is returned.
func (r *Value) ValueByColumn(col int) Value {
zv, err := r.Slice(col)
if err != nil {
return Value{}
}
return Value{r.Columns()[col].Type, zv}
}
func (r *Value) ValueByField(field string) (Value, error) {
col, ok := r.ColumnOfField(field)
if !ok {
return Value{}, ErrMissing
}
return r.ValueByColumn(col), nil
}
func (r *Value) ColumnOfField(field string) (int, bool) {
return TypeRecordOf(r.Type).ColumnOfField(field)
}
func (r *Value) TypeOfColumn(col int) Type {
return TypeRecordOf(r.Type).Columns[col].Type
}
func (r *Value) Access(field string) (Value, error) {
col, ok := r.ColumnOfField(field)
if !ok {
return Value{}, ErrMissing
}
return r.ValueByColumn(col), nil
}
func (r *Value) Deref(path field.Path) (Value, error) {
v := *r
for _, f := range path {
typ := TypeRecordOf(v.Type)
if typ == nil {
return Value{}, errors.New("field access on non-record value")
}
var err error
v, err = NewValue(typ, v.Bytes).Access(f)
if err != nil {
return Value{}, err
}
}
return v, nil
}
func (r *Value) AccessString(field string) (string, error) {
v, err := r.Access(field)
if err != nil {
return "", err
}
switch AliasOf(v.Type).(type) {
case *TypeOfString, *TypeOfBstring:
return DecodeString(v.Bytes)
default:
return "", ErrTypeMismatch
}
}
func (r *Value) AccessBool(field string) (bool, error) {
v, err := r.Access(field)
if err != nil {
return false, err
}
if _, ok := AliasOf(v.Type).(*TypeOfBool); !ok {
return false, ErrTypeMismatch
}
return DecodeBool(v.Bytes)
}
func (r *Value) AccessInt(field string) (int64, error) {
v, err := r.Access(field)
if err != nil {
return 0, err
}
switch AliasOf(v.Type).(type) {
case *TypeOfUint8:
b, err := DecodeUint(v.Bytes)
return int64(b), err
case *TypeOfInt16, *TypeOfInt32, *TypeOfInt64:
return DecodeInt(v.Bytes)
case *TypeOfUint16, *TypeOfUint32:
v, err := DecodeUint(v.Bytes)
return int64(v), err
case *TypeOfUint64:
v, err := DecodeUint(v.Bytes)
if v > math.MaxInt64 {
return 0, errors.New("conversion from uint64 to signed int results in overflow")
}
return int64(v), err
}
return 0, ErrTypeMismatch
}
func (r *Value) AccessIP(field string) (net.IP, error) {
v, err := r.Access(field)
if err != nil {
return nil, err
}
if _, ok := AliasOf(v.Type).(*TypeOfIP); !ok {
return nil, ErrTypeMismatch
}
return DecodeIP(v.Bytes)
}
func (r *Value) AccessTime(field string) (nano.Ts, error) {
v, err := r.Access(field)
if err != nil {
return 0, err
}
if _, ok := AliasOf(v.Type).(*TypeOfTime); !ok {
return 0, ErrTypeMismatch
}
return DecodeTime(v.Bytes)
}
func (r *Value) AccessTimeByColumn(colno int) (nano.Ts, error) {
zv, err := r.Slice(colno)
if err != nil {
return 0, err
}
return DecodeTime(zv)
}