This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
forked from gocarina/gocsv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
288 lines (265 loc) · 8.81 KB
/
types.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
package gocsv_alt
import (
"encoding"
"fmt"
"reflect"
"strconv"
)
// --------------------------------------------------------------------------
// Conversion interfaces
// TypeMarshaller is implemented by any value that has a MarshalCSV method
// This converter is used to convert the value to it string representation
type TypeMarshaller interface {
MarshalCSV() (string, error)
}
// Stringer is implemented by any value that has a String method
// This converter is used to convert the value to it string representation
// This converter will be used if your value does not implement TypeMarshaller
type Stringer interface {
String() string
}
// TypeUnmarshaller is implemented by any value that has an UnmarshalCSV method
// This converter is used to convert a string to your value representation of that string
type TypeUnmarshaller interface {
UnmarshalCSV(string) error
}
var (
stringerType = reflect.TypeOf((*Stringer)(nil)).Elem()
marshallerType = reflect.TypeOf((*TypeMarshaller)(nil)).Elem()
unMarshallerType = reflect.TypeOf((*TypeUnmarshaller)(nil)).Elem()
textMarshalerType = reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem()
textUnMarshalerType = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()
)
// --------------------------------------------------------------------------
// Conversion helpers
func toString(in interface{}) (string, error) {
inValue := reflect.ValueOf(in)
switch inValue.Kind() {
case reflect.String:
return inValue.String(), nil
case reflect.Bool:
b := inValue.Bool()
if b {
return "true", nil
}
return "false", nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return fmt.Sprintf("%v", inValue.Int()), nil
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return fmt.Sprintf("%v", inValue.Uint()), nil
case reflect.Float32, reflect.Float64:
return strconv.FormatFloat(inValue.Float(), byte('f'), 64, 64), nil
}
return "", fmt.Errorf("No known conversion from " + inValue.Type().String() + " to string")
}
func toBool(in interface{}) (bool, error) {
inValue := reflect.ValueOf(in)
switch inValue.Kind() {
case reflect.String:
s := inValue.String()
if s == "true" || s == "yes" || s == "1" {
return true, nil
} else if s == "false" || s == "no" || s == "0" {
return false, nil
}
case reflect.Bool:
return inValue.Bool(), nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
i := inValue.Int()
if i != 0 {
return true, nil
}
return false, nil
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
i := inValue.Uint()
if i != 0 {
return true, nil
}
return false, nil
case reflect.Float32, reflect.Float64:
f := inValue.Float()
if f != 0 {
return true, nil
}
return false, nil
}
return false, fmt.Errorf("No known conversion from " + inValue.Type().String() + " to bool")
}
func toInt(in interface{}) (int64, error) {
inValue := reflect.ValueOf(in)
switch inValue.Kind() {
case reflect.String:
return strconv.ParseInt(inValue.String(), 0, 64)
case reflect.Bool:
if inValue.Bool() {
return 1, nil
}
return 0, nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return inValue.Int(), nil
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return int64(inValue.Uint()), nil
case reflect.Float32, reflect.Float64:
return int64(inValue.Float()), nil
}
return 0, fmt.Errorf("No known conversion from " + inValue.Type().String() + " to int")
}
func toUint(in interface{}) (uint64, error) {
inValue := reflect.ValueOf(in)
switch inValue.Kind() {
case reflect.String:
return strconv.ParseUint(inValue.String(), 0, 64)
case reflect.Bool:
if inValue.Bool() {
return 1, nil
}
return 0, nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return uint64(inValue.Int()), nil
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return inValue.Uint(), nil
case reflect.Float32, reflect.Float64:
return uint64(inValue.Float()), nil
}
return 0, fmt.Errorf("No known conversion from " + inValue.Type().String() + " to uint")
}
func toFloat(in interface{}) (float64, error) {
inValue := reflect.ValueOf(in)
switch inValue.Kind() {
case reflect.String:
return strconv.ParseFloat(inValue.String(), 64)
case reflect.Bool:
if inValue.Bool() {
return 1, nil
}
return 0, nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return float64(inValue.Int()), nil
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return float64(inValue.Uint()), nil
case reflect.Float32, reflect.Float64:
return inValue.Float(), nil
}
return 0, fmt.Errorf("No known conversion from " + inValue.Type().String() + " to float")
}
func setField(field reflect.Value, value string) error {
switch field.Kind() {
case reflect.String:
s, err := toString(value)
if err != nil {
return err
}
field.SetString(s)
case reflect.Bool:
b, err := toBool(value)
if err != nil {
return err
}
field.SetBool(b)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
i, err := toInt(value)
if err != nil {
return err
}
field.SetInt(i)
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
ui, err := toUint(value)
if err != nil {
return err
}
field.SetUint(ui)
case reflect.Float32, reflect.Float64:
f, err := toFloat(value)
if err != nil {
return err
}
field.SetFloat(f)
default:
return unmarshall(field, value)
}
return nil
}
func getFieldAsString(field reflect.Value) (str string, err error) {
switch field.Kind() {
case reflect.String:
return field.String(), nil
case reflect.Bool:
str, err = toString(field.Bool())
if err != nil {
return str, err
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
str, err = toString(field.Int())
if err != nil {
return str, err
}
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
str, err = toString(field.Uint())
if err != nil {
return str, err
}
case reflect.Float32, reflect.Float64:
str, err = toString(field.Float())
if err != nil {
return str, err
}
default:
return marshall(field)
}
return str, nil
}
// --------------------------------------------------------------------------
// Un/serializations helpers
func unmarshall(field reflect.Value, value string) error {
dupField := field
unMarshallIt := func(finalField reflect.Value) error {
if finalField.CanInterface() && finalField.Type().Implements(unMarshallerType) {
if err := finalField.Interface().(TypeUnmarshaller).UnmarshalCSV(value); err != nil {
return err
}
return nil
} else if finalField.CanInterface() && finalField.Type().Implements(textUnMarshalerType) { // Otherwise try to use TextMarshaller
if err := finalField.Interface().(encoding.TextUnmarshaler).UnmarshalText([]byte(value)); err != nil {
return err
}
}
return fmt.Errorf("No known conversion from string to " + field.Type().String() + ", " + field.Type().String() + " does not implements TypeUnmarshaller")
}
for dupField.Kind() == reflect.Interface || dupField.Kind() == reflect.Ptr {
if dupField.IsNil() {
dupField = reflect.New(field.Type().Elem())
field.Set(dupField)
return unMarshallIt(dupField)
break
}
dupField = dupField.Elem()
}
if dupField.CanAddr() {
return unMarshallIt(dupField.Addr())
}
return fmt.Errorf("No known conversion from string to " + field.Type().String() + ", " + field.Type().String() + " does not implements TypeUnmarshaller")
}
func marshall(field reflect.Value) (value string, err error) {
dupField := field
marshallIt := func(finalField reflect.Value) (string, error) {
if finalField.CanInterface() && finalField.Type().Implements(marshallerType) { // Use TypeMarshaller when possible
return finalField.Interface().(TypeMarshaller).MarshalCSV()
} else if finalField.CanInterface() && finalField.Type().Implements(stringerType) { // Otherwise try to use Stringer
return finalField.Interface().(Stringer).String(), nil
} else if finalField.CanInterface() && finalField.Type().Implements(textMarshalerType) { // Otherwise try to use TextMarshaller
text, err := finalField.Interface().(encoding.TextMarshaler).MarshalText()
return string(text), err
}
return value, fmt.Errorf("No known conversion from " + field.Type().String() + " to string, " + field.Type().String() + " does not implements TypeMarshaller nor Stringer")
}
for dupField.Kind() == reflect.Interface || dupField.Kind() == reflect.Ptr {
if dupField.IsNil() {
return value, nil
}
dupField = dupField.Elem()
}
if dupField.CanAddr() {
return marshallIt(dupField.Addr())
}
return value, fmt.Errorf("No known conversion from " + field.Type().String() + " to string, " + field.Type().String() + " does not implements TypeMarshaller nor Stringer")
}