Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CanonicalHeaderKey ¶
CanonicalHeaderKey returns the canonical format of the header key. For example, the canonical key for " User\t Id " is "user id".
Types ¶
type CellError ¶
type CellError struct { // The key used to get the cell value in the row. Key string // The value obtained from the cell by the given key in the row. // The value is empty string, if the cell cannot be found by the key. Val string // The row number of cell in the csv file. // // Numbering of rows starts at 1; Row int // The column number of cell in the csv file. // // Numbering of column starts at 1; // The column is -1, if the cell cannot be found by the key. Column int // The line number of cell in csv file. // // Numbering of lines starts at 1; // A row can span multiple lines. // The line is -1, if the cell cannot be found by the key. Line int // ParseErr occurs when parsing cell value. ParseErr error }
CellError uses context to wrap cell value parse error.
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
Decoder reads rows from a CSV-encoded file.
Example ¶
package main import ( "bytes" "encoding/csv" "errors" "fmt" "io" "strconv" "strings" "github.com/go-camp/csvd" ) func main() { type User struct { Name string Age int8 } var csvFile = "User \tName , Age\ninvalid int,a\ngood age , 18\ntoo yong,17\ntoo old,121\n" r := bytes.NewReader([]byte(csvFile)) decoder, err := csvd.NewDecoder(csvd.Options{ Reader: csv.NewReader(r), }) if err != nil { panic(err) } decoder.ParseHeader() for { var user User row, err := decoder.Next() if err != nil { if err == io.EOF { break } panic(err) } row.Parse("user name", func(val string) error { val = strings.TrimSpace(val) user.Name = val return nil }) row.Parse("age", func(val string) error { val = strings.TrimSpace(val) age, err := strconv.ParseInt(val, 10, 8) if err != nil { return err } if age < 18 { return errors.New("too yong") } if age > 120 { return errors.New("too old") } user.Age = int8(age) return nil }) if err := row.Error().Err(); err != nil { fmt.Println(err) } } }
Output: 1 cell error(s) found. - row: 2, column: 2, key: "age", val: "a", err: strconv.ParseInt: parsing "a": invalid syntax. 1 cell error(s) found. - row: 4, column: 2, key: "age", val: "17", err: too yong. 1 cell error(s) found. - row: 5, column: 2, key: "age", val: "121", err: too old.
func NewDecoder ¶
NewDecoder returns a new Decoder that reads from opts.Reader.
func (*Decoder) Next ¶
Next reads one row of csv file.
If there is no row left to be read, Next returns nil, io.EOF.
func (*Decoder) ParseHeader ¶
ParseHeader parses a row of csv file into header.
type Header ¶
Header represents the key-index pairs in the csv file header. The keys should be in canonical form, as returned by CanonicalHeaderKey.
func (Header) Del ¶
Del deletes the index associated with the given key.
The key is canonicalized by CanonicalHeaderKey.
func (Header) Get ¶
Get gets the index associated with the given key. If there are no index associated with the key, Get returns -1.
The key is canonicalized by CanonicalHeaderKey.
type Row ¶
type Row struct {
// contains filtered or unexported fields
}
Row represents a row in csv file.
func (*Row) Get ¶
Get gets the value of cell associated with the given key. If there are no cell associated with the key, Get returns empty string.
The key is canonicalized by CanonicalHeaderKey.
func (*Row) Has ¶
Has checks if has a cell associated with the given key. If there are no cell associated with the key, Has returns false.
The key is canonicalized by CanonicalHeaderKey.
func (*Row) LastParseError ¶
LastParseError returns the error that occurred the last time the Parse method was called.
func (*Row) Parse ¶
Parse gets the value of cell associated with the given key then call parse method with that value.
The error returned by the parse method is wrapped as a CellError and can be obtained through the LastParseError method.
If you want to get all parse errors, call the Error() method.