-
Notifications
You must be signed in to change notification settings - Fork 4
/
read.go
272 lines (252 loc) · 6.46 KB
/
read.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
package table
import (
"errors"
"fmt"
"io"
"os"
"strings"
"github.com/grokify/mogo/encoding/csvutil"
"github.com/grokify/mogo/errors/errorsutil"
)
var debugReadCSV = false // should not need to use this.
// ReadFile reads one or more delimited files and returns a merged `Table` struct.
// An error is returned if the columns count differs between files.
func ReadFile(opts *ParseOptions, filenames ...string) (Table, error) {
tbl := NewTable("")
if len(filenames) == 0 {
return tbl, errors.New("no filenames provided")
}
for i, filename := range filenames {
tblx, err := readSingleFile(opts, filename)
if err != nil || len(filenames) == 1 {
return tblx, err
}
if i == 0 {
tbl = tblx
continue
} else if !tbl.Columns.Equal(tblx.Columns) {
return tbl, fmt.Errorf("csv column mismatch earlier files cols1 [%s] cols2 [%s]",
strings.Join(tbl.Columns, ","),
strings.Join(tblx.Columns, ","))
}
tbl.Rows = append(tbl.Rows, tblx.Rows...)
}
return tbl, nil
}
func trimSpaceSliceSliceString(s [][]string) [][]string {
for i, row := range s {
for j, cell := range row {
s[i][j] = strings.TrimSpace(cell)
}
}
return s
}
func readSingleFile(opts *ParseOptions, filename string) (Table, error) {
f, err := os.Open(filename)
if err != nil {
return NewTable(""), err
}
defer f.Close()
tbl, err := ParseReadSeeker(opts, f)
if err != nil {
return tbl, err
}
return tbl, f.Close()
}
// ParseReadSeeker parses an `io.ReadSeeker` and returns a `Table` struct.
func ParseReadSeeker(opts *ParseOptions, rs io.ReadSeeker) (Table, error) {
tbl := NewTable("")
if opts == nil {
opts = &ParseOptions{}
}
csvReader, err := csvutil.NewReader(rs, opts.CommaValue())
if err != nil {
return tbl, err
}
if debugReadCSV {
i := -1
for {
line, err := csvReader.Read()
if err == io.EOF {
break
} else if err != nil {
return tbl, err
}
i++
if i == 0 && (opts == nil || !opts.NoHeader) {
tbl.Columns = line
continue
}
tbl.Rows = append(tbl.Rows, line)
if i > 2500 {
fmt.Printf("[%v] %v\n", i, strings.Join(line, ","))
}
}
} else {
csvReader.FieldsPerRecord = opts.FieldsPerRecord
if opts.NoHeader {
csvReader.FieldsPerRecord = -1
}
errorOutofBounds := true
if csvReader.FieldsPerRecord < 0 {
errorOutofBounds = false
}
lines, err := csvReader.ReadAll()
if opts.TrimSpace {
lines = trimSpaceSliceSliceString(lines)
}
if err != nil {
return tbl, errorsutil.Wrap(err, "csv.Reader.ReadAll()")
}
if len(lines) == 0 {
return tbl, errors.New("no content")
}
if !opts.NoHeader { // hasHeader
if !opts.HasFilter() {
tbl.LoadMergedRows(lines)
} else {
if len(opts.FilterColNames) > 0 {
tbl.Columns = Columns(opts.FilterColNames)
} else {
cols := lines[0]
wantColNames := []string{}
for _, idx := range opts.FilterColIndices {
if int(idx) < len(cols) {
wantColNames = append(wantColNames, cols[int(idx)])
} else {
return tbl, fmt.Errorf("want column index not found [%d]", idx)
}
}
tbl.Columns = wantColNames
}
rows, err := opts.Filter(lines[0], lines[1:], errorOutofBounds)
if err != nil {
return tbl, err
}
tbl.Rows = rows
}
} else {
if len(opts.FilterColIndices) == 0 {
tbl.Rows = lines
} else {
rows, err := opts.Filter([]string{}, lines, errorOutofBounds)
if err != nil {
return tbl, err
}
tbl.Rows = rows
}
}
}
return tbl, nil
}
// ParseOptions provides a set of configuraation parameters
// for parsing a CSV file. If an empty or nil `ParseOptions`
// is provided to `ReadFile`, default options for reading
// files will be used.
type ParseOptions struct {
UseComma bool
Comma rune
NoHeader bool // HasHeader is default (false)
FieldsPerRecord int
FilterColNames []string
FilterColIndices []uint
TrimSpace bool
}
func (opts *ParseOptions) CommaValue() rune {
if opts.UseComma {
return opts.Comma
}
return ','
}
func (opts *ParseOptions) HasFilter() bool {
if len(opts.FilterColNames) > 0 || len(opts.FilterColIndices) > 0 {
return true
}
return false
}
func (opts *ParseOptions) Filter(cols []string, rows [][]string, errorOutOfBounds bool) ([][]string, error) {
newRows := [][]string{}
indices := opts.FilterColIndices
if !opts.HasFilter() {
return rows, nil
}
if len(opts.FilterColIndices) == 0 &&
len(opts.FilterColNames) > 0 && len(cols) > 0 {
colsPlus := Columns(cols)
indicesTry := []uint{}
wantColNamesNotFound := []string{}
for _, wantColName := range opts.FilterColNames {
idx := colsPlus.Index(wantColName)
if idx < 0 {
wantColNamesNotFound = append(wantColNamesNotFound, wantColName)
} else {
indicesTry = append(indicesTry, uint(idx))
}
}
if len(wantColNamesNotFound) > 0 {
return newRows, fmt.Errorf(
"filter columns not found [%s]",
strings.Join(wantColNamesNotFound, ","))
}
indices = indicesTry
}
if len(indices) == 0 {
return newRows, fmt.Errorf(
"no colIndices or row match: colNameFilter [%s] colNames [%s]",
strings.Join(opts.FilterColNames, ","),
strings.Join(cols, ","))
}
for _, row := range rows {
newRow := []string{}
for _, idx := range indices {
if int(idx) <= len(row) {
newRow = append(newRow, row[int(idx)])
} else if errorOutOfBounds {
return newRows, fmt.Errorf("desired index out of bounds: index[%d] row len [%d]", idx, len(row))
} else {
newRow = append(newRow, "")
}
}
newRows = append(newRows, newRow)
}
return newRows, nil
}
/*
func ParseBytes(data []byte, delimiter rune, hasHeaderRow bool) (Table, error) {
return ParseReader(bytes.NewReader(data), delimiter, hasHeaderRow)
}
func ParseReader(reader io.Reader, delimiter rune, hasHeaderRow bool) (Table, error) {
tbl := NewTable("")
csvReader := csv.NewReader(reader)
csvReader.Comma = delimiter
csvReader.TrimLeadingSpace = true
idx := -1
for {
row, err := csvReader.Read()
if err == io.EOF {
break
}
if err != nil {
return tbl, err
}
idx++
if idx == 0 && hasHeaderRow {
tbl.Columns = row
continue
}
tbl.Rows = append(tbl.Rows, row)
}
return tbl, nil
}
*/
// Unmarshal is a convenience function to provide a simple interface to
// unmarshal table contents into any desired output.
func (tbl *Table) Unmarshal(funcRow func(row []string) error) error {
for i, row := range tbl.Rows {
err := funcRow(row)
if err != nil {
return errorsutil.Wrap(err, fmt.Sprintf("Error on Record Index [%d]", i))
}
}
return nil
}