-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoder.go
151 lines (127 loc) · 3.02 KB
/
decoder.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
package linebuf
import (
"bufio"
"encoding/json"
"io"
"math"
"reflect"
"code.cloudfoundry.org/bytefmt"
)
type DecoderOption (func(*Decoder) error)
/*
WithDecoderUnbuffered can be provided as option to NewDecoder, thus turning off buffer mode
*/
func WithDecoderUnbuffered() DecoderOption {
return func(dec *Decoder) error {
dec.unbuffered = true
return nil
}
}
/*
WithDecoderBuffersize can be used as option to NewDecoder, thus providing another buffersize than the default 4k
*/
func WithDecoderBuffersize(bufSize string) DecoderOption {
return func(dec *Decoder) error {
var (
bufBytes uint64
err error
)
if bufBytes, err = bytefmt.ToBytes(bufSize); err != nil {
return err
}
dec.bufBytes = bufBytes
return nil
}
}
/*
NewDecoder returns a linebuf decoder from an io.Reader while allowing to specify custom options like unbuffered mode and/or setting the buffer size.
The default buffer size is 4k
An error will be returned e.g. if parsing of the bufsize fails
*/
func NewDecoder(r io.Reader, options ...DecoderOption) (*Decoder, error) {
var (
err error
jsonDec *json.Decoder
option DecoderOption
dec = &Decoder{
err: nil,
r: r,
s: make(chan interface{}),
}
buf *bufio.Reader
)
for _, option = range options {
if err = option(dec); err != nil {
return nil, err
}
}
buf = bufio.NewReaderSize(r, int(math.Max(float64(dec.bufBytes), 4<<10)))
jsonDec = json.NewDecoder(buf)
jsonDec.UseNumber()
dec.buf = buf
dec.jsonDec = jsonDec
if !dec.unbuffered {
dec.s = make(chan interface{}, 100)
}
return dec, nil
}
/*
LastError returns the last error that occured or nil if everything worked well
*/
func (dec *Decoder) LastError() error {
return dec.err
}
func (dec *Decoder) read(v interface{}) error {
var (
err error
)
if err = dec.jsonDec.Decode(v); err != nil {
return err
}
return nil
}
/*
Decode decodes a single entry that is received by the underlaying io.Reader into a pointer to an object given via parameter "v".
If some errors occure, they will mainly be I/O errors
*/
func (dec *Decoder) Decode(v interface{}) error {
return dec.read(v)
}
/*
Stream starts decoding entries that are received by the underlaying io.Reader. Specify an optional pointer parameter `entry` that's used to decode JSON into a specific structure.
It returns a channel that can be used to receive the encoded entries inside a loop like:
for entry := range dec.Stream() {
// do sth.
}
To retrieve the last error that occured, use
dec.LastError()
*/
func (dec *Decoder) Stream(entry ...interface{}) <-chan interface{} {
var (
v interface{}
isPtr = false
)
if len(entry) > 0 {
v = entry[0]
isPtr = (reflect.ValueOf(v).Kind() == reflect.Ptr)
}
dec.once.Do(func() {
go func() {
for {
if dec.err = dec.read(&v); dec.err == io.EOF {
close(dec.s)
dec.err = nil
break
} else if dec.err != nil {
break
}
if isPtr {
dec.s <- reflect.ValueOf(v).Elem().Interface()
} else {
dec.s <- v
}
}
}()
})
return dec.s
}