-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv_processor.go
91 lines (80 loc) · 1.96 KB
/
csv_processor.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
package csvutil
import (
"encoding/csv"
"io"
)
// CSVProcessor process CSV read from csv.Reader, and write to csv.Writer.
type CSVProcessor struct {
reader *csv.Reader
writer *csv.Writer
headerHandler func([]string) ([]string, error)
preBodyRead func() error
recordHandler func([]string) ([]string, error)
}
// NewCSVProcessor returns new processor from reader and writer.
func NewCSVProcessor(r *csv.Reader, w *csv.Writer) *CSVProcessor {
return &CSVProcessor{
reader: r,
writer: w,
}
}
// NewReadOnlyCSVProcessor returns new processor from reader.
// Processor does not write to CSV.
func NewReadOnlyCSVProcessor(r *csv.Reader) *CSVProcessor {
return &CSVProcessor{reader: r}
}
// SetHeaderHanlder set function for calling on header line read.
func (csvp *CSVProcessor) SetHeaderHanlder(f func([]string) ([]string, error)) {
csvp.headerHandler = f
}
// SetPreBodyRead set function for calling before first body line read.
func (csvp *CSVProcessor) SetPreBodyRead(f func() error) {
csvp.preBodyRead = f
}
// SetRecordHandler set function for calling on each body line read.
func (csvp *CSVProcessor) SetRecordHandler(f func([]string) ([]string, error)) {
csvp.recordHandler = f
}
// Process CSV.
// Read each line and apply each functions and write line.
func (csvp *CSVProcessor) Process() error {
if csvp.headerHandler != nil {
hdr, err := csvp.reader.Read()
if err != nil {
if err == io.EOF {
return nil
}
return err
}
pHdr, err := csvp.headerHandler(hdr)
if err != nil {
return err
}
if csvp.writer != nil && pHdr != nil {
csvp.writer.Write(pHdr)
}
}
if csvp.preBodyRead != nil {
err := csvp.preBodyRead()
if err != nil {
return err
}
}
for {
rec, err := csvp.reader.Read()
if err != nil {
if err == io.EOF {
break
}
return err
}
pRec, err := csvp.recordHandler(rec)
if err != nil {
return err
}
if csvp.writer != nil && pRec != nil {
csvp.writer.Write(pRec)
}
}
return nil
}