Skip to content

Commit c71c0e1

Browse files
author
Stefan Tudose
committed
add doc for package
1 parent 47171bf commit c71c0e1

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
# csvdecoder
22

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.
3+
csvdecoder is a Go library for parsing and deserializing csv files into Go objects.
4+
It relies on [encoding/csv](https://golang.org/pkg/encoding/csv/) for the actual parsing of the CSV file and follows a similar usage pattern as the [database/sql](https://golang.org/pkg/database/sql/) package for scanning rows.
5+
6+
csvdecoder allows to iterate through the CSV records (using 'Next') and scan the fields into target variables or fields of variables (using 'Scan').
7+
8+
The scanning method is **not thread safe**; `Next` and `Scan` are not expected to be called concurrently.
49

5-
The scanning method is *not multi-thread safe*; `Next` and `Scan` are not expected to be called concurrently.
610
## Installation
711

812
```bash
@@ -84,19 +88,21 @@ See also the example files for more usage examples.
8488

8589
## Configuration
8690

87-
The behaviour of the decoder can be configured by passing one of following options when creating the decoder:
91+
The behavior of the decoder can be configured by passing one of following options when creating the decoder:
8892
- Comma: the character that separates values. Default value is comma.
89-
- IgnoreHeaders: if set to true, the first line will be ignored
93+
- IgnoreHeaders: if set to true, the first line will be ignored. This is useful when the CSV file contains a header line.
9094
- 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.
9195

9296
```golang
9397
decoder, err := csvdecoder.NewWithConfig(file, csvdecoder.Config{Comma: ';', IgnoreHeaders: true})
9498
```
9599

96100
## Contributing
101+
97102
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
98103

99104
Please make sure to update tests as needed.
100105

101106
## License
107+
102108
[MIT](https://choosealicense.com/licenses/mit/)

doc.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// csvdecoder is a tool for parsing and deserializing CSV values into Go objects.
2+
// It follows the same usage pattern as the Rows scanning using database/sql package.
3+
// It relies on encoding/csv for the actual csv parsing.
4+
5+
// csvdecoder allows to iterate through the CSV records (using 'Next')
6+
// and scan the fields into target variables or fields of variables (using 'Scan').
7+
// The 'Next' and 'Scan' methods are not thread-safe and are not expected to be called concurrently.
8+
9+
// csvdecoder supports converting CSV fields into any of the following types:
10+
// - `*string`
11+
// - `*int`, `*int8`, `*int16`, `*int32`, `*int64`
12+
// - `*uint`, `*uint8`, `*uint16`, `*uint32`, `*uint64`
13+
// - `*bool`
14+
// - `*float32`, `*float64`
15+
// - 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.
16+
// - 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.
17+
// - a pointer to any type implementing the `csvdecoder.Interface` interface
18+
19+
// csvdecoder uses the same terminology as package encoding/csv:
20+
// A csv file contains zero or more records. Each record contains one or more
21+
// fields separated by the fields separator (the "comma"). The fields separator character
22+
// can be configured to be another character than comma.
23+
// Each record is separated by the newline character. The final record may
24+
// optionally be followed by a newline character.
25+
26+
// The behavior of the decoder can be configured by passing one of following options when creating the decoder:
27+
// - Comma: the character that separates values. The default value is comma.
28+
// - IgnoreHeaders: if set to true, the first line will be ignored. This is useful when the CSV file contains a header line.
29+
// - 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.
30+
31+
// See README.md for more info.
32+
package csvdecoder

0 commit comments

Comments
 (0)