-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream.go
558 lines (501 loc) · 12.6 KB
/
stream.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
package jspath
import (
"context"
"encoding/json"
"io"
"strings"
"sync"
"github.com/gobwas/glob"
)
const (
tokenTopValue = iota
tokenArrayStart
tokenArrayValue
tokenArrayComma
tokenObjectStart
tokenObjectKey
tokenObjectColon
tokenObjectValue
tokenObjectComma
)
type UnmarshalerStream interface {
AtPath() string
//UnmarshalStream is called once the patch is matched
//the content of the message is only valid until the function return
UnmarshalStream(key []byte, message json.RawMessage) error
}
// A StreamDecoder reads and decodes JSON values from an input stream at specified json path.
type StreamDecoder struct {
r io.Reader
buf []byte
scanp int // start of unread data in buf
scanned int64 // amount of data already scanned
scan scanner
errMu sync.Mutex
err error
tokenState int
tokenStack []int
context context.Context
done chan struct{}
path pathBuilder
}
// NewStreamDecoder returns a new StreamDecoder that reads from r.
//
// The StreamDecoder introduces its own buffering and may
// read data from r beyond the JSON values requested.
func NewStreamDecoder(r io.Reader) *StreamDecoder {
return &StreamDecoder{r: r, path: newPathBuilder(), done: make(chan struct{}, 0), context: context.Background()}
}
func (dec *StreamDecoder) WithContext(ctx context.Context) {
dec.context = ctx
}
func (dec *StreamDecoder) Decode(itemDecoders ...UnmarshalerStream) (err error) {
var decoders = make([]decoder, 0, len(itemDecoders))
for i := range itemDecoders {
matcher, err := dec.compilePath(itemDecoders[i].AtPath())
if err != nil {
return err
}
decoders = append(decoders, decoder{unmarshaler: itemDecoders[i], matcher: matcher})
}
go dec.decode(decoders...)
<-dec.Done()
return dec.err
}
func (dec *StreamDecoder) DecodePath(jsPath string, onPath func(key []byte, message json.RawMessage) error) (err error) {
matcher, err := dec.compilePath(jsPath)
if err != nil {
return err
}
go dec.decode(decoder{unmarshaler: NewRawStreamUnmarshaler(jsPath, onPath), matcher: matcher})
<-dec.Done()
return dec.err
}
func (dec *StreamDecoder) Done() <-chan struct{} {
return dec.done
}
//Err can only be called after the decode finish
func (dec *StreamDecoder) Err() error {
return dec.err
}
func (dec *StreamDecoder) Reset(reader io.Reader) (err error) {
select {
case <-dec.done:
//ok
default:
panic("cannot call reset while decoder is running")
}
dec.done = make(chan struct{})
dec.path.Reset()
dec.tokenStack = dec.tokenStack[0:0]
dec.tokenState = 0
dec.context = context.Background()
dec.scan.reset()
dec.buf = dec.buf[0:0]
dec.scanned = 0
dec.scanp = 0
dec.r = reader
return
}
func (dec *StreamDecoder) decode(decoders ...decoder) {
defer func() {
close(dec.done)
}()
for {
select {
case <-dec.context.Done():
dec.err = dec.context.Err()
return
default:
}
c, err := dec.peek()
if err != nil {
if err == io.EOF {
break
}
dec.err = err
return
}
switch c {
case '[':
if !dec.tokenValueAllowed() {
dec.err = dec.tokenError(c)
return
}
if dec.more() {
curPath := dec.path.PathBytes()
dec.path.StartArray()
match, itemDecoder := matcher(decoders).match(BytesToString(curPath))
if match {
bytes, err := dec.decodeBytes()
if err != nil {
if err == io.EOF {
break
}
dec.err = err
return
}
//update state
dec.path.EndArray()
dec.tokenValueEnd()
if err := itemDecoder.unmarshaler.UnmarshalStream(curPath, bytes); err != nil {
dec.err = err
return
}
continue
}
}
dec.scanp++
dec.tokenStack = append(dec.tokenStack, dec.tokenState)
dec.tokenState = tokenArrayStart
continue
case ']':
if dec.tokenState != tokenArrayStart && dec.tokenState != tokenArrayComma {
dec.err = dec.tokenError(c)
return
}
dec.scanp++
dec.tokenState = dec.tokenStack[len(dec.tokenStack)-1]
dec.tokenStack = dec.tokenStack[:len(dec.tokenStack)-1]
dec.path.EndObject()
dec.tokenValueEnd()
continue
case '{':
if !dec.tokenValueAllowed() {
dec.err = dec.tokenError(c)
return
}
if dec.more() {
curPath := dec.path.PathBytes()
match, itemDecoder := matcher(decoders).match(BytesToString(curPath))
if match {
bytes, err := dec.decodeBytes()
if err != nil {
if err == io.EOF {
break
}
dec.err = err
return
}
if err := itemDecoder.unmarshaler.UnmarshalStream(curPath, bytes); err != nil {
dec.err = err
return
}
dec.tokenValueEnd()
continue
}
}
dec.scanp++
dec.tokenStack = append(dec.tokenStack, dec.tokenState)
dec.tokenState = tokenObjectStart
dec.path.StartObject()
continue
case '}':
if dec.tokenState != tokenObjectStart && dec.tokenState != tokenObjectComma {
dec.err = dec.tokenError(c)
return
}
dec.scanp++
dec.tokenState = dec.tokenStack[len(dec.tokenStack)-1]
dec.tokenStack = dec.tokenStack[:len(dec.tokenStack)-1]
dec.path.EndObject()
dec.tokenValueEnd()
continue
case ':':
if dec.tokenState != tokenObjectColon {
dec.err = dec.tokenError(c)
return
}
dec.scanp++
dec.tokenState = tokenObjectValue
continue
case ',':
if dec.tokenState == tokenArrayComma {
dec.scanp++
dec.path.IncrementArrayIndex()
dec.tokenState = tokenArrayValue
continue
}
if dec.tokenState == tokenObjectComma {
dec.scanp++
dec.tokenState = tokenObjectKey
continue
}
dec.err = dec.tokenError(c)
return
case '"':
if dec.tokenState == tokenObjectStart || dec.tokenState == tokenObjectKey {
old := dec.tokenState
dec.tokenState = tokenTopValue
keyBytes, err := dec.decodeBytes()
dec.tokenState = old
if err != nil {
dec.err = err
return
}
dec.tokenState = tokenObjectColon
dec.path.SetObjectKey(keyBytes[1 : len(keyBytes)-1])
continue
}
fallthrough
default:
if !dec.tokenValueAllowed() {
dec.err = dec.tokenError(c)
return
}
if bytes, err := dec.decodeBytes(); err != nil {
dec.err = err
return
} else {
curPath := dec.path.PathBytes()
if match, itemDecoder := matcher(decoders).match(BytesToString(curPath)); match {
if err := itemDecoder.unmarshaler.UnmarshalStream(curPath, bytes); err != nil {
dec.err = err
return
}
}
}
}
}
dec.err = nil
return
}
func (dec *StreamDecoder) decodeBytes() ([]byte, error) {
if dec.err != nil {
return nil, dec.err
}
if err := dec.tokenPrepareForDecode(); err != nil {
return nil, err
}
if !dec.tokenValueAllowed() {
return nil, &SyntaxError{msg: "not at beginning of value", Offset: dec.offset()}
}
// Read whole value into buffer.
n, err := dec.readValue()
if err != nil {
return nil, err
}
out := dec.buf[dec.scanp : dec.scanp+n]
dec.scanp += n
// fixup token streaming state
dec.tokenValueEnd()
return out, nil
}
// readValue reads a JSON value into dec.buf.
// It returns the length of the encoding.
func (dec *StreamDecoder) readValue() (int, error) {
dec.scan.reset()
scanp := dec.scanp
var err error
Input:
for {
// Look in the buffer for a new value.
for i, c := range dec.buf[scanp:] {
dec.scan.bytes++
v := dec.scan.step(&dec.scan, c)
if v == scanEnd {
scanp += i
break Input
}
// scanEnd is delayed one byte.
// We might block trying to get that byte from src,
// so instead invent a space byte.
if (v == scanEndObject || v == scanEndArray) && dec.scan.step(&dec.scan, ' ') == scanEnd {
scanp += i + 1
break Input
}
if v == scanError {
dec.err = dec.scan.err
return 0, dec.scan.err
}
}
scanp = len(dec.buf)
// Did the last read have an error?
// Delayed until now to allow buffer scan.
if err != nil {
if err == io.EOF {
if dec.scan.step(&dec.scan, ' ') == scanEnd {
break Input
}
if nonSpace(dec.buf) {
err = io.ErrUnexpectedEOF
}
}
dec.err = err
return 0, err
}
n := scanp - dec.scanp
err = dec.refill()
scanp = dec.scanp + n
}
return scanp - dec.scanp, nil
}
func (dec *StreamDecoder) refill() error {
// Make room to read more into the buffer.
// First slide down data already consumed.
if dec.scanp > 0 {
dec.scanned += int64(dec.scanp)
n := copy(dec.buf, dec.buf[dec.scanp:])
dec.buf = dec.buf[:n]
dec.scanp = 0
}
// Grow buffer if not large enough.
const minRead = 4096
if cap(dec.buf)-len(dec.buf) < minRead {
newBuf := make([]byte, len(dec.buf), 2*cap(dec.buf)+minRead)
copy(newBuf, dec.buf)
dec.buf = newBuf
}
// Read. Delay error for next iteration (after scan).
n, err := dec.r.Read(dec.buf[len(dec.buf):cap(dec.buf)])
dec.buf = dec.buf[0 : len(dec.buf)+n]
return err
}
func nonSpace(b []byte) bool {
for _, c := range b {
if !isSpace(c) {
return true
}
}
return false
}
// advance tokenstate from a separator state to a value state
func (dec *StreamDecoder) tokenPrepareForDecode() error {
// Note: Not calling peek before switch, to avoid
// putting peek into the standard UnmarshalStream path.
// peek is only called when using the Token API.
switch dec.tokenState {
case tokenArrayComma:
c, err := dec.peek()
if err != nil {
return err
}
if c != ',' {
return &SyntaxError{msg: "expected comma after array element", Offset: dec.offset()}
}
dec.scanp++
dec.tokenState = tokenArrayValue
case tokenObjectColon:
c, err := dec.peek()
if err != nil {
return err
}
if c != ':' {
return &SyntaxError{msg: "expected colon after object key", Offset: dec.offset()}
}
dec.scanp++
dec.tokenState = tokenObjectValue
}
return nil
}
func (dec *StreamDecoder) tokenValueAllowed() bool {
switch dec.tokenState {
case tokenTopValue, tokenArrayStart, tokenArrayValue, tokenObjectValue:
return true
}
return false
}
func (dec *StreamDecoder) tokenValueEnd() {
switch dec.tokenState {
case tokenArrayStart, tokenArrayValue:
dec.tokenState = tokenArrayComma
case tokenObjectValue:
dec.tokenState = tokenObjectComma
}
}
func (dec *StreamDecoder) tokenError(c byte) error {
var context string
switch dec.tokenState {
case tokenTopValue:
context = " looking for beginning of value"
case tokenArrayStart, tokenArrayValue, tokenObjectValue:
context = " looking for beginning of value"
case tokenArrayComma:
context = " after array element"
case tokenObjectKey:
context = " looking for beginning of object key string"
case tokenObjectColon:
context = " after object key"
case tokenObjectComma:
context = " after object key:value pair"
}
return &SyntaxError{msg: "invalid character " + quoteChar(c) + " " + context, Offset: dec.offset()}
}
// more reports whether there is another element in the
// current array or object being parsed.
func (dec *StreamDecoder) more() bool {
c, err := dec.peek()
return err == nil && c != ']' && c != '}'
}
func (dec *StreamDecoder) peek() (byte, error) {
var err error
for {
for i := dec.scanp; i < len(dec.buf); i++ {
c := dec.buf[i]
if isSpace(c) {
continue
}
dec.scanp = i
return c, nil
}
// buffer has been scanned, now report any error
if err != nil {
return 0, err
}
err = dec.refill()
}
}
func (dec *StreamDecoder) offset() int64 {
return dec.scanned + int64(dec.scanp)
}
func (dec *StreamDecoder) compilePath(jsPath string) (func(curPath, jsPath string) bool, error) {
if i := strings.IndexByte(jsPath, '*'); i != -1 {
s := escapeGlob(jsPath)
re, err := glob.Compile(s)
if err != nil {
return nil, err
}
return func(curPath string, jsPath string) bool {
return re.Match(curPath)
}, nil
}
return func(curPath string, jsPath string) bool {
//exception for root path
if curPath == "$" && jsPath == "$." {
return true
}
return curPath == jsPath
}, nil
}
func escapeGlob(jsPath string) string {
s := strings.Replace(jsPath, "[", "\\[", -1)
s = strings.Replace(s, "]", "\\]", -1)
return s
}
type decoder struct {
unmarshaler UnmarshalerStream
matcher func(curPath, jsPath string) bool
}
type matcher []decoder
func (matchers matcher) match(curPath string) (bool, decoder) {
for i := range matchers {
match := matchers[i].matcher(curPath, matchers[i].unmarshaler.AtPath())
if match {
return true, matchers[i]
}
}
return false, decoder{}
}
func NewRawStreamUnmarshaler(matchPath string, onMatch func(key []byte, message json.RawMessage) error) UnmarshalerStream {
return &RawStreamUnmarshaler{matchPath: matchPath, onMatch: onMatch}
}
type RawStreamUnmarshaler struct {
matchPath string
onMatch func(key []byte, message json.RawMessage) error
}
func (r *RawStreamUnmarshaler) AtPath() string {
return r.matchPath
}
func (r *RawStreamUnmarshaler) UnmarshalStream(key []byte, message json.RawMessage) error {
return r.onMatch(key, message)
}