-
Notifications
You must be signed in to change notification settings - Fork 62
/
example_encoder_test.go
217 lines (176 loc) · 4.32 KB
/
example_encoder_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
package csvutil_test
import (
"bytes"
"encoding/csv"
"fmt"
"strconv"
"time"
"github.com/jszwec/csvutil"
)
func ExampleEncoder_Encode_streaming() {
type Address struct {
City string
Country string
}
type User struct {
Name string
Address
Age int `csv:"age,omitempty"`
}
users := []User{
{Name: "John", Address: Address{"Boston", "USA"}, Age: 26},
{Name: "Bob", Address: Address{"LA", "USA"}, Age: 27},
{Name: "Alice", Address: Address{"SF", "USA"}},
}
var buf bytes.Buffer
w := csv.NewWriter(&buf)
enc := csvutil.NewEncoder(w)
for _, u := range users {
if err := enc.Encode(u); err != nil {
fmt.Println("error:", err)
}
}
w.Flush()
if err := w.Error(); err != nil {
fmt.Println("error:", err)
}
fmt.Println(buf.String())
// Output:
// Name,City,Country,age
// John,Boston,USA,26
// Bob,LA,USA,27
// Alice,SF,USA,
}
func ExampleEncoder_Encode_all() {
type Address struct {
City string
Country string
}
type User struct {
Name string
Address
Age int `csv:"age,omitempty"`
}
users := []User{
{Name: "John", Address: Address{"Boston", "USA"}, Age: 26},
{Name: "Bob", Address: Address{"LA", "USA"}, Age: 27},
{Name: "Alice", Address: Address{"SF", "USA"}},
}
var buf bytes.Buffer
w := csv.NewWriter(&buf)
if err := csvutil.NewEncoder(w).Encode(users); err != nil {
fmt.Println("error:", err)
}
w.Flush()
if err := w.Error(); err != nil {
fmt.Println("error:", err)
}
fmt.Println(buf.String())
// Output:
// Name,City,Country,age
// John,Boston,USA,26
// Bob,LA,USA,27
// Alice,SF,USA,
}
func ExampleEncoder_EncodeHeader() {
type User struct {
Name string
Age int `csv:"age,omitempty"`
}
var buf bytes.Buffer
w := csv.NewWriter(&buf)
enc := csvutil.NewEncoder(w)
if err := enc.EncodeHeader(User{}); err != nil {
fmt.Println("error:", err)
}
w.Flush()
if err := w.Error(); err != nil {
fmt.Println("error:", err)
}
fmt.Println(buf.String())
// Output:
// Name,age
}
func ExampleEncoder_Encode_inline() {
type Owner struct {
Name string `csv:"name"`
}
type Address struct {
Street string `csv:"street"`
City string `csv:"city"`
Owner Owner `csv:"owner_,inline"`
}
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"`
}
users := []User{
{
Name: "John",
Address: Address{"Washington", "Boston", Owner{"Steve"}},
HomeAddress: Address{"Boylston", "Boston", Owner{"Steve"}},
WorkAddress: Address{"River St", "Cambridge", Owner{"Steve"}},
Age: 26,
},
}
b, err := csvutil.Marshal(users)
if err != nil {
fmt.Println("error:", err)
}
fmt.Printf("%s\n", b)
// Output:
// name,street,city,owner_name,home_address_street,home_address_city,home_address_owner_name,work_address_street,work_address_city,work_address_owner_name,age
// John,Washington,Boston,Steve,Boylston,Boston,Steve,River St,Cambridge,Steve,26
}
func ExampleEncoder_Register() {
type Foo struct {
Time time.Time `csv:"time"`
Hex int `csv:"hex"`
PtrHex *int `csv:"ptr_hex"`
Buffer *bytes.Buffer `csv:"buffer"`
}
foos := []Foo{
{
Time: time.Date(2020, 6, 20, 12, 0, 0, 0, time.UTC),
Hex: 15,
Buffer: bytes.NewBufferString("hello"),
},
}
marshalInt := csvutil.MarshalFunc(func(n *int) ([]byte, error) {
if n == nil {
return []byte("NULL"), nil
}
return strconv.AppendInt(nil, int64(*n), 16), nil
})
marshalTime := csvutil.MarshalFunc(func(t time.Time) ([]byte, error) {
return t.AppendFormat(nil, time.Kitchen), nil
})
// all fields which implement String method will use this, unless their
// concrete type was already overriden.
marshalStringer := csvutil.MarshalFunc(func(s fmt.Stringer) ([]byte, error) {
return []byte(s.String()), nil
})
marshalers := csvutil.NewMarshalers(
marshalInt,
marshalTime,
marshalStringer,
)
var buf bytes.Buffer
w := csv.NewWriter(&buf)
enc := csvutil.NewEncoder(w)
enc.WithMarshalers(marshalers)
if err := enc.Encode(foos); err != nil {
fmt.Println("error:", err)
}
w.Flush()
if err := w.Error(); err != nil {
fmt.Println("error:", err)
}
fmt.Println(buf.String())
// Output:
// time,hex,ptr_hex,buffer
// 12:00PM,f,NULL,hello
}