Skip to content

Commit ecf1fa8

Browse files
why2gowillsun
authored andcommitted
v0.0.1
1 parent 038686f commit ecf1fa8

File tree

8 files changed

+911
-2
lines changed

8 files changed

+911
-2
lines changed

README.md

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,78 @@
1-
# csv_reader
2-
read csv file and unmarshal data to golang structs
1+
# csv_parser
2+
Csv parser can read from csv file and then parse record to golang structs.
3+
4+
5+
6+
## Getting start
7+
8+
### Installation
9+
10+
```
11+
go get github.com/why2go/csv_parser
12+
```
13+
14+
### Supported field types
15+
16+
1. primitive types: bool, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, string.
17+
18+
2. pointer of primitive types.
19+
20+
3. slice: whose element's type is primitive type or pointer of primitive type.
21+
22+
4. map: whose key's type is string and value's type is primitive type or pointer of primitive type.
23+
24+
### Example
25+
26+
```go
27+
package main
28+
29+
import (
30+
"bytes"
31+
"context"
32+
"encoding/csv"
33+
"encoding/json"
34+
"fmt"
35+
36+
"github.com/why2go/csv_parser"
37+
)
38+
39+
func main() {
40+
data := `name,{{attri:age}},{{attri:height}},[[msg]],[[msg]]
41+
Alice,20,,"Hi, I'm Alice.",Nice to meet you!
42+
Bob,21,175,"Hi, I'm Bob.",Nice to meet you!
43+
Candy,,189,"Hi, I'm Candy.",Nice to meet you!
44+
David,23,172,"Hi, I'm David.",Nice to meet you!
45+
`
46+
type Demo struct {
47+
Name string `csv:"name,required" json:"name"`
48+
Attri map[string]*int16 `csv:"attri" json:"attri"`
49+
Msg []string `csv:"msg" json:"msg"`
50+
}
51+
52+
r := csv.NewReader(bytes.NewBufferString(data))
53+
parser, err := csv_parser.NewCsvParser[Demo](r) // create a csv parser
54+
if err != nil {
55+
panic(err)
56+
}
57+
defer parser.Close() // close the parser
58+
59+
// get parsed data from channel
60+
for dataWrapper := range parser.DataChan(context.Background()) {
61+
if err != nil {
62+
panic(err)
63+
}
64+
b, err := json.Marshal(dataWrapper.Data)
65+
if err != nil {
66+
panic(err)
67+
}
68+
fmt.Printf("demo: %s\n", string(b))
69+
}
70+
71+
fmt.Printf("done\n")
72+
}
73+
```
74+
75+
76+
77+
78+

doc.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Copyright 2023 Sun Zhi. All rights reserved.
2+
// Use of this source code is governed by a MIT style
3+
// license that can be found in the LICENSE file.
4+
5+
// The csv parser is used to parse a csv file into go structs.
6+
7+
package csv_parser

go.mod

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module github.com/why2go/csv_parser
2+
3+
go 1.21.5
4+
5+
require github.com/stretchr/testify v1.8.4
6+
7+
require (
8+
github.com/davecgh/go-spew v1.1.1 // indirect
9+
github.com/pmezard/go-difflib v1.0.0 // indirect
10+
gopkg.in/yaml.v3 v3.0.1 // indirect
11+
)

go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5+
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
6+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
7+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
8+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
9+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
10+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)