https://github.com/huboh/records
A light weight Go package that marshals & unmarshals CSV records(slice of slice of strings) to/from CSV entries(slice of structs).
https://github.com/huboh/records
csv csv-mapper go go-package marshaller
Last synced: 2 months ago
JSON representation
A light weight Go package that marshals & unmarshals CSV records(slice of slice of strings) to/from CSV entries(slice of structs).
- Host: GitHub
- URL: https://github.com/huboh/records
- Owner: huboh
- License: mit
- Created: 2022-07-13T11:55:49.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-07-18T11:11:27.000Z (over 3 years ago)
- Last Synced: 2024-06-21T09:56:00.894Z (almost 2 years ago)
- Topics: csv, csv-mapper, go, go-package, marshaller
- Language: Go
- Homepage:
- Size: 19.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Records
## A light weight & fast Go data marshaler
Records is a light weight Go package that marshals & unmarshals CSV records`(slice of slice of strings)` to/from CSV entries`(slice of structs)`.
Go provides the `csv.NewReader` & `csv.NewWriter` functions to read a CSV file into a *slice of slice of strings* & to write a *slice of slice of strings* out to a CSV file, without a way to map that data to the fields in a struct. this package add that missing functionality.
## Installation
Once Go is installed, run the following command to get `Records`.
```bash
go get github.com/huboh/records
```
## Usage
### Unmarshaling CSV records
import Records
```go
package main
import (
"github.com/huboh/records"
)
```
get csv records from a data source, most likely from a file
```go
r, err := os.Open("") // import "os"
csvReader := csv.NewReader(r) // import "encoding/csv"
csvRecords, err := csvReader.ReadAll() // read csv records
```
unmarshal csv records. note that unexported fields or fields without csv struct tags are ignored.
```go
// create a struct that represent your csv data.
type CsvFileEntries struct {
Age int `csv:"age"`
Name string `csv:"name"`
IsEmployee bool `csv:"isEmployee"`
}
entries := []CsvFileEntries{} // initialize variable to hold csv records
err := records.Unmarshal(csvRecords, &entries)
if err != nil && errors.Is(err, records.ErrUnSupportedKind) {
// encountered fields with unsupported types
// supported types are: all int, uint & float types, bool, string.
}
// success.. entries => []CsvFileEntries{{...}}
```
### Marshaling CSV entries
import Records
```go
package main
import (
"github.com/huboh/records"
)
```
transform csv data to csv records. note that fields without csv struct tags are ignored.
```go
// create a struct that represent your csv data.
type CsvFileEntries struct {
Age int `csv:"age"`
Name string `csv:"name"`
IsEmployee bool `csv:"isEmployee"`
}
entries := []CsvFileEntries{...}
csvRecords, err := records.Marshal(entries) // transform csv data to csv records
if err != nil && error.Is(err, records.ErrUnSupportedKind) {
// encountered fields with unsupported types
// supported types are: all int, uint & float types, bool, string
}
// success.. csvRecords => [][]string{{...}}
```
after successfully marshalling your csv data to records, writing the csv records to a file is as easy as:
```go
w, e := os.Create("")
csvWriter := csv.NewWriter(w)
csvWriterErr := csvWriter.WriteAll(csvRecords) // writes csv records to file
```
## Contributions
Contributions are welcome to this project to further improve it to suit the public need. I hope you enjoy the simplicity of this package.
## License
This package is provided under MIT license.