-
Notifications
You must be signed in to change notification settings - Fork 63
/
example_decoder_test.go
219 lines (176 loc) · 4.06 KB
/
example_decoder_test.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
package csvutil_test
import (
"encoding/csv"
"fmt"
"io"
"log"
"strings"
"github.com/jszwec/csvutil"
)
func ExampleDecoder_Decode() {
type User struct {
ID *int `csv:"id,omitempty"`
Name string `csv:"name"`
City string `csv:"city"`
Age int `csv:"age"`
}
csvReader := csv.NewReader(strings.NewReader(`
id,name,age,city
,alice,25,la
,bob,30,ny`))
dec, err := csvutil.NewDecoder(csvReader)
if err != nil {
log.Fatal(err)
}
var users []User
for {
var u User
if err := dec.Decode(&u); err == io.EOF {
break
} else if err != nil {
log.Fatal(err)
}
users = append(users, u)
}
fmt.Println(users)
// Output:
// [{<nil> alice la 25} {<nil> bob ny 30}]
}
func ExampleDecoder_Decode_slice() {
type User struct {
ID *int `csv:"id,omitempty"`
Name string `csv:"name"`
City string `csv:"city"`
Age int `csv:"age"`
}
csvReader := csv.NewReader(strings.NewReader(`
id,name,age,city
,alice,25,la
,bob,30,ny`))
dec, err := csvutil.NewDecoder(csvReader)
if err != nil {
log.Fatal(err)
}
var users []User
if err := dec.Decode(&users); err != nil {
log.Fatal(err)
}
fmt.Println(users)
// Output:
// [{<nil> alice la 25} {<nil> bob ny 30}]
}
func ExampleDecoder_Decode_array() {
type User struct {
ID *int `csv:"id,omitempty"`
Name string `csv:"name"`
City string `csv:"city"`
Age int `csv:"age"`
}
csvReader := csv.NewReader(strings.NewReader(`
id,name,age,city
,alice,25,la
,bob,30,ny
,john,29,ny`))
dec, err := csvutil.NewDecoder(csvReader)
if err != nil {
log.Fatal(err)
}
var users [2]User
if err := dec.Decode(&users); err != nil {
log.Fatal(err)
}
fmt.Println(users)
// Output:
// [{<nil> alice la 25} {<nil> bob ny 30}]
}
func ExampleDecoder_Unused() {
type User struct {
Name string `csv:"name"`
City string `csv:"city"`
Age int `csv:"age"`
OtherData map[string]string `csv:"-"`
}
csvReader := csv.NewReader(strings.NewReader(`
name,age,city,zip
alice,25,la,90005
bob,30,ny,10005`))
dec, err := csvutil.NewDecoder(csvReader)
if err != nil {
log.Fatal(err)
}
header := dec.Header()
var users []User
for {
u := User{OtherData: make(map[string]string)}
if err := dec.Decode(&u); err == io.EOF {
break
} else if err != nil {
log.Fatal(err)
}
for _, i := range dec.Unused() {
u.OtherData[header[i]] = dec.Record()[i]
}
users = append(users, u)
}
fmt.Println(users)
// Output:
// [{alice la 25 map[zip:90005]} {bob ny 30 map[zip:10005]}]
}
func ExampleDecoder_decodeEmbedded() {
type Address struct {
ID int `csv:"id"` // same field as in User - this one will be empty
City string `csv:"city"`
State string `csv:"state"`
}
type User struct {
Address
ID int `csv:"id"` // same field as in Address - this one wins
Name string `csv:"name"`
Age int `csv:"age"`
}
csvReader := csv.NewReader(strings.NewReader(
"id,name,age,city,state\n" +
"1,alice,25,la,ca\n" +
"2,bob,30,ny,ny"))
dec, err := csvutil.NewDecoder(csvReader)
if err != nil {
log.Fatal(err)
}
var users []User
for {
var u User
if err := dec.Decode(&u); err == io.EOF {
break
} else if err != nil {
log.Fatal(err)
}
users = append(users, u)
}
fmt.Println(users)
// Output:
// [{{0 la ca} 1 alice 25} {{0 ny ny} 2 bob 30}]
}
func ExampleDecoder_Decode_inline() {
type Address struct {
Street string `csv:"street"`
City string `csv:"city"`
}
type User struct {
Name string `csv:"name"`
Address Address `csv:",inline"`
HomeAddress Address `csv:"home_address_,inline"`
WorkAddress Address `csv:"work_address_,inline"`
Age int `csv:"age,omitempty"`
}
data := []byte(
"name,street,city,home_address_street,home_address_city,work_address_street,work_address_city,age\n" +
"John,Washington,Boston,Boylston,Boston,River St,Cambridge,26",
)
var users []User
if err := csvutil.Unmarshal(data, &users); err != nil {
fmt.Println("error:", err)
}
fmt.Println(users)
// Output:
// [{John {Washington Boston} {Boylston Boston} {River St Cambridge} 26}]
}