Documentation ¶
Overview ¶
Package csvdec provides a generic CSV decoder. Wraps the encoding/csv package with a decoder that can populate structs.
Accepted Types ¶
The T type parameter for this package's functions accepts structs. Field may be of types bool, int*, uint*, float* or string for automatic parsing. For manual parsing with a method, any type is allowed. Unexported fields are ignored.
Default Behavior ¶
File and Reader match column to field according to their order. For example:
type a struct { Name string // Matches first column Age int // Matches second column Height float64 // Matches third column }
FileHeader and ReaderHeader match column to field according to the first line and the field's name, case insensitively. For example:
type a struct { Name string // Matches a column titled name, Name, nAmE, etc. Age int // Matches a column titled age, Age, aGe, etc. Height float64 // Matches a column titled height, Height, hEiGhT, etc. }
Note that the Header functions use the first line as metadata, while the other two functions expect data starting from the first line. In all functions yielding continues upon parsing errors, so that a caller may choose to skip lines.
Field Tags ¶
Field tags can be used to change the default behavior. The format for a field tag is as follows:
Field int `csvdec:"column,modifier1,modifier2..."`
The column part may be:
- empty: use the default behavior
- column name or index: associate this field with the column with this name or at this 0-based index, case sensitively
- "-": a single hyphen, ignore this field entirely
Modifiers may be:
- "allowempty": the input value may be empty, in which case no parsing will be attempted
- "optional": don't err if the column for this field is missing
- exported method name: use T's method with this name to parse the input value
A custom parsing method will replace the default parsing. The method's signature must take a string as input, and return the field's type and an error.
Index ¶
- func File[T any](file string, fn func(*csv.Reader)) iter.Seq2[T, error]
- func FileHeader[T any](file string, fn func(*csv.Reader)) iter.Seq2[T, error]
- func Reader[T any](r io.Reader, fn func(*csv.Reader)) iter.Seq2[T, error]
- func ReaderHeader[T any](r io.Reader, fn func(*csv.Reader)) iter.Seq2[T, error]
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func File ¶ added in v1.0.0
File returns an iterator over parsed instances of T, using column numbers for matching columns to fields.
fn is an optional function for modifying the CSV parser, for example for changing the delimiter.
func FileHeader ¶ added in v1.0.0
FileHeader returns an iterator over parsed instances of T, using the first line for matching columns to fields.
fn is an optional function for modifying the CSV parser, for example for changing the delimiter.
func Reader ¶ added in v1.0.0
Reader returns an iterator over parsed instances of T, using column numbers for matching columns to fields.
fn is an optional function for modifying the CSV parser, for example for changing the delimiter.
func ReaderHeader ¶ added in v1.0.0
ReaderHeader returns an iterator over parsed instances of T, using the first line for matching columns to fields.
fn is an optional function for modifying the CSV parser, for example for changing the delimiter.
Types ¶
This section is empty.