-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathexample_test.go
165 lines (155 loc) · 3.56 KB
/
example_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
package easycsv
import (
"bytes"
"fmt"
"log"
"reflect"
"time"
)
func ExampleReader_read() {
r := NewReaderFile("testdata/sample.csv")
var entry struct {
Name string `index:"0"`
Age int `index:"1"`
}
for r.Read(&entry) {
fmt.Print(entry)
}
if err := r.Done(); err != nil {
log.Fatalf("Failed to read a CSV file: %v", err)
}
// Output: {Alice 10}{Bob 20}
}
func ExampleReader_loop() {
r := NewReaderFile("testdata/sample.csv")
err := r.Loop(func(entry *struct {
Name string `index:"0"`
Age int `index:"1"`
}) error {
fmt.Print(entry)
return nil
})
if err != nil {
log.Fatalf("Failed to read a CSV file: %v", err)
}
// Output: &{Alice 10}&{Bob 20}
}
func ExampleReader_readAll() {
r := NewReaderFile("testdata/sample.csv")
var entry []struct {
Name string `index:"0"`
Age int `index:"1"`
}
if err := r.ReadAll(&entry); err != nil {
log.Fatalf("Failed to read a CSV file: %v", err)
}
fmt.Println(entry)
// Output: [{Alice 10} {Bob 20}]
}
func ExampleReader_tSV() {
r := NewReaderFile("testdata/sample.tsv", Option{
Comma: '\t',
})
var entry struct {
Name string `index:"0"`
Age int `index:"1"`
}
for r.Read(&entry) {
fmt.Print(entry)
}
if err := r.Done(); err != nil {
log.Fatalf("Failed to read a CSV file: %v", err)
}
// Output: {Alice 10}{Bob 20}
}
func ExampleReader_encodings() {
r := NewReader(bytes.NewReader([]byte("010,010,010")))
var entry struct {
Deci int `index:"0" enc:"deci"`
Oct int `index:"1" enc:"oct"`
Hex int `index:"2" enc:"hex"`
}
for r.Read(&entry) {
fmt.Print(entry)
}
if err := r.Done(); err != nil {
log.Fatalf("Failed to read: %v", err)
}
// Output: {10 8 16}
}
func ExampleReader_typeEncodings() {
r := NewReader(bytes.NewReader([]byte("2017-01-02,2016-02-03\n2015-03-04,2014-04-05")),
Option{TypeDecoders: map[reflect.Type]interface{}{
reflect.TypeOf(time.Time{}): func(s string) (time.Time, error) {
return time.Parse("2006-01-02", s)
},
}})
var entry []time.Time
for r.Read(&entry) {
for _, e := range entry {
fmt.Print(e.Format("2006/1/2"), ";")
}
}
if err := r.Done(); err != nil {
log.Fatalf("Failed to read: %v", err)
}
// Output: 2017/1/2;2016/2/3;2015/3/4;2014/4/5;
}
func ExampleReader_LineNumber_reader() {
r := NewReaderFile("testdata/sample.csv")
var entry struct {
Name string `index:"0"`
Age int `index:"1"`
}
bob := "Bob"
lino := 0
for r.Read(&entry) {
if entry.Name == bob {
lino = r.LineNumber()
}
}
if lino > 0 {
fmt.Printf("Found %s at line %d", bob, lino)
}
// Output: Found Bob at line 2
}
func ExampleReader_DoneDefer() {
f := func() (err error) {
r := NewReaderFile("testdata/sample.csv")
defer r.DoneDefer(&err)
var entry struct {
Name string `index:"3"`
}
// This fails with a index-out-of-range error.
for r.Read(&entry) {
err = fmt.Errorf("This block must not be executed")
}
return
}
err := f()
if err != nil {
fmt.Printf("Failed: %v", err)
}
// Output: Failed: Accessed index 3 though the size of the row is 2
}
func ExampleOption_decoders() {
r := NewReader(bytes.NewBufferString("name,birthday\nAlice,1980-12-30\nBob,1975-06-09"),
Option{
Decoders: map[string]interface{}{
"date": func(s string) (time.Time, error) {
return time.Parse("2006-01-02", s)
},
},
})
var entry struct {
Name string `name:"name"`
Birth time.Time `name:"birthday" enc:"date"`
}
for r.Read(&entry) {
fmt.Print(entry)
}
if err := r.Done(); err != nil {
log.Fatalf("Failed: %v\n", err)
}
// Output: {Alice 1980-12-30 00:00:00 +0000 UTC}{Bob 1975-06-09 00:00:00 +0000 UTC}
}