/
readerinterface.go
434 lines (362 loc) · 7.98 KB
/
readerinterface.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
// Copyright 2015-2018, Shulhan <ms@kilabit.info>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package dsv
import (
"bytes"
"fmt"
"github.com/shuLhan/tabula"
"github.com/shuLhan/tekstus"
"io"
"os"
)
//
// ReaderInterface is the interface for reading DSV file.
//
type ReaderInterface interface {
ConfigInterface
AddInputMetadata(*Metadata)
AppendMetadata(MetadataInterface)
GetInputMetadata() []MetadataInterface
GetInputMetadataAt(idx int) MetadataInterface
GetMaxRows() int
SetMaxRows(max int)
GetDatasetMode() string
SetDatasetMode(mode string)
GetNColumnIn() int
GetInput() string
SetInput(path string)
GetRejected() string
SetRejected(path string)
GetSkip() int
SetSkip(n int)
IsTrimSpace() bool
SetDefault()
OpenInput() error
OpenRejected() error
SkipLines() error
Reset() error
Flush() error
ReadLine() ([]byte, error)
FetchNextLine([]byte) ([]byte, error)
Reject(line []byte) (int, error)
Close() error
GetDataset() interface{}
MergeColumns(ReaderInterface)
}
//
// Read row from input file.
//
func Read(reader ReaderInterface) (n int, e error) {
var (
row *tabula.Row
line []byte
linenum int
eRead *ReaderError
)
maxrows := reader.GetMaxRows()
e = reader.Reset()
if e != nil {
return
}
dataset := reader.GetDataset().(tabula.DatasetInterface)
// Loop until we reached MaxRows (> 0) or when all rows has been
// read (= -1)
for {
row, line, linenum, eRead = ReadRow(reader, linenum)
if nil == eRead {
dataset.PushRow(row)
n++
if maxrows > 0 && n >= maxrows {
break
}
continue
}
if eRead.T&EReadEOF == EReadEOF {
_ = reader.Flush()
e = io.EOF
return
}
eRead.N = linenum
fmt.Fprintf(os.Stderr, "%s\n", eRead)
// If error, save the rejected line.
line = append(line, DefEOL)
_, e = reader.Reject(line)
if e != nil {
break
}
}
// remember to flush if we have rejected rows.
e = reader.Flush()
return n, e
}
//
// parsingLeftQuote parse the left-quote string from line.
//
func parsingLeftQuote(lq, line []byte, startAt int) (
p int, eRead *ReaderError,
) {
p = startAt
// parsing until we found left quote token
p, found := tekstus.BytesSkipUntil(line, lq, p, false)
if found {
return p, nil
}
eRead = &ReaderError{
T: EReadMissLeftQuote,
Func: "parsingLeftQuote",
What: "Missing left-quote '" + string(lq) + "'",
Line: string(line),
Pos: p,
N: 0,
}
return p, eRead
}
//
// parsingSeparator parsing the line until we found the separator.
//
// Return the data and index of last parsed line, or error if separator is not
// found or not match with specification.
//
func parsingSeparator(sep, line []byte, startAt int) (
v []byte, p int, eRead *ReaderError,
) {
p = startAt
v, p, found := tekstus.BytesCutUntil(line, sep, p, false)
if found {
return v, p, nil
}
eRead = &ReaderError{
Func: "parsingSeparator",
What: "Missing separator '" + string(sep) + "'",
Line: string(line),
Pos: p,
N: 0,
}
return v, p, eRead
}
//
// parsingRightQuote parsing the line until we found the right quote or separator.
//
// Return the data and index of last parsed line, or error if right-quote is not
// found or not match with specification.
//
func parsingRightQuote(reader ReaderInterface, rq, line []byte, startAt int) (
v, lines []byte, p int, eRead *ReaderError,
) {
var e error
var content []byte
p = startAt
var found bool
// (2.2.1)
for {
content, p, found = tekstus.BytesCutUntil(line, rq, p, true)
v = append(v, content...)
if found {
return v, line, p, nil
}
// EOL before finding right-quote.
// Read and join with the next line.
line, e = reader.FetchNextLine(line)
if e != nil {
break
}
}
eRead = &ReaderError{
T: EReadMissRightQuote,
Func: "parsingRightQuote",
What: "Missing right-quote '" + string(rq) + "'",
Line: string(line),
Pos: p,
N: 0,
}
if e == io.EOF {
eRead.T &= EReadEOF
}
return v, line, p, eRead
}
//
// parsingSkipSeparator parse until we found separator or EOF
//
func parsingSkipSeparator(sep, line []byte, startAt int) (
p int, eRead *ReaderError,
) {
p = startAt
p, found := tekstus.BytesSkipUntil(line, sep, p, false)
if found {
return p, nil
}
eRead = &ReaderError{
T: EReadMissSeparator,
Func: "parsingSkipSeparator",
What: "Missing separator '" + string(sep) + "'",
Line: string(line),
Pos: p,
N: 0,
}
return p, eRead
}
//
// parsingSkipSpace skip all space starting from `startAt`.
//
func parsingSkipSpace(line []byte, startAt int) (p int) {
linelen := len(line)
for p = startAt; p < linelen; p++ {
if line[p] == ' ' || line[p] == '\t' || line[p] == '\n' ||
line[p] == '\r' {
continue
}
break
}
return
}
//
// ParseLine parse a line containing records. The output is array of record
// (or single row).
//
// This is how the algorithm works
// (1) create n slice of record, where n is number of column metadata
// (2) for each metadata
// (2.0) Check if the next sequence matched with separator.
// (2.0.1) If its match, create empty record
// (2.1) If using left quote, skip until we found left-quote
// (2.2) If using right quote, append byte to buffer until right-quote
// (2.2.1) If using separator, skip until separator
// (2.3) If using separator, append byte to buffer until separator
// (2.4) else append all byte to buffer.
// (3) save buffer to record
//
func ParseLine(reader ReaderInterface, line []byte) (
prow *tabula.Row, eRead *ReaderError,
) {
p := 0
rIdx := 0
inputMd := reader.GetInputMetadata()
row := make(tabula.Row, 0)
for _, md := range inputMd {
lq := md.GetLeftQuote()
rq := md.GetRightQuote()
sep := md.GetSeparator()
v := []byte{}
// (2.0)
if sep != "" && sep != lq {
match := tekstus.BytesMatchForward(line, []byte(sep),
p)
// (2.0.1)
if match {
p += len(sep)
goto empty
}
}
// (2.1)
if lq != "" {
p, eRead = parsingLeftQuote([]byte(lq), line, p)
if eRead != nil {
return
}
}
// (2.2)
if rq != "" {
v, line, p, eRead = parsingRightQuote(reader, []byte(rq),
line, p)
if eRead != nil {
return
}
if sep != "" {
p, eRead = parsingSkipSeparator([]byte(sep),
line, p)
if eRead != nil {
return
}
// Handle multi space if separator is a single
// space.
if sep == " " {
p = parsingSkipSpace(line, p)
}
}
} else {
if sep != "" {
// Skip space at beginning if separator is a
// single space.
if sep == " " {
p = parsingSkipSpace(line, p)
}
v, p, eRead = parsingSeparator([]byte(sep),
line, p)
if eRead != nil {
return
}
// Handle multi space if separator is a single
// space.
if sep == " " {
p = parsingSkipSpace(line, p)
}
} else {
v = line[p:]
p = p + len(line)
}
}
if md.GetSkip() {
continue
}
empty:
r, e := tabula.NewRecordBy(string(v), md.GetType())
if nil != e {
msg := fmt.Sprintf("md %s: Type convertion error from %q to %s",
md.GetName(), string(v), md.GetTypeName())
return nil, &ReaderError{
T: ETypeConversion,
Func: "ParseLine",
What: msg,
Line: string(line),
Pos: p,
N: 0,
}
}
row = append(row, r)
rIdx++
}
return &row, nil
}
//
// ReadRow read one line at a time until we get one row or error when parsing the
// data.
//
func ReadRow(reader ReaderInterface, linenum int) (
row *tabula.Row,
line []byte,
n int,
eRead *ReaderError,
) {
var e error
n = linenum
// Read one line, skip empty line.
for {
line, e = reader.ReadLine()
n++
if e != nil {
goto err
}
// check for empty line
linetrimed := bytes.TrimSpace(line)
if len(linetrimed) > 0 {
break
}
}
if reader.IsTrimSpace() {
line = bytes.TrimSpace(line)
}
row, eRead = ParseLine(reader, line)
return row, line, n, eRead
err:
eRead = &ReaderError{
Func: "ReadRow",
What: fmt.Sprint(e),
}
if e == io.EOF {
eRead.T = EReadEOF
} else {
eRead.T = EReadLine
}
return nil, line, n, eRead
}