-
Notifications
You must be signed in to change notification settings - Fork 67
/
builder.go
180 lines (163 loc) · 4.26 KB
/
builder.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
package jsonio
import (
"errors"
"sort"
"github.com/brimdata/zed"
"github.com/brimdata/zed/zcode"
)
type builder struct {
zctx *zed.Context
containers []int // Stack of open containers (as indexes into items).
items []item // Stack of items.
// These exist only to reduce memory allocations.
fields []zed.Field
itemptrs []*item
types []zed.Type
val zed.Value
}
type item struct {
fieldName string
typ zed.Type
zb zcode.Builder
}
func (b *builder) reset() {
b.containers = b.containers[:0]
b.items = b.items[:0]
}
func (b *builder) pushPrimitiveItem(fieldName string, typ zed.Type, bytes zcode.Bytes) {
i := b.pushItem(fieldName)
i.typ = typ
i.zb.Append(bytes)
}
func (b *builder) pushItem(fieldName string) *item {
n := len(b.items)
if n == cap(b.items) {
b.items = append(b.items, item{})
}
b.items = b.items[:n+1]
i := &b.items[n]
i.fieldName = fieldName
i.zb.Truncate()
return i
}
func (b *builder) beginContainer(fieldName string) {
// This item represents the container. endArray or endRecord will set
// its type and bytes.
b.pushItem(fieldName)
b.containers = append(b.containers, len(b.items))
}
func (b *builder) endContainer() (container *item, items []item) {
start := b.containers[len(b.containers)-1]
b.containers = b.containers[:len(b.containers)-1]
items = b.items[start:]
b.items = b.items[:start]
return &b.items[start-1], items
}
func (b *builder) endArray() {
container, items := b.endContainer()
b.types = b.types[:0]
for i := range items {
if items[i].typ != zed.TypeNull {
b.types = append(b.types, items[i].typ)
}
}
sort.Slice(b.types, func(i, j int) bool { return b.types[i].ID() < b.types[j].ID() })
dedupedTypes := b.types[:0]
var prev zed.Type
for _, t := range b.types {
// JSON doesn't use named types, so even though b.types was
// sorted by zed.Type.ID above, we can compare elements directly
// without calling zed.TypeUnder.
if t != prev {
dedupedTypes = append(dedupedTypes, t)
prev = t
}
}
b.types = dedupedTypes
container.zb.BeginContainer()
switch len(b.types) {
case 0:
container.typ = b.zctx.LookupTypeArray(zed.TypeNull)
for range items {
container.zb.Append(nil)
}
case 1:
container.typ = b.zctx.LookupTypeArray(b.types[0])
for i := range items {
container.zb.Append(items[i].zb.Bytes().Body())
}
default:
union := b.zctx.LookupTypeUnion(b.types)
container.typ = b.zctx.LookupTypeArray(union)
for i := range items {
if bytes := items[i].zb.Bytes().Body(); bytes == nil {
container.zb.Append(nil)
} else {
tag := union.TagOf(items[i].typ)
zed.BuildUnion(&container.zb, tag, bytes)
}
}
}
container.zb.EndContainer()
}
func (b *builder) endRecord() {
container, items := b.endContainer()
b.itemptrs = b.itemptrs[:0]
for i := range items {
b.itemptrs = append(b.itemptrs, &items[i])
}
itemptrs := b.itemptrs
for {
b.fields = b.fields[:0]
for _, item := range itemptrs {
b.fields = append(b.fields, zed.NewField(item.fieldName, item.typ))
}
var err error
container.typ, err = b.zctx.LookupTypeRecord(b.fields)
if err == nil {
break
}
var dferr *zed.DuplicateFieldError
if !errors.As(err, &dferr) {
panic(err)
}
// removeDuplicateItems operates on itemptrs rather than items
// to avoid copying zcode.Builders.
itemptrs = removeDuplicateItems(itemptrs, dferr.Name)
}
container.zb.BeginContainer()
for _, item := range itemptrs {
container.zb.Append(item.zb.Bytes().Body())
}
container.zb.EndContainer()
}
// removeDuplicateItems removes from itemptrs any item whose fieldName field
// equals name except for the last such item, which it moves to the position at
// which it found the first such item. (This is how both ECMAScript 2015 and jq
// handle duplicate object keys.)
func removeDuplicateItems(itemptrs []*item, name string) []*item {
out := itemptrs[:0]
var first = -1
for i, item := range itemptrs {
if item.fieldName == name {
if first >= 0 {
out[first] = item
continue
}
first = i
}
out = append(out, item)
}
return out
}
func (b *builder) value() *zed.Value {
if len(b.containers) > 0 {
panic("open container")
}
if len(b.items) > 1 {
panic("multiple items")
}
item := &b.items[0]
b.val = *zed.NewValue(item.typ, item.zb.Bytes().Body())
return &b.val
}