Skip to content

Commit b77c603

Browse files
author
Stefan Tudose
committed
add some examples
1 parent eabbfaa commit b77c603

File tree

4 files changed

+188
-0
lines changed

4 files changed

+188
-0
lines changed

data/simple.csv

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
john,44,true
2+
lucy,48,false
3+
mr hyde,34,true

example_custom_decoder_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package csvdecoder_test
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"strings"
7+
8+
"github.com/stefantds/csvdecoder"
9+
)
10+
11+
type Point struct {
12+
X int
13+
Y int
14+
}
15+
16+
// DecodeRecord implements the csvdecoder.Interface type
17+
func (p *Point) DecodeRecord(record string) error {
18+
// the decode code is specific to the way the object is serialized.
19+
// in this example the point is encoded as a JSON array with two values
20+
21+
data := make([]int, 2)
22+
if err := json.NewDecoder(strings.NewReader(record)).Decode(&data); err != nil {
23+
return fmt.Errorf("could not parse %s as JSON array: %w", record, err)
24+
}
25+
26+
(*p).X = data[0]
27+
(*p).Y = data[1]
28+
29+
return nil
30+
}
31+
32+
func Example_custom_decoder() {
33+
// the csv separator is a semicolon in this example
34+
exampleData := strings.NewReader(
35+
`[0, 0];[0, 2];[1, 2]
36+
[-1, 2];[0, -2];[1, 0]
37+
`)
38+
39+
// create a new decoder that will read from the given file
40+
decoder, err := csvdecoder.NewWithConfig(exampleData, csvdecoder.Config{Comma: ';'})
41+
if err != nil {
42+
// handle error
43+
return
44+
}
45+
46+
// iterate over the rows in the file
47+
for decoder.Next() {
48+
var a, b, c Point
49+
50+
// scan the first values to the types
51+
if err := decoder.Scan(&a, &b, &c); err != nil {
52+
// handle error
53+
return
54+
}
55+
fmt.Printf("a: %v, b: %v, c: %v\n", a, b, c)
56+
}
57+
58+
// check if the loop stopped prematurely because of an error
59+
if err = decoder.Err(); err != nil {
60+
// handle error
61+
return
62+
}
63+
64+
// Output: a: {0 0}, b: {0 2}, c: {1 2}
65+
// a: {-1 2}, b: {0 -2}, c: {1 0}
66+
}

example_simple_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package csvdecoder_test
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/stefantds/csvdecoder"
8+
)
9+
10+
type User struct {
11+
Name string
12+
Active bool
13+
Age int
14+
}
15+
16+
func Example_simple() {
17+
18+
// the csv file contains the values:
19+
//john,44,true
20+
//lucy,48,false
21+
//mr hyde,34,true
22+
file, err := os.Open("./data/simple.csv")
23+
if err != nil {
24+
// handle error
25+
return
26+
}
27+
defer file.Close()
28+
29+
// create a new decoder that will read from the given file
30+
decoder, err := csvdecoder.New(file)
31+
if err != nil {
32+
// handle error
33+
return
34+
}
35+
36+
// iterate over the rows in the file
37+
for decoder.Next() {
38+
var u User
39+
40+
// scan the first three values in the name, age and active fields respectively
41+
if err := decoder.Scan(&u.Name, &u.Age, &u.Active); err != nil {
42+
// handle error
43+
return
44+
}
45+
fmt.Println(u)
46+
}
47+
48+
// check if the loop stopped prematurely because of an error
49+
if err = decoder.Err(); err != nil {
50+
// handle error
51+
return
52+
}
53+
54+
// Output: {john true 44}
55+
// {lucy false 48}
56+
// {mr hyde true 34}
57+
}

example_slices_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package csvdecoder_test
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/stefantds/csvdecoder"
8+
)
9+
10+
type MyStringCollection []string
11+
12+
// DecodeRecord implements the csvdecoder.Interface type
13+
func (c *MyStringCollection) DecodeRecord(record string) error {
14+
// the decode code is specific to the way the value is serialized.
15+
// in this example the array is represented as int values separated by space
16+
*c = MyStringCollection(strings.Split(record, " "))
17+
18+
return nil
19+
}
20+
21+
func Example_slices() {
22+
// the csv separator is a semicolon in this example
23+
// the values are arrays serialized in two different ways.
24+
exampleData := strings.NewReader(
25+
`jon;elvis boris ahmed jane;["jo", "j"]
26+
jane;lucas george;["j", "jay"]
27+
`)
28+
29+
// create a new decoder that will read from the given file
30+
decoder, err := csvdecoder.NewWithConfig(exampleData, csvdecoder.Config{Comma: ';'})
31+
if err != nil {
32+
// handle error
33+
return
34+
}
35+
36+
type Person struct {
37+
Name string
38+
Friends MyStringCollection
39+
Nicknames []string
40+
}
41+
42+
// iterate over the rows in the file
43+
for decoder.Next() {
44+
var p Person
45+
46+
// scan the first values to the types
47+
if err := decoder.Scan(&p.Name, &p.Friends, &p.Nicknames); err != nil {
48+
// handle error
49+
return
50+
}
51+
fmt.Printf("%v\n", p)
52+
}
53+
54+
// check if the loop stopped prematurely because of an error
55+
if err = decoder.Err(); err != nil {
56+
// handle error
57+
return
58+
}
59+
60+
// Output: {jon [elvis boris ahmed jane] [jo j]}
61+
// {jane [lucas george] [j jay]}
62+
}

0 commit comments

Comments
 (0)