-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjsonl_reader.go
60 lines (50 loc) · 1.25 KB
/
jsonl_reader.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
package ratchet_processors
import (
"bufio"
"encoding/json"
"errors"
"io"
"github.com/licaonfee/ratchet/data"
"github.com/licaonfee/ratchet/processors"
"github.com/licaonfee/ratchet/util"
)
type JSONLReader struct {
reader *bufio.Reader
}
// Assert JSONLReader satisfies the interface processors.DataProcessor
var _ processors.DataProcessor = &JSONLReader{}
// NewJSONLReader returns a new JSONLReader wrapping the given io.Reader object
func NewJSONLReader(r io.Reader) *JSONLReader {
return &JSONLReader{
reader: bufio.NewReader(r),
}
}
func (r *JSONLReader) ProcessData(d data.JSON, outputChan chan data.JSON, killChan chan error) {
// TODO: This allocates more than is necessary but at least it can handle large lines
Outer:
for {
var line []byte
for {
chunk, isPrefix, err := r.reader.ReadLine()
if err != nil {
if err == io.EOF {
break Outer
}
util.KillPipelineIfErr(err, killChan)
}
line = append(line, chunk...)
if !isPrefix {
break
}
}
if !json.Valid(line) {
util.KillPipelineIfErr(errors.New("Not valid JSON"), killChan)
}
outputChan <- line
}
}
func (r *JSONLReader) Finish(outputChan chan data.JSON, killChan chan error) {
}
func (r *JSONLReader) String() string {
return "JSONLReader"
}