-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv2structs.go
74 lines (71 loc) · 1.88 KB
/
csv2structs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Package csv2structs parses CSV data into a slice of structs.
//
// # Example Usage
//
// package main
//
// import (
// "fmt"
// "strings"
//
// "github.com/digitalocean-labs/csv2structs"
// )
//
// func main() {
// csvData := `first_name,age
// Alice,30
// Bob,25
// Charlie,35`
//
// type Person struct {
// FirstName string
// Age int
// }
//
// r := strings.NewReader(csvData)
// people, err := csv2structs.Parse[Person](r)
// if err != nil {
// fmt.Println("error:", err)
// return
// }
//
// for _, p := range people {
// fmt.Printf("%+v\n", p)
// }
// }
//
// # Headers
//
// All exported fields in the struct passed must match the headers in the CSV data.
//
// By default, the headers in the CSV data are transformed from snake_case to TitleCase.
//
// If you want to disable the header transformation, you can use the WithHeaderType option with the HeaderTypeNone value:
//
// people, err := csv2structs.Parse[Person](r, csv2structs.WithHeaderType(csv2structs.HeaderTypeNone))
//
// If your CSV data has headers in a different format, you can implement your own
// header transformation function and pass it to the WithHeaderTransform option:
//
// func customHeaderTransform(header string) string {
// // your custom header transformation logic
// }
//
// people, err := csv2structs.Parse[Person](r, csv2structs.WithHeaderTransform(customHeaderTransform))
//
// Or, if your CSV data has headers in snake_case format and you want to be explicit,
// you can use the WithHeaderType option with the HeaderTypeSnake value:
//
// people, err := csv2structs.Parse[Person](r, csv2structs.WithHeaderType(csv2structs.HeaderTypeSnake))
package csv2structs
import (
"io"
)
// Parse parses a CSV and returns a slice of structs
func Parse[T any](reader io.Reader, opts ...Option) ([]*T, error) {
p, err := NewParser[T](reader, opts...)
if err != nil {
return nil, err
}
return p.ReadAll()
}