-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolumn.go
87 lines (77 loc) · 1.43 KB
/
column.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
package csvutil
import (
"strconv"
"github.com/pkg/errors"
)
type column struct {
symbol string
index int
err error
}
type columns []*column
func (cs columns) err() error {
for _, c := range cs {
if c.err != nil {
return c.err
}
}
return nil
}
func (c *column) findIndex(hdr []string) error {
if c.symbol == "" {
return nil
}
if isDigit(c.symbol) {
i, _ := strconv.Atoi(c.symbol)
c.index = i
return nil
}
if hdr == nil {
return errors.New("not number column symbol")
}
for i, h := range hdr {
if h == c.symbol {
c.index = i
return nil
}
}
return errors.Errorf("column %s not found", c.symbol)
}
func newColumnWithIndex(sym string, hdr []string) *column {
col := &column{
symbol: sym,
index: -1,
}
err := col.findIndex(hdr)
if err != nil {
col.err = err
}
return col
}
func newColumnsWithIndexes(syms []string, hdr []string) columns {
cols := make([]*column, len(syms))
for i, sym := range syms {
cols[i] = newColumnWithIndex(sym, hdr)
}
return cols
}
func newUniqueColumns(syms []string, hdr []string) columns {
cols := newColumnsWithIndexes(syms, hdr)
return uniqColumns(cols)
}
func uniqColumns(cols columns) columns {
var newCols []*column
for _, col := range cols {
exists := false
for _, newCol := range newCols {
if col.index != -1 && newCol.index == col.index {
exists = true
break
}
}
if !exists {
newCols = append(newCols, col)
}
}
return newCols
}