https://github.com/jamesrr39/csvx
Convienience tools for CSV in Go
https://github.com/jamesrr39/csvx
csv csv-parser go golang
Last synced: 2 months ago
JSON representation
Convienience tools for CSV in Go
- Host: GitHub
- URL: https://github.com/jamesrr39/csvx
- Owner: jamesrr39
- License: apache-2.0
- Created: 2022-03-31T07:06:51.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2022-07-26T23:19:46.000Z (over 3 years ago)
- Last Synced: 2024-06-20T05:10:15.896Z (almost 2 years ago)
- Topics: csv, csv-parser, go, golang
- Language: Go
- Homepage:
- Size: 46.9 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# csvx
[](https://pkg.go.dev/github.com/jamesrr39/csvx)
`csvx` is a package with a CSV string-fields-to-struct encoder and decoder for Go. It makes converting raw strings from CSV files into objects (and vice-versa) much easier.
It is licensed under the permissive Apache 2 license.
## Usage
Use with the stdlib `csv` reader. Use this library to quickly turn `[]string` into an object, or an object into `[]string`.
```
type targetType struct {
Name string `csv:"name"`
Age *int `csv:"age"`
NonCSVField string // field will be ignored by struct scanner, since it is missing the "csv" tag
}
// decoding
fields := []string{"name", "age"}
decoder := NewDecoder(fields)
target := new(targetType)
err := decoder.Decode([]string{"John Smith","40"}, target)
if err != nil {
panic(err)
}
fmt.Printf("result: %#v\n", target)
// encoding
encoder := NewEncoder(fields)
records, err := encoder.Encode(target)
if err != nil {
panic(err)
}
fmt.Printf("records: %#v\n", records)
```
See also the example on [pkg.go.dev](https://pkg.go.dev/github.com/jamesrr39/csvx#example-package) and the tests in this package for more examples.
## Implemented Types
- [x] string
- [x] int
- [x] int64
- [x] int32
- [x] int16
- [x] int8
- [x] uint
- [x] uint64
- [x] uint32
- [x] uint16
- [x] uint8
- [x] float64
- [x] float32
- [x] bool (`true`, `yes`, `1`, `1.0` = true, `false`, `no`, `0`, `0.0` = false, other values result in an error, customisable in the `Encoder` and `Decoder` fields)
- [x] struct with encoding.TextUnmarshaler and encoding.TextMarshaler implemented on them
- [x] Pointer types to above underlying types, e.g. `*string` (empty string and `null` result in `nil` being set on the Go struct)
- [x] Custom non-struct types, e.g. `type Name string`, so long as the underlying type is in the list above.
## Performance
The struct scanner uses `reflect` quite heavily, so this library will not be as fast as writing a specific parser for the struct. However, for the vast majority of cases, the performance hit will be acceptable and the development speed increase and simple client code will be worth it!
## Auditability/readability
This aims to be a simple, easy-to-audit library with stdlib-only dependencies (`github.com/stretchr/testify` is also used, but only for test files).