-
Notifications
You must be signed in to change notification settings - Fork 66
/
walk.go
126 lines (116 loc) · 2.79 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
package super
import (
"errors"
"github.com/brimdata/super/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 *TypeNamed:
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)
case *TypeError:
return Walk(typ.Type, body, visit)
}
return nil
}
func walkRecord(typ *TypeRecord, body zcode.Bytes, visit Visitor) error {
if body == nil {
return nil
}
it := body.Iter()
for _, f := range typ.Fields {
if it.Done() {
return ErrMissingField
}
if err := Walk(f.Type, it.Next(), 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() {
if err := Walk(inner, it.Next(), 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 {
return errors.New("union has empty body")
}
it := body.Iter()
tag := DecodeInt(it.Next())
inner, err := typ.Type(int(tag))
if err != nil {
return err
}
body = it.Next()
if !it.Done() {
return errors.New("union value container has more than two items")
}
return Walk(inner, body, visit)
}
func walkSet(typ *TypeSet, body zcode.Bytes, visit Visitor) error {
if body == nil {
return nil
}
inner := TypeUnder(InnerType(typ))
it := body.Iter()
for !it.Done() {
if err := Walk(inner, it.Next(), visit); err != nil {
return err
}
}
return nil
}
func walkMap(typ *TypeMap, body zcode.Bytes, visit Visitor) error {
if body == nil {
return nil
}
keyType := TypeUnder(typ.KeyType)
valType := TypeUnder(typ.ValType)
it := body.Iter()
for !it.Done() {
if err := Walk(keyType, it.Next(), visit); err != nil {
return err
}
if err := Walk(valType, it.Next(), visit); err != nil {
return err
}
}
return nil
}