-
Notifications
You must be signed in to change notification settings - Fork 67
/
walk.go
194 lines (183 loc) · 4.37 KB
/
walk.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
package zed
import (
"errors"
"github.com/brimdata/zed/zcode"
)
// A Visitor is called for each value in a record encountered by
// Walk. If the visitor returns an error, the walk stops and that
// error will be returned to the caller of Walk(). The sole exception
// is when the visitor returns the special value SkipContainer.
type Visitor func(typ Type, body zcode.Bytes) error
// SkipContainer is used as a return value from Visitors to indicate
// that the container passed in the call should not be visited. It is
// not returned as an error by any function.
var SkipContainer = errors.New("skip this container")
func Walk(typ Type, body zcode.Bytes, visit Visitor) error {
if err := visit(typ, body); err != nil {
if err == SkipContainer {
return nil
}
return err
}
switch typ := typ.(type) {
case *TypeAlias:
return Walk(typ.Type, body, visit)
case *TypeRecord:
return walkRecord(typ, body, visit)
case *TypeArray:
return walkArray(typ, body, visit)
case *TypeSet:
return walkSet(typ, body, visit)
case *TypeUnion:
return walkUnion(typ, body, visit)
case *TypeMap:
return walkMap(typ, body, visit)
}
return nil
}
func checkKind(name string, typ Type, body zcode.Bytes, container bool) error {
if body == nil {
return nil
}
isContainer := IsContainerType(typ)
if isContainer == container {
return nil
}
var err error
if isContainer {
err = ErrNotContainer
} else {
err = ErrNotPrimitive
}
return &RecordTypeError{Name: name, Type: typ.String(), Err: err}
}
func walkRecord(typ *TypeRecord, body zcode.Bytes, visit Visitor) error {
if body == nil {
return nil
}
it := body.Iter()
for _, col := range typ.Columns {
if it.Done() {
return &RecordTypeError{Name: string(col.Name), Type: col.Type.String(), Err: ErrMissingField}
}
body, container, err := it.Next()
if err != nil {
return err
}
if err := checkKind(col.Name, col.Type, body, container); err != nil {
return err
}
if err := Walk(col.Type, body, visit); err != nil {
return err
}
}
return nil
}
func walkArray(typ *TypeArray, body zcode.Bytes, visit Visitor) error {
if body == nil {
return nil
}
inner := InnerType(typ)
it := body.Iter()
for !it.Done() {
body, container, err := it.Next()
if err != nil {
return err
}
if err := checkKind("<array element>", inner, body, container); err != nil {
return err
}
if err := Walk(inner, body, visit); err != nil {
return err
}
}
return nil
}
func walkUnion(typ *TypeUnion, body zcode.Bytes, visit Visitor) error {
if body == nil {
return nil
}
if len(body) == 0 {
err := errors.New("union as empty body")
return &RecordTypeError{Name: "<union type>", Type: typ.String(), Err: err}
}
it := body.Iter()
v, container, err := it.Next()
if err != nil {
return err
}
if container {
return ErrBadValue
}
selector, err := DecodeInt(v)
if err != nil {
return err
}
inner, err := typ.Type(int(selector))
if err != nil {
return err
}
body, container, err = it.Next()
if err != nil {
return err
}
if !it.Done() {
err := errors.New("union value container has more than two items")
return &RecordTypeError{Name: "<union>", Type: typ.String(), Err: err}
}
if err := checkKind("<union body>", inner, body, container); err != nil {
return err
}
return Walk(inner, body, visit)
}
func walkSet(typ *TypeSet, body zcode.Bytes, visit Visitor) error {
if body == nil {
return nil
}
inner := AliasOf(InnerType(typ))
it := body.Iter()
for !it.Done() {
body, container, err := it.Next()
if err != nil {
return err
}
if err := checkKind("<set element>", inner, body, container); err != nil {
return err
}
if err := Walk(inner, body, visit); err != nil {
return err
}
}
return nil
}
func walkMap(typ *TypeMap, body zcode.Bytes, visit Visitor) error {
if body == nil {
return nil
}
keyType := AliasOf(typ.KeyType)
valType := AliasOf(typ.ValType)
it := body.Iter()
for !it.Done() {
body, container, err := it.Next()
if err != nil {
return err
}
if err := checkKind("<map key>", keyType, body, container); err != nil {
return err
}
if err := Walk(keyType, body, visit); err != nil {
return err
}
body, container, err = it.Next()
if err != nil {
return err
}
if err := checkKind("<map value>", valType, body, container); err != nil {
return err
}
if err := Walk(valType, body, visit); err != nil {
return err
}
}
return nil
}