Documentation
¶
Overview ¶
Package csvcoder decodes Go types from CSV records using struct tags.
The user should typically define a structure to hold the parsed value of a CSV record.
Index ¶
- func ParseCell(ctx *CellContext, value string, dst interface{}) error
- func ParseRow(row *Row, destination interface{}) error
- func RegisterRowStruct(t reflect.Type)
- func RegisterTextCoder(t reflect.Type, encoder, decoder interface{})
- func SafeRegisterRowStruct(t reflect.Type) error
- func SafeRegisterTextCoder(t reflect.Type, encoder, decoder interface{}) error
- type CellContext
- type ColumnNumber
- type FileParser
- type Header
- type Row
- type RowNumber
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ParseCell ¶
func ParseCell(ctx *CellContext, value string, dst interface{}) error
ParseCell parses a single CSV cell's textual value into dst.
func ParseRow ¶
ParseRow returns an error if the row fails to parse.
If the destination object has a `ParseCSVRow(*Row) error` method, that method will be called on the destination object.
func RegisterRowStruct ¶
RegisterRowStruct registers a struct type T that can be encoded as a CSV row.
Each public field of the struct definition will be examined and treated as follows:
1. If the field has a `csv-skip` tag, it will not be parsed.
2. If the field has a `csv` tag, that tag will be treated as the name of the CSV column for the field. If the tag is absent, the column name used will be the name of the field.
3. Let FT be the Go type of the field. If there is a registered decoder for *FT, that decoder will be used to decode the string value of the field with the name from step 2 into the field of a row being parsed. If there is no registered decoder for *FT, RegisterRowStruct will panic and SafeRegisterRowStruct returns an error.
Example ¶
type Pet struct {
Name string `csv:"pet-name"`
Internal string `csv-skip:""`
WeightPounds float64 `csv:"weight-lb"`
}
RegisterRowStruct(reflect.TypeOf(&Pet{}))
henry := &Pet{}
err := ParseRow(NewRow(
[]string{"ignored", "Henry", "32.15"},
NewHeader([]string{"id", "pet-name", "weight-lb"}),
1,
"example.csv"),
henry)
if err != nil {
fmt.Printf("error: %v", err)
return
}
fmt.Printf("%q weighs %d pounds", henry.Name, int(henry.WeightPounds))
Output: "Henry" weighs 32 pounds
func RegisterTextCoder ¶
RegisterTextCoder registers an encoding function and a decoding function for parsing and printing values of the provided type.
If the provided type is T, the decoder should be of the form func(string, *T) error, and the encoder should be of the form func(T) (string, error).
Example ¶
func SafeRegisterRowStruct ¶
SafeRegisterRowStruct calls RegisterRowStruct but returns an error instead of panicking if there are any issues.
func SafeRegisterTextCoder ¶
SafeRegisterTextCoder is like RegisterTextCoder but returns an error instead of panicking if an error is encountered.
Types ¶
type CellContext ¶
type CellContext struct {
// contains filtered or unexported fields
}
CellContext is passed to the function parsing a single CSV cell.
func NewCellContext ¶
func NewCellContext(rctx *Row) *CellContext
NewCellContext returns a new CellContext to use when parsing within a given Row.
func (*CellContext) Row ¶
func (c *CellContext) Row() *Row
Row returns the row that is being parsed.
type ColumnNumber ¶
type ColumnNumber int
ColumnNumber is used instead of an int for column indexes.
const InvalidColumn ColumnNumber = -1
InvalidColumn returns an invalid column number.
func (ColumnNumber) IsValid ¶
func (n ColumnNumber) IsValid() bool
IsValid returns whether the column refers to a valid column in the CSV file or or not.
func (ColumnNumber) Offset ¶
func (n ColumnNumber) Offset() int
Offset returns the offset of the value within a row.
type FileParser ¶
type FileParser struct {
// contains filtered or unexported fields
}
FileParser is
func NewFileParser ¶
func NewFileParser(r *csv.Reader, path string, recordPrototype interface{}) (*FileParser, error)
NewFileParser returns an object for parsing a set of records from a file.
The input CSV reader will be used to read all rows. The path argument is only for error reporting purposes.
The type of the recordPrototype should have been registered with a call to RegisterRowStruct.
func (*FileParser) Read ¶
func (fp *FileParser) Read() (interface{}, error)
Read parses the next record in the CSV. The header is parsed automatically.
func (*FileParser) ReadAll ¶
func (fp *FileParser) ReadAll(callback func(interface{}) error) error
ReadAll calls Read() until the end of the file and calls cb for each value.
type Header ¶
type Header struct {
// contains filtered or unexported fields
}
Header contains the values of the first row of the CSV file.
func (*Header) ColumnIndex ¶
func (h *Header) ColumnIndex(col string) ColumnNumber
ColumnIndex returns the index of the column with the given name.
func (*Header) ColumnNames ¶
ColumnNames returns the names of the columns.
type Row ¶
type Row struct {
// contains filtered or unexported fields
}
Row is passed to Unmarshal
func (*Row) Path ¶
Path returns the path of the CSV file or empty if this information is not available.
func (*Row) PositionString ¶
PositionString returns a human readable representation of the row position.
type RowNumber ¶
type RowNumber int
RowNumber is used instead of an int for representing the position of a row in a CSV file.
const InvalidRow RowNumber = -1
InvalidRow returns an invalid row number.
func (RowNumber) IsValid ¶
IsValid returns whether the row refers to a valid row in the CSV file or or not.