-
Notifications
You must be signed in to change notification settings - Fork 1
/
tree.go
326 lines (292 loc) · 6.96 KB
/
tree.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
package tmpl
import (
"bytes"
"fmt"
"io"
"strconv"
"github.com/canopyclimate/golive/internal/json"
)
type Tree struct {
Statics []string
Dynamics []any // string | *Tree | []any
ExcludeStatics bool // controls if MarshalText Statics with serializing
Title string
Events [][]byte
isRange bool
rangeStep int
}
func NewTree() *Tree {
return &Tree{Statics: []string{""}}
}
func (t *Tree) AppendStatic(text string) {
if t.rangeStep > 0 {
// We're inside a range loop, and we've already seen all the statics.
return
}
// Prevent two consecutive statics by concatenation.
// TODO: this is potentially quadratic, consider storing statics as slices of strings instead
nDyn := t.nDyn()
if len(t.Statics) > nDyn {
t.Statics[len(t.Statics)-1] += text
return
}
t.Statics = append(t.Statics, text)
}
func (t *Tree) AppendDynamic(d string) {
t.appendDynamic(d)
}
func (t *Tree) AppendSub() *Tree {
sub := NewTree()
t.appendDynamic(sub)
return sub
}
// AppendRangeSub adds a range subnode to tree.
// Templates call it on entering a range statement.
func (t *Tree) AppendRangeSub() *Tree {
sub := NewTree()
sub.isRange = true
t.appendDynamic(sub)
return sub
}
// IncRangeStep records that a single range iteration has completed it.
func (t *Tree) IncRangeStep() {
if t == nil {
return
}
t.rangeStep++
}
func (t *Tree) appendDynamic(d any) {
// For non-ranges, we pair dynamics with statics directly.
if !t.isRange {
t.appendDynamicWithStatic(d)
return
}
// For ranges, the Dynamics are held in the interior slices, each of which is a []any.
// Append d to that inner slice, ensuring space first as needed.
if t.rangeStep >= len(t.Dynamics) {
t.Dynamics = append(t.Dynamics, []any(nil))
}
dyn := t.Dynamics[t.rangeStep].([]any)
t.Dynamics[t.rangeStep] = append(dyn, d)
// During the first range iteration, append placeholder statics in parallel with dynamics.
if t.rangeStep == 0 {
t.Statics = append(t.Statics, "")
}
}
// appendDynamicWithStatic adds a to t's dynamics,
// preserving the alternating static/dynamic layout.
// Note that the empty static that gets appended here
// will get replaced if AppendStatic gets called next.
func (t *Tree) appendDynamicWithStatic(a any) {
t.Dynamics = append(t.Dynamics, a)
t.Statics = append(t.Statics, "")
}
// nDyn returns the number of dynamic elements of t.
func (t *Tree) nDyn() int {
nDyn := len(t.Dynamics)
if !t.isRange || nDyn == 0 {
return nDyn
}
// In a range loop, the Dynamics are stored in the interior slices;
// the outer slice is for each iteration of the range loop.
// This must be identical for each element, so it suffices
// to look at index 0, if present.
return len(t.Dynamics[0].([]any))
}
// Valid performs an internal consistency check and returns an error if it fails.
func (t *Tree) Valid() error {
nDyn := t.nDyn()
if nDyn+1 != len(t.Statics) {
return fmt.Errorf("nDyn = %d, nStatic = %d, want nDyn + 1 = nStatic", nDyn, len(t.Statics))
}
return nil
}
func Diff(a, b *Tree) {
panic("TODO")
}
// JSON returns a JSON representation of the tree.
func (t *Tree) JSON() ([]byte, error) {
b := new(bytes.Buffer)
_, err := t.WriteTo(b)
if err != nil {
return nil, err
}
return b.Bytes(), nil
}
type countWriter struct {
n int64
w io.Writer
buf []byte // re-usable buffer
err error
}
func (cw *countWriter) writeBytes(p []byte) {
if cw.err != nil {
return
}
n, err := cw.w.Write(p)
cw.n += int64(n)
cw.err = err
}
func (cw *countWriter) writeByte(b byte) {
if cw.err != nil {
return
}
cw.buf = cw.buf[:0]
cw.buf = append(cw.buf, b)
cw.writeBytes(cw.buf)
}
func (cw *countWriter) writeString(s string) {
if cw.err != nil {
return
}
if len(s) == 1 {
cw.writeByte(s[0])
return
}
if sw, ok := cw.w.(io.StringWriter); ok {
n, err := sw.WriteString(s)
cw.n += int64(n)
cw.err = err
return
}
cw.buf = cw.buf[:0]
cw.buf = append(cw.buf, s...)
cw.writeBytes(cw.buf)
}
func (cw *countWriter) writeInt(x int) {
if cw.err != nil {
return
}
cw.buf = cw.buf[:0]
cw.buf = strconv.AppendInt(cw.buf, int64(x), 10)
cw.writeBytes(cw.buf)
}
func (cw *countWriter) writeJSONString(s string) {
if cw.err != nil {
return
}
cw.buf = cw.buf[:0]
var err error
cw.buf, err = json.AppendString(cw.buf, s)
if err != nil {
cw.err = err
return
}
cw.writeBytes(cw.buf)
}
func (cw *countWriter) writeLeadingComma(i int) {
if i == 0 {
return
}
cw.writeByte(',')
}
func (cw *countWriter) writeDynamic(d any) {
if cw.err != nil {
return
}
switch d := d.(type) {
case string:
cw.writeJSONString(d)
case *Tree:
// TODO - we want to include statics and diff them out elsewhere
d.writeTo(cw)
default:
panic(fmt.Sprintf("unexpected type of Dynamic: %T, want string or *Tree, value is: %v", d, d))
}
}
// WriteTo writes a JSON representation of the tree to w.
func (t *Tree) WriteTo(w io.Writer) (written int64, err error) {
cw := &countWriter{w: w}
t.writeTo(cw)
return cw.n, cw.err
}
func (t *Tree) writeTo(cw *countWriter) {
if cw.err != nil {
return
}
if len(t.Dynamics) == 0 {
if len(t.Statics) != 1 {
panic(fmt.Sprintf("internal error: malformed tree with 0 dynamics and %d statics", len(t.Statics)))
}
cw.writeJSONString(t.Statics[0])
return
}
cw.writeString(`{`)
if !t.isRange {
for i, d := range t.Dynamics {
cw.writeLeadingComma(i)
cw.writeString(`"`)
cw.writeInt(i)
cw.writeString(`":`)
cw.writeDynamic(d)
}
} else {
cw.writeString(`"d":[`)
for i, d := range t.Dynamics {
cw.writeLeadingComma(i)
cw.writeString(`[`)
for j, dd := range d.([]any) {
cw.writeLeadingComma(j)
cw.writeDynamic(dd)
}
cw.writeString(`]`)
}
cw.writeString(`]`)
}
if !t.ExcludeStatics {
cw.writeString(`,"s":[`)
for i, s := range t.Statics {
cw.writeLeadingComma(i)
// TODO: json encode s when we first receive it, instead of every time
cw.writeJSONString(s)
}
cw.writeString(`]`)
}
if t.Title != "" {
cw.writeString(`,"t":`)
cw.writeJSONString(t.Title)
}
if len(t.Events) > 0 {
cw.writeString(`,"e":[`)
for i, e := range t.Events {
cw.writeLeadingComma(i)
cw.writeBytes(e)
}
cw.writeString(`]`)
}
cw.writeString(`}`)
}
// RenderTo renders the content represented by t to w.
func (t *Tree) RenderTo(w io.Writer) error {
if t.Events != nil || t.Title != "" {
return fmt.Errorf("RenderTo does not support events or title")
}
dynamics := t.Dynamics
if !t.isRange {
dynamics = []any{t.Dynamics}
}
for _, dyns := range dynamics {
dyns := dyns.([]any)
for i := 0; i < len(t.Statics); i++ {
if _, err := io.WriteString(w, t.Statics[i]); err != nil {
return err
}
if i >= len(dyns) {
continue
}
switch dyn := dyns[i].(type) {
case string:
if _, err := io.WriteString(w, dyn); err != nil {
return err
}
case *Tree:
if err := dyn.RenderTo(w); err != nil {
return err
}
default:
panic(fmt.Sprintf("unexpected type of Dynamic: %T, want string or *Tree, value is: %v", dyn, dyn))
}
}
}
return nil
}