-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsvlib.go
More file actions
212 lines (175 loc) · 4.14 KB
/
csvlib.go
File metadata and controls
212 lines (175 loc) · 4.14 KB
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
package csvlib
import (
"fmt"
"strconv"
"time"
"github.com/pkg/errors"
"github.com/ericlagergren/decimal"
)
type Parser interface {
Parse(val string) ([]ParsedValue, error)
Defaults() []ParsedValue
}
type ParsedValue interface {
I32() int32
I64() int64
F32() float32
F64() float64
String() string
Bool() bool
Time() time.Time
Ifc() interface{}
IsValid() bool
}
type RowParser struct {
P []Parser
}
func (r *RowParser) AppendColumn(p Parser) {
r.P = append(r.P, p)
}
type ElemNotMatch struct {
Want, Got int
}
func (e ElemNotMatch) Error() string {
return fmt.Sprintf("number of elements(%d) does NOT match number of parser (%d)", e.Got, e.Want)
}
var ErrNumberOfElemNotMatch = fmt.Errorf("number of elements does NOT match number of parser")
func (r *RowParser) Parse(row []string) ([]ParsedValue, error) {
if len(row) != r.Len() {
return nil, ElemNotMatch{r.Len(), len(row)}
}
var values []ParsedValue
for i, p := range r.P {
if row[i] == "\\N" {
values = append(values, p.Defaults()...)
continue
}
vals, err := p.Parse(row[i])
if err != nil {
return vals, err
}
values = append(values, vals...)
}
return values, nil
}
func (r *RowParser) Len() int {
return len(r.P)
}
type Int32Parser struct {
Name string
Optional bool // if "" then nil
Default int32 // as defautl value
}
func (p Int32Parser) Parse(val string) ([]ParsedValue, error) {
if p.Optional && val == "" {
return p.Defaults(), nil
}
ret, err := strconv.ParseInt(val, 10, 32)
if err != nil {
return nil, errors.Wrapf(err, " %s ", p.Name)
}
return []ParsedValue{ParsedInt32(ret)}, err
}
func (p Int32Parser) Defaults() []ParsedValue {
return []ParsedValue{ParsedInt32(p.Default)}
}
type Int64Parser struct {
Name string
}
func (p Int64Parser) Parse(val string) ([]ParsedValue, error) {
ret, err := strconv.ParseInt(val, 10, 64)
if err != nil {
return nil, errors.Wrapf(err, " %s ", p.Name)
}
return []ParsedValue{ParsedInt64(ret)}, err
}
func (Int64Parser) Defaults() []ParsedValue {
return []ParsedValue{ParsedInt64(0)}
}
type Float32Parser struct {
Name string
Optional bool
}
func (p Float32Parser) Parse(val string) ([]ParsedValue, error) {
if val == "" && p.Optional {
return p.Defaults(), nil
}
ret, err := strconv.ParseFloat(val, 32)
if err != nil {
return nil, errors.Wrapf(err, " %s ", p.Name)
}
return []ParsedValue{ParsedFloat32(ret)}, err
}
func (Float32Parser) Defaults() []ParsedValue {
return []ParsedValue{ParsedFloat32(0)}
}
type StringParser struct {
Name string
}
func (StringParser) Parse(val string) ([]ParsedValue, error) {
return []ParsedValue{ParsedString(val)}, nil
}
func (StringParser) Defaults() []ParsedValue {
return []ParsedValue{ParsedString("")}
}
type BoolParser struct {
Name string
}
func (p BoolParser) Parse(val string) ([]ParsedValue, error) {
if val == "" {
return p.Defaults(), nil
}
if val == "N" {
val = "F"
}
ret, err := strconv.ParseBool(val)
if err != nil {
return p.Defaults(), errors.Wrapf(err, " %s ", p.Name)
}
return []ParsedValue{ParsedBool{ret, true}}, err
}
func (BoolParser) Defaults() []ParsedValue {
return []ParsedValue{ParsedBool{false, false}}
}
type TimeParser struct {
Name string
Layout string
}
func (t TimeParser) Parse(val string) ([]ParsedValue, error) {
tm, err := time.Parse(t.Layout, val)
if err != nil {
return nil, errors.Wrapf(err, " %s ", t.Name)
}
return []ParsedValue{ParsedTime(tm)}, err
}
func (TimeParser) Defaults() []ParsedValue {
return []ParsedValue{ParsedTime(time.Unix(0, 0))}
}
// Skips parsing one value.
type SkipParser struct{
Name string
}
func (SkipParser) Parse(val string) ([]ParsedValue, error) {
return nil,nil
}
func (SkipParser) Defaults() []ParsedValue {
return nil
}
type DecimalParser struct {
Name string
value *decimal.Big
}
var ErrCannotParse = errors.New("cannot parse")
func (d DecimalParser) Parse(val string) ([]ParsedValue, error) {
if val == "" {
return d.Defaults(), nil
}
b, ok := (&decimal.Big{}).SetString(val)
if !ok {
return nil, ErrCannotParse
}
return []ParsedValue{ParsedDecimal{b: b}}, nil
}
func (DecimalParser) Defaults() []ParsedValue {
return []ParsedValue{ParsedDecimal{}}
}