-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconcat.go
76 lines (67 loc) · 1.92 KB
/
concat.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
package jsonstream
import (
"encoding/json"
"io"
)
// ConcatReader reads concatenated documents. The input must be valid
// documents concatenated one after the other. This uses a
// json.Decoder to read the stream.
type ConcatReader struct {
d *json.Decoder
err error
}
// NewConcatReader returns a new stream reader
func NewConcatReader(r io.Reader) ConcatReader {
return ConcatReader{d: json.NewDecoder(r)}
}
// ReadRaw reads the next raw document. The returned buffer is a
// copy of the read buffer, so subsequent read calls will not
// overwrite the buffer. If the end of the stream is reached, returns
// io.EOF. If another error is detected (such as invalid
// document), the stream is tagged invalid and all subsequent calls
// will fail with the same error.
func (r ConcatReader) ReadRaw() ([]byte, error) {
if r.err != nil {
return nil, r.err
}
var m json.RawMessage
r.err = r.d.Decode(&m)
if r.err != nil {
return nil, r.err
}
return []byte(m), nil
}
// Unmarshal the next document from the input. Returns unmarshal error
// if there is any. If the end of stream is reached, returns
// io.EOF. If there is an error during unmarshaling, the rest of the
// stream cannot be recovered.
func (r ConcatReader) Unmarshal(out interface{}) error {
if r.err != nil {
return r.err
}
r.err = r.d.Decode(out)
return r.err
}
// ConcatWriter streams JSON documents by concatenating one after the
// other.
type ConcatWriter struct {
w io.Writer
}
// NewConcatWriter returns a new writer
func NewConcatWriter(w io.Writer) ConcatWriter {
return ConcatWriter{w: w}
}
// WriteRaw writes a raw JSON document. It does not validate if the
// doc is valid.
func (w ConcatWriter) WriteRaw(out []byte) error {
_, err := w.w.Write(out)
return err
}
// Marshal data as a JSON document to the output
func (w ConcatWriter) Marshal(data interface{}) error {
x, err := json.Marshal(data)
if err != nil {
return err
}
return w.WriteRaw(x)
}