-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract.go
82 lines (72 loc) · 1.75 KB
/
extract.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
package csvutil
import (
"io"
"github.com/pkg/errors"
)
// ExtractOption is option holder for Extract.
type ExtractOption struct {
// Source file does not have header line. (default false)
NoHeader bool
// Encoding of source file. (default utf8)
Encoding string
// Encoding for output.
OutputEncoding string
// ColumnSyms header or column index list.
ColumnSyms []string
}
func (o ExtractOption) validate() error {
if len(o.ColumnSyms) == 0 {
return errors.New("no column")
}
if o.NoHeader {
for _, c := range o.ColumnSyms {
if !isDigit(c) {
return errors.New("not number column symbol")
}
}
}
return nil
}
func (o ExtractOption) outputEncoding() string {
if o.OutputEncoding != "" {
return o.OutputEncoding
}
return o.Encoding
}
// Extract column(s) from CSV.
func Extract(r io.Reader, w io.Writer, o ExtractOption) error {
if err := o.validate(); err != nil {
return errors.Wrap(err, "invalid option")
}
cr, bom := reader(r, o.Encoding)
cw := writer(w, bom, o.outputEncoding())
defer cw.Flush()
var cols columns
csvp := NewCSVProcessor(cr, cw)
if o.NoHeader {
csvp.SetPreBodyRead(func() error {
cols = newUniqueColumns(o.ColumnSyms, nil)
return cols.err()
})
} else {
csvp.SetHeaderHanlder(func(hdr []string) ([]string, error) {
cols = newUniqueColumns(o.ColumnSyms, hdr)
if err := cols.err(); err != nil {
return nil, err
}
return extractFromRecord(hdr, cols), nil
})
}
csvp.SetRecordHandler(func(rec []string) ([]string, error) {
return extractFromRecord(rec, cols), nil
})
return csvp.Process()
}
func extractFromRecord(rec []string, cols columns) []string {
newRec := make([]string, len(cols))
copy(newRec, rec)
for n, col := range cols {
newRec[n] = rec[col.index]
}
return newRec
}