forked from olivere/ndjson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader.go
71 lines (61 loc) · 2 KB
/
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
61
62
63
64
65
66
67
68
69
70
71
package ndjson
import (
"bufio"
"encoding/json"
"io"
)
var (
_ io.Reader = (*Reader)(nil)
)
// Reader allows reading line-oriented JSON data following the
// ndjson spec at http://ndjson.org/.
type Reader struct {
r io.Reader
s *bufio.Scanner
}
// NewReader returns a new reader, using the underlying io.Reader
// as input.
func NewReader(r io.Reader) *Reader {
s := bufio.NewScanner(r)
return &Reader{r: r, s: s}
}
// NewReaderSize returns a new reader whose buffer has the specified max size, using the underlying io.Reader
// as input.
func NewReaderSize(r io.Reader, maxSize int) *Reader {
s := bufio.NewScanner(r)
buf := make([]byte, 0, bufio.MaxScanTokenSize)
s.Buffer(buf, maxSize)
return &Reader{r: r, s: s}
}
// Read reads data into p. It returns the number of bytes read into p.
// The bytes are taken from the underlying reader. Read follows the
// protocol defined by io.Reader.
func (r *Reader) Read(p []byte) (n int, err error) {
return r.r.Read(p)
}
// Next advances the Reader to the next line, which will then be available
// through the Decode method. It returns false when the reader stops,
// either by reaching the end of the input or an error. After Next returns
// false, the Err method will return any error that occured while reading,
// except if it was io.EOF, Err will return nil.
//
// Next might panic if the underlying split function returns too many tokens
// without advancing the input.
func (r *Reader) Next() bool {
return r.s.Scan()
}
// Bytes returns the most recent buffer generated by a call to Next.
// The underlying array may point to data that will be overwritten
// by a subsequent call to Next. It does no allocation.
func (r *Reader) Bytes() []byte {
return r.s.Bytes()
}
// Err returns the first non-EOF error that was encountered by the Reader.
func (r *Reader) Err() error {
return r.s.Err()
}
// Decode decodes the bytes read after the last call to Next into
// the specified value.
func (r *Reader) Decode(v interface{}) error {
return json.Unmarshal(r.s.Bytes(), v)
}