-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombine.go
96 lines (87 loc) · 2.04 KB
/
combine.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
package csvutil
import (
"io"
"strings"
"github.com/pkg/errors"
)
// CombineOption is option holder for Combine.
type CombineOption 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
// SourceSyms header or column index list
SourceSyms []string
// Destination column symbol
Destination string
// Delimiter
Delimiter string
}
func (o CombineOption) validate() error {
if len(o.SourceSyms) == 0 {
return errors.New("no column")
}
if o.NoHeader {
for _, c := range o.SourceSyms {
if !isDigit(c) {
return errors.Errorf("not number column symbol: %s", c)
}
}
}
return nil
}
func (o CombineOption) outputEncoding() string {
if o.OutputEncoding != "" {
return o.OutputEncoding
}
return o.Encoding
}
// Combine column(s) from CSV.
func Combine(r io.Reader, w io.Writer, o CombineOption) 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 srcs columns
var dst *column
csvp := NewCSVProcessor(cr, cw)
if o.NoHeader {
csvp.SetPreBodyRead(func() error {
srcs = newUniqueColumns(o.SourceSyms, nil)
dst = newColumnWithIndex(o.Destination, nil)
if err := srcs.err(); err != nil {
return err
}
return dst.err
})
} else {
csvp.SetHeaderHanlder(func(hdr []string) ([]string, error) {
srcs = newUniqueColumns(o.SourceSyms, hdr)
dst = newColumnWithIndex(o.Destination, hdr)
if err := srcs.err(); err != nil {
return hdr, err
}
return hdr, dst.err
})
}
csvp.SetRecordHandler(func(rec []string) ([]string, error) {
newRec := make([]string, len(rec))
vals := make([]string, len(srcs))
for i, src := range srcs {
vals[i] = rec[src.index]
}
for i, s := range rec {
if i == dst.index {
newRec[i] = strings.Join(vals, o.Delimiter)
} else {
newRec[i] = s
}
}
return newRec, nil
})
return csvp.Process()
}