-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoder_test.go
More file actions
229 lines (183 loc) · 4.72 KB
/
decoder_test.go
File metadata and controls
229 lines (183 loc) · 4.72 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package csvx
import (
"bytes"
"encoding/csv"
"fmt"
"io"
"strconv"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDecoder_Decode(t *testing.T) {
type namedType string
type targetType struct {
String string `csv:"string"`
Int int `csv:"int"`
Int64 int64 `csv:"int64"`
NamedType namedType `csv:"namedType"`
PtrInt *int `csv:"ptrInt"`
PtrIntNull *int `csv:"ptrIntNull"`
PtrBool *bool `csv:"ptrBool"`
PtrString *string `csv:"ptrString"`
Float64 float64 `csv:"float64"`
PtrFloat64 *float64 `csv:"ptrFloat64"`
NonCSVField string
}
ptrIntVal := 21
ptrBoolVal := true
ptrStringVal := "PtrStringVal..."
ptrFloat64Val := 50.5
fields := []string{"string", "int", "int64", "namedType", "ptrInt", "ptrIntNull", "ptrBool", "ptrString", "float64", "ptrFloat64"}
decoder := NewDecoder(fields)
csvData := bytes.NewBufferString(`Hello World!,50,50,Hello World 2!,21,,true,PtrStringVal...,50.5,50.5`)
wanted := &targetType{
String: "Hello World!",
Int: 50,
Int64: 50,
NamedType: "Hello World 2!",
PtrInt: &ptrIntVal,
PtrIntNull: nil,
PtrBool: &ptrBoolVal,
PtrString: &ptrStringVal,
Float64: 50.5,
PtrFloat64: &ptrFloat64Val,
}
var results []*targetType
reader := csv.NewReader(csvData)
for {
valueStrings, err := reader.Read()
if err != nil {
if err == io.EOF {
break
}
require.NoError(t, err)
}
target := new(targetType)
err = decoder.Decode(valueStrings, target)
require.NoError(t, err)
results = append(results, target)
}
require.Len(t, results, 1)
assert.Equal(t, wanted, results[0])
}
func ExampleDecoder() {
// setup types. Note "csv" field tag.
type targetType struct {
Name string `csv:"name"`
Age *int `csv:"age"`
}
fields := []string{"name", "age"}
decoder := NewDecoder(fields)
csvData := bytes.NewBufferString("John Smith,40\nJane Doe,")
var results []*targetType
// use stdlib csv reader to read line by line []string slices
reader := csv.NewReader(csvData)
for {
valueStrings, err := reader.Read()
if err != nil {
if err == io.EOF {
break
}
// unexpected error
panic(err)
}
target := new(targetType)
err = decoder.Decode(valueStrings, target)
if err != nil {
panic(err)
}
results = append(results, target)
}
fmt.Printf("Found %d results\n", len(results))
for _, result := range results {
age := "nil"
if result.Age != nil {
age = fmt.Sprintf("%d", *result.Age)
}
fmt.Printf("%s: %s\n", result.Name, age)
}
// Output:
// Found 2 results
// John Smith: 40
// Jane Doe: nil
}
func Test_embedded_struct_decode(t *testing.T) {
type EmbeddedType struct {
Field1 string `csv:"field1"`
}
type EmbeddedType2 struct {
Field2 int `csv:"field2"`
Field3 *float64 `csv:"field3"`
}
type SubType struct {
Field4 string `csv:"field4"`
}
type myType struct {
*EmbeddedType // pointer
EmbeddedType2 // not pointer
SubType SubType
}
t.Run("no silent fail for invalid fields", func(t *testing.T) {
obj := myType{}
values := []string{"50", "Test1", "0", "Test2"}
decoder := NewDecoder([]string{"field2", "field1", "field3", "field4"})
err := decoder.Decode(values, &obj)
require.Error(t, err)
})
t.Run("decode", func(t *testing.T) {
obj := myType{
EmbeddedType: &EmbeddedType{},
}
values := []string{"50", "Test1", "10", "Test2"}
decoder := NewDecoder([]string{"field2", "field1", "field3", "field4"})
err := decoder.Decode(values, &obj)
require.NoError(t, err)
expected := myType{
EmbeddedType: &EmbeddedType{
Field1: "Test1",
},
EmbeddedType2: EmbeddedType2{
Field2: 50,
Field3: toFloatPtr(10),
},
SubType: SubType{
Field4: "Test2",
},
}
assert.Equal(t, expected, obj)
})
}
func Test_decode_time(t *testing.T) {
type MyType struct {
Submitted time.Time `csv:"submitted"`
}
obj := MyType{Submitted: time.Time{}}
decoder := NewDecoder([]string{"submitted"})
err := decoder.Decode([]string{"1970-01-12T14:46:40+01:00"}, &obj)
require.NoError(t, err)
assert.Equal(t, obj.Submitted, time.Unix(1000000, 0))
}
func Test_CustomDecoderFunc(t *testing.T) {
decoder := NewDecoder([]string{"date"})
decoder.CustomDecoderMap = map[string]CustomDecoderFunc{
"date": func(val string) (interface{}, error) {
valInt, err := strconv.ParseInt(val, 10, 64)
if err != nil {
return nil, err
}
return time.Unix(valInt, 0), nil
},
}
type myType struct {
Date time.Time `csv:"date"`
}
obj := myType{}
err := decoder.Decode([]string{"1000000"}, &obj)
require.NoError(t, err)
assert.Equal(t, time.Unix(1000000, 0), obj.Date)
}
func toFloatPtr(val float64) *float64 {
return &val
}