-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv.go
205 lines (169 loc) · 4.21 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
package csv
import (
"bufio"
"fmt"
"io"
"reflect"
"strconv"
"strings"
"time"
)
type mapping struct {
field field
columnIndex int
}
type field struct {
columnName string
index int
kind reflect.Kind
}
type Reader struct {
reader *bufio.Reader
mapping []mapping
// Parsing options
Delimiter byte
TimeLayout string
}
const (
defaultDelimiter = ','
defaultTimeLayout = time.RFC3339
csvTag = "csv"
)
func NewReader(r io.Reader) *Reader {
br := bufio.NewReader(r)
return &Reader{
reader: br,
Delimiter: defaultDelimiter,
TimeLayout: defaultTimeLayout}
}
func (r Reader) trim(str string) string {
str = strings.Trim(str, string([]byte{'\t', ' '}))
if str[0] == '"' && str[len(str)-1] == '"' {
str = str[1 : len(str)-1]
}
return string(str)
}
func (r Reader) parseLine() ([]string, error) {
lineBytes, _, err := r.reader.ReadLine()
if err != nil {
return nil, err
}
var result []string
var i, pos int
var inQuatations bool
for ; i < len(lineBytes); i++ {
if lineBytes[i] == '"' {
if !inQuatations {
inQuatations = true
} else {
// escaping - if the next rune is also quotation remove current character
if i+1 != len(lineBytes) && lineBytes[i+1] == '"' {
lineBytes = append(lineBytes[:i], lineBytes[i+1:]...)
continue
}
inQuatations = false
}
}
if lineBytes[i] == r.Delimiter && !inQuatations {
v := r.trim(string(lineBytes[pos:i]))
result = append(result, v)
pos = i + 1
}
}
// handle the last string at the end
if i > pos {
v := r.trim(string(lineBytes[pos:i]))
result = append(result, v)
}
return result, nil
}
func (r *Reader) mapFields(t reflect.Type) error {
columns, err := r.parseLine()
if err != nil {
return err
}
fields := r.getFields(t)
FieldsLoop:
for _, field := range fields {
for colIndex, colName := range columns {
if field.columnName == colName {
r.mapping = append(r.mapping, mapping{field, colIndex})
continue FieldsLoop
}
}
return fmt.Errorf("column %s was not found", field.columnName)
}
return nil
}
func (r Reader) getFields(t reflect.Type) (fields []field) {
for fieldIndex := 0; fieldIndex < t.NumField(); fieldIndex++ {
fieldInfo := t.Field(fieldIndex)
if fieldInfo.IsExported() {
colName := fieldInfo.Tag.Get(csvTag)
if colName == "" {
colName = fieldInfo.Name
}
fields = append(fields, field{colName, fieldIndex, fieldInfo.Type.Kind()})
}
}
return fields
}
func (r Reader) setFieldValue(value string, elemField reflect.Value, kind reflect.Kind) error {
switch kind {
case reflect.String:
elemField.SetString(value)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
valueInt, err := strconv.ParseInt(value, 0, 64)
if err != nil {
return fmt.Errorf("can not parse '%s' as int", value)
}
elemField.SetInt(valueInt)
case reflect.Float32, reflect.Float64:
float, err := strconv.ParseFloat(value, 64)
if err != nil {
return fmt.Errorf("can not parse '%s' as float", value)
}
elemField.SetFloat(float)
case reflect.ValueOf(time.Time{}).Kind():
t, err := time.Parse(r.TimeLayout, value)
if err != nil {
return fmt.Errorf("can not parse '%s' as time.Time", value)
}
elemField.Set(reflect.ValueOf(t))
default:
return fmt.Errorf("unsupported type %s", kind)
}
return nil
}
func (r *Reader) ReadAll(i interface{}) error {
// type extract
t := reflect.TypeOf(i)
if t.Kind() != reflect.Ptr || t.Elem().Kind() != reflect.Slice || t.Elem().Elem().Kind() != reflect.Struct {
return fmt.Errorf("input type must be a reference to a slice of structs")
}
itemType := t.Elem().Elem()
err := r.mapFields(itemType)
if err != nil {
return err
}
slice := reflect.ValueOf(i).Elem()
for lineNumber := 1; ; lineNumber++ {
cols, err := r.parseLine()
if err == io.EOF || len(cols) == 0 {
return nil
}
if err != nil {
return err
}
item := reflect.New(itemType)
for i, m := range r.mapping {
elemField := item.Elem().Field(m.field.index)
value := cols[m.columnIndex]
err := r.setFieldValue(value, elemField, elemField.Kind())
if err != nil {
return fmt.Errorf("line %d column %d: %s", lineNumber, i+1, err)
}
}
slice.Set(reflect.Append(slice, item.Elem()))
}
}