-
Notifications
You must be signed in to change notification settings - Fork 4
/
byline.go
322 lines (281 loc) · 7.25 KB
/
byline.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
package byline
import (
"bufio"
"bytes"
"errors"
"io"
"io/ioutil"
"regexp"
)
var (
// ErrOmitLine - error for Map*Err/AWKMode, for omitting current line
ErrOmitLine = errors.New("ErrOmitLine")
// ErrNilReader - error for provided reader being nil
ErrNilReader = errors.New("nil reader")
// default field separator
defaultFS = regexp.MustCompile(`\s+`)
// default line separator
defaultRS byte = '\n'
// for Grep* methods
nullBytes = []byte{}
// bytes.Buffer growth to this limit
bufferSizeLimit = 512
)
// Reader - line by line Reader
type Reader struct {
scanner *bufio.Scanner
buffer bytes.Buffer
existsData bool
filterFuncs []func(line []byte) ([]byte, error)
awkVars AWKVars
}
// AWKVars - settings for AWK mode, see man awk
type AWKVars struct {
NR int // number of the current line (begin from 1)
NF int // number of fields in the current line
RS byte // record separator, default is '\n'
FS *regexp.Regexp // field separator, default is `\s+`
}
// NewReader - get new line by line Reader
func NewReader(reader io.Reader) *Reader {
if reader == nil {
return nil
}
lr := &Reader{
scanner: bufio.NewScanner(reader),
existsData: true,
awkVars: AWKVars{
RS: defaultRS,
FS: defaultFS,
},
}
lr.scanner.Split(lr.scanLinesBySep)
lr.buffer.Grow(bufferSizeLimit)
return lr
}
func (lr *Reader) scanLinesBySep(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}
if i := bytes.IndexByte(data, lr.awkVars.RS); i >= 0 {
// We have a full RS-terminated line.
return i + 1, data[0 : i+1], nil
}
// If we're at EOF, we have a final, non-terminated line. Return it.
if atEOF {
return len(data), data, nil
}
// Request more data.
return 0, nil, nil
}
// Read - implement io.Reader interface
func (lr *Reader) Read(p []byte) (n int, err error) {
if lr == nil {
return 0, ErrNilReader
}
var (
bufErr, filterErr error
lineBytes []byte
)
for lr.existsData && bufErr == nil && lr.buffer.Len() < bufferSizeLimit {
if lr.existsData = lr.scanner.Scan(); !lr.existsData {
break
}
lineBytes = lr.scanner.Bytes()
lr.awkVars.NR++
for _, filterFunc := range lr.filterFuncs {
lineBytes, filterErr = filterFunc(lineBytes)
if filterErr != nil {
switch filterErr {
case ErrOmitLine:
lineBytes = nullBytes
default:
bufErr = filterErr
}
break
}
}
_, _ = lr.buffer.Write(lineBytes) // #nosec - err always is nil
}
if !lr.existsData && bufErr == nil {
bufErr = lr.scanner.Err()
}
n, err = lr.buffer.Read(p)
if err != nil && bufErr == nil {
bufErr = err
}
return n, bufErr
}
// Map - set filter function for process each line
func (lr *Reader) Map(filterFn func([]byte) []byte) *Reader {
if lr == nil {
return nil
}
return lr.MapErr(func(line []byte) ([]byte, error) {
return filterFn(line), nil
})
}
// MapErr - set filter function for process each line, returns error if needed (io.EOF for example)
func (lr *Reader) MapErr(filterFn func([]byte) ([]byte, error)) *Reader {
if lr == nil {
return nil
}
lr.filterFuncs = append(lr.filterFuncs, filterFn)
return lr
}
// MapString - set filter function for process each line as string
func (lr *Reader) MapString(filterFn func(string) string) *Reader {
if lr == nil {
return nil
}
return lr.MapErr(func(line []byte) ([]byte, error) {
return []byte(filterFn(string(line))), nil
})
}
// MapStringErr - set filter function for process each line as string, returns error if needed (io.EOF for example)
func (lr *Reader) MapStringErr(filterFn func(string) (string, error)) *Reader {
if lr == nil {
return nil
}
return lr.MapErr(func(line []byte) ([]byte, error) {
newString, err := filterFn(string(line))
return []byte(newString), err
})
}
// Each - processing each line.
// Do not save the value of the byte slice, since it can change in the next filter-steps.
func (lr *Reader) Each(filterFn func([]byte)) *Reader {
if lr == nil {
return nil
}
return lr.MapErr(func(line []byte) ([]byte, error) {
filterFn(line)
return line, nil
})
}
// EachString - processing each line as string
func (lr *Reader) EachString(filterFn func(string)) *Reader {
if lr == nil {
return nil
}
return lr.MapErr(func(line []byte) ([]byte, error) {
filterFn(string(line))
return line, nil
})
}
// Grep - grep lines by func
func (lr *Reader) Grep(filterFn func([]byte) bool) *Reader {
if lr == nil {
return nil
}
return lr.MapErr(func(line []byte) ([]byte, error) {
if filterFn(line) {
return line, nil
}
return nullBytes, ErrOmitLine
})
}
// GrepString - grep lines as string by func
func (lr *Reader) GrepString(filterFn func(string) bool) *Reader {
if lr == nil {
return nil
}
return lr.Grep(func(line []byte) bool {
return filterFn(string(line))
})
}
// GrepByRegexp - grep lines by regexp
func (lr *Reader) GrepByRegexp(re *regexp.Regexp) *Reader {
if lr == nil {
return nil
}
return lr.Grep(func(line []byte) bool {
return re.Match(line)
})
}
// SetRS - set lines (records) separator
func (lr *Reader) SetRS(rs byte) *Reader {
if lr == nil {
return nil
}
lr.awkVars.RS = rs
return lr
}
// SetFS - set field separator for AWK mode
func (lr *Reader) SetFS(fs *regexp.Regexp) *Reader {
if lr == nil {
return nil
}
lr.awkVars.FS = fs
return lr
}
// AWKMode - process lines with AWK like mode
func (lr *Reader) AWKMode(filterFn func(line string, fields []string, vars AWKVars) (string, error)) *Reader {
if lr == nil {
return nil
}
return lr.MapErr(func(line []byte) ([]byte, error) {
addRS := false
RS := []byte{lr.awkVars.RS}
if bytes.HasSuffix(line, RS) {
addRS = true
line = bytes.TrimSuffix(line, RS)
}
lineStr := string(line)
fields := lr.awkVars.FS.Split(lineStr, -1)
lr.awkVars.NF = len(fields)
result, err := filterFn(lineStr, fields, lr.awkVars)
if err != nil {
return nullBytes, err
}
resultBytes := []byte(result)
if !bytes.HasSuffix(resultBytes, RS) && addRS {
resultBytes = append(resultBytes, lr.awkVars.RS)
}
return resultBytes, nil
})
}
// Discard - read all content from Reader for side effect from filter functions
func (lr *Reader) Discard() error {
if lr == nil {
return ErrNilReader
}
_, err := io.Copy(ioutil.Discard, lr)
return err
}
// ReadAllSlice - read all content from Reader by lines to slice of []byte
func (lr *Reader) ReadAllSlice() ([][]byte, error) {
result := [][]byte{}
err := lr.Map(func(line []byte) []byte {
result = append(result, line)
return nullBytes
}).Discard()
return result, err
}
// ReadAll - read all content from Reader to slice of bytes
func (lr *Reader) ReadAll() ([]byte, error) {
if lr == nil {
return []byte{}, ErrNilReader
}
return ioutil.ReadAll(lr)
}
// ReadAllSliceString - read all content from Reader to string slice by lines
func (lr *Reader) ReadAllSliceString() ([]string, error) {
if lr == nil {
return []string{}, ErrNilReader
}
result := []string{}
err := lr.MapString(func(line string) string {
result = append(result, line)
return ""
}).Discard()
return result, err
}
// ReadAllString - read all content from Reader to one string
func (lr *Reader) ReadAllString() (string, error) {
if lr == nil {
return "", ErrNilReader
}
result, err := ioutil.ReadAll(lr)
return string(result), err
}