Skip to content

Commit 1732f46

Browse files
author
Stefan Tudose
committed
update README
1 parent b77c603 commit 1732f46

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

README.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# csvdecoder
2+
3+
csvdecoder is a Go library for parsing and decoding csv files into Go objects. It uses [encoding/csv](https://golang.org/pkg/encoding/csv/) for the parsing and follows a usage pattern inspired by the [database/sql](https://golang.org/pkg/database/sql/) package.
4+
5+
The scanning method is *not multi-thread safe*; `Next` and `Scan` are not expected to be called concurrently.
6+
## Installation
7+
8+
```bash
9+
go get github.com/stefantds/csvdecoder
10+
```
11+
12+
## Supported formats
13+
14+
Csvdecoder supports converting columns read from the source file into the following types:
15+
- `*string`
16+
- `*int`, `*int8`, `*int16`, `*int32`, `*int64`
17+
- `*uint`, `*uint8`, `*uint16`, `*uint32`, `*uint64`
18+
- `*bool`
19+
- `*float32`, `*float64`
20+
- a slice of values. Note that the CSV record must be a valid JSON array. If not a JSON array, a custom decoder implementing the `csvdecoder.Interface` interface must be implemented.
21+
- an array of values. Note that the CSV record must be a valid JSON array. If not a JSON array, a custom decoder implementing the `csvdecoder.Interface` interface must be implemented.
22+
- a pointer to any type implementing the `csvdecoder.Interface` interface
23+
24+
## Usage
25+
26+
```golang
27+
import (
28+
"fmt"
29+
"os"
30+
31+
"github.com/stefantds/csvdecoder"
32+
)
33+
34+
type User struct {
35+
Name string
36+
Active bool
37+
Age int
38+
}
39+
40+
func Example_simple() {
41+
// the csv file contains the values:
42+
//john,44,true
43+
//lucy,48,false
44+
//mr hyde,34,true
45+
file, err := os.Open("./data/simple.csv")
46+
if err != nil {
47+
// handle error
48+
return
49+
}
50+
defer file.Close()
51+
52+
// create a new decoder that will read from the given file
53+
decoder, err := csvdecoder.New(file)
54+
if err != nil {
55+
// handle error
56+
return
57+
}
58+
59+
// iterate over the rows in the file
60+
for decoder.Next() {
61+
var u User
62+
63+
// scan the first three values in the name, age and active fields respectively
64+
if err := decoder.Scan(&u.Name, &u.Age, &u.Active); err != nil {
65+
// handle error
66+
return
67+
}
68+
fmt.Println(u)
69+
}
70+
71+
// check if the loop stopped prematurely because of an error
72+
if err = decoder.Err(); err != nil {
73+
// handle error
74+
return
75+
}
76+
77+
// Output: {john true 44}
78+
// {lucy false 48}
79+
// {mr hyde true 34}
80+
}
81+
```
82+
83+
See also the example files for more usage examples.
84+
85+
## Configuration
86+
87+
The behaviour of the decoder can be configured by passing one of following options when creating the decoder:
88+
- Comma: the character that separates values. Default value is comma.
89+
- IgnoreHeaders: if set to true, the first line will be ignored
90+
- IgnoreUnmatchingFields: if set to true, the number of records and scan targets are allowed to be different. By default, if they don't match exactly it will cause an error.
91+
92+
```golang
93+
decoder, err := csvdecoder.NewWithConfig(file, csvdecoder.Config{Comma: ';', IgnoreHeaders: true})
94+
```
95+
96+
## Contributing
97+
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
98+
99+
Please make sure to update tests as needed.
100+
101+
## License
102+
[MIT](https://choosealicense.com/licenses/mit/)

0 commit comments

Comments
 (0)