-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv.go
329 lines (266 loc) · 6.62 KB
/
csv.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package csv
// TODO(tbshill): Encoding multiline elements
import (
"bufio"
"fmt"
"io"
"reflect"
"strings"
"errors"
)
const CSVTagName = "csv"
var (
ErrInvalidSchema = errors.New("row did not have the same number of fields as the struct")
)
// Decoder is a structure that will read a deliniated string into a structure
type Decoder struct {
del string
nl string
s *bufio.Scanner
row string
}
func ColsToRow(cols []string, del string) string {
return colsToRow(cols, del)
}
func RowToCols(row, del string) []string {
return rowToCols(row, del)
}
func wrapInQuotes(s string) string {
return "\"" + s + "\""
}
func containsDelimeter(text, del string) bool {
return strings.Contains(text, del)
}
func wrapInQuotesIfTextContainsDelimeter(text, del string) string {
if containsDelimeter(text, del) {
return wrapInQuotes(text)
}
return text
}
func colsToRow(cols []string, del string) string {
if len(cols) == 0 {
return ""
}
if len(cols) == 1 {
return wrapInQuotesIfTextContainsDelimeter(cols[0], del)
}
var sb strings.Builder
sb.WriteString(wrapInQuotesIfTextContainsDelimeter(cols[0], del))
for _, col := range cols[1:] {
sb.WriteString(del)
sb.WriteString(wrapInQuotesIfTextContainsDelimeter(col, del))
}
return sb.String()
}
type rowToColState int
const (
columnStartState rowToColState = iota
quotedInnerState
secondQuoteState
innerColState
)
func rowToCols(row, del string) []string {
var sb strings.Builder
var cols []string
doColumnStartState := func(next string) rowToColState {
switch next {
case del:
cols = append(cols, sb.String())
sb.Reset()
return columnStartState
case "\"":
return quotedInnerState
default:
sb.WriteString(next)
return innerColState
}
}
doQuotedInnerState := func(next string) rowToColState {
switch next {
case "\"":
return secondQuoteState
default:
sb.WriteString(next)
return quotedInnerState
}
}
doSecondQuoteState := func(next string) rowToColState {
switch next {
case del:
cols = append(cols, sb.String())
sb.Reset()
return columnStartState
default:
panic("comma was expected at the end of a column, or support escaped quotes")
}
}
doInnerColState := func(next string) rowToColState {
switch next {
case del:
cols = append(cols, sb.String())
sb.Reset()
return columnStartState
default:
sb.WriteString(next)
return innerColState
}
}
state := columnStartState
for _, v := range row {
switch state {
case columnStartState:
state = doColumnStartState(string(v))
case quotedInnerState:
state = doQuotedInnerState(string(v))
case secondQuoteState:
state = doSecondQuoteState(string(v))
case innerColState:
state = doInnerColState(string(v))
}
}
cols = append(cols, sb.String())
sb.Reset()
return cols
}
func ScanQuotedLine(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}
if i := indexOfQuotedEndOfLine(data); i >= 0 {
return i + 1, dropCR(data[0:i]), nil
}
if atEOF {
return len(data), dropCR(data), nil
}
return 0, nil, nil
}
func indexOfQuotedEndOfLine(data []byte) int {
char := 0
quoted := 1
state := char
for i, r := range data {
switch state {
case char:
switch r {
case '\n':
return i
case '"':
state = quoted
}
case quoted:
switch r {
case '"':
state = char
}
}
}
return -1
}
func dropCR(data []byte) []byte {
if len(data) > 0 && data[len(data)-1] == '\r' {
return data[0: len(data)-1]
}
return data
}
// NewDecoder returns a new csv Decoder. 'del' is the delimeter, 'nl' is the
// newline character, and r is the source to read from
func NewDecoder(del, nl string, r io.Reader) *Decoder {
decoder := &Decoder{
del: del,
nl: nl,
s: bufio.NewScanner(r),
}
decoder.s.Split(ScanQuotedLine)
return decoder
}
// Decode takes the string record that was loaded by Scan() and marshals it
// into a struct. The struct must be a reference, otherwise this function will
// panic
func (d *Decoder) Decode(obj interface{}) error {
columns := rowToCols(d.row, d.del)
v := reflect.ValueOf(obj).Elem()
if v.NumField() != len(columns) {
return fmt.Errorf("%w: Struct: %d, Fields: %d\n", ErrInvalidSchema, v.NumField(), len(columns))
}
for i, column := range columns {
v.Field(i).SetString(column)
}
return nil
}
// Scan reads a string record into a buffer to be decoded. It returns true if
// it found a record or false if it reached the end of the input stream
func (d *Decoder) Scan() bool {
ok := d.s.Scan()
if ok {
d.row = d.s.Text()
}
return ok
}
func (d *Decoder) Text() string {
return d.row
}
// Encoder is a utility that will encode a structure into a deliniated string.
// Like a csv
type Encoder struct {
// del is the delimeter used to separate fields
del string
// nl is the newline character used to separate records.
nl string
// w is the destination to write the record
w io.Writer
}
// NewEncoder creates a new csv encoder. del is the delimeter used, nl is the
// newline characters used, and w is the destination to write to
func NewEncoder(del, nl string, w io.Writer) Encoder {
return Encoder{
del: del,
nl: nl,
w: w,
}
}
// WriteHeadersFor writes a record for the column headers. This should be the
// first record written
func (e Encoder) WriteHeadersFor(obj interface{}) error {
columns := reflectColumnHeaders(obj)
row := colsToRow(columns, e.del)
_, err := fmt.Fprintf(e.w, "%s%s", row, e.nl)
return err
}
// Encode writes a single record to the encoder. obj is reflected to obtain all
// of the values from the struct. obj may be either passed by value or
// reference. obj may not be a chan, slice, or pimitive like int, string or
// float.
func (e Encoder) Encode(obj interface{}) error {
v := reflect.ValueOf(obj)
// If the obj is a reference
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
columns := make([]string, v.NumField())
for i, _ := range columns {
columns[i] = fmt.Sprintf("%v", v.Field(i).Interface())
}
row := colsToRow(columns, e.del)
_, err := fmt.Fprintf(e.w, "%s%s", row, e.nl)
return err
}
// reflectColumnHeaders returns the column headers for a specified struct. A
// column header is the name of the field or if the field has the "csv" tag,
// then it will use it's value
func reflectColumnHeaders(obj interface{}) []string {
t := reflect.TypeOf(obj)
// If the obj is a reference
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
headers := make([]string, t.NumField())
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
if headerVal, ok := field.Tag.Lookup(CSVTagName); ok {
headers[i] = headerVal
} else {
headers[i] = field.Name
}
}
return headers
}