Documentation
¶
Overview ¶
Package csvamp - Go package for reading CSV directly into structs
Example:
type MyStruct struct {
Line int `csv:"[line]"` // optionally capture the csv line number
Raw []string `csv:"[raw]"` // optionally capture the csv record
Foo string `csv:"[1]"` // map to indexed csv field (1 based)
Bar string `csv:"bar"` // map to named csv field
}
// create the mapper...
m, err := csvamp.NewMapper[MyStruct]()
if err != nil {
log.Fatal(err)
}
const sample = `foo,bar
Aaa,Bbb
Ccc,Ddd`
result, err := m.Reader(strings.NewReader(sample), nil).ReadAll()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", result)
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CsvQuotedUnmarshaler ¶ added in v1.2.0
type CsvQuotedUnmarshaler interface {
UnmarshalQuotedCSV(val string, quoted bool, record []string) error
}
CsvQuotedUnmarshaler is the interface implemented by an object that can unmarshal a string CSV field representation of itself.
CsvQuotedUnmarshaler is similar to CsvUnmarshaler, except that it informs whether the value string (which may be empty) was quoted
type CsvUnmarshaler ¶
CsvUnmarshaler is the interface implemented by an object that can unmarshal a string CSV field representation of itself.
This is effectively the same as encoding.TextUnmarshaler, but provides the field as a string along with the entire record
type DefaultEmptyValues ¶ added in v1.2.0
type DefaultEmptyValues bool
DefaultEmptyValues is an option that can be passed to NewMapper / MustNewMapper
if set to true, when reading, empty fields in the CSV are treated as zero values for types bool, int, uint and float
By default, reading empty CSV fields into bool, int, uint & float will cause an error
type ErrorHandler ¶ added in v1.1.0
type ErrorHandler interface {
// Handle handles the error - if it returns the error, then further processing stops
Handle(err error, line int) error
}
ErrorHandler is an interface that can be used with ReaderContext.WithErrorHandler
type IgnoreUnknownFieldNames ¶
type IgnoreUnknownFieldNames bool
IgnoreUnknownFieldNames is an option that can be passed to NewMapper / MustNewMapper
if set to true, the mapper will ignore references to unknown CSV field (header) names
By default, if a struct field references (via tag) an unknown CSV field (header) name - it will error when reading
type Mapper ¶
type Mapper[T any] interface { // Reader returns a reader context for the mapper using the provided io.Reader // // the postProcessor func, if provided, can be used to validate (or modify) the struct after it has been read // // the options can be any of csv.Comma, csv.Comment, csv.FieldsPerRecord, csv.LazyQuotes, csv.TrimLeadingSpace or csv.NoHeader Reader(r io.Reader, postProcessor func(row *T) error, options ...any) ReaderContext[T] // ReaderContext returns a reader context for the mapper using the provided csv.Reader // // the postProcessor func, if provided, can be used to validate (or modify) the struct after it has been read ReaderContext(r *csv.Reader, postProcessor func(row *T) error) ReaderContext[T] // Adapt creates a new Mapper from this mapper with struct field to CSV fields overridden // // Options from the original mapper are preserved unless overridden by the provided options Adapt(clear bool, mappings OverrideMappings, options ...any) (Mapper[T], error) // Mappings returns the current effective struct field to CSV field mappings Mappings() OverrideMappings }
Mapper is an interface for mapping structs onto CSV
Use NewMapper / MustNewMapper to create a new Mapper
func MustNewMapper ¶
MustNewMapper is the same as NewMapper - except that it panics in case of error
type OverrideMapping ¶
type OverrideMapping struct {
// FieldName is the name of the field in the original struct
FieldName string
// CsvFieldIndex is the field index in the CSV
//
// If this value is 0 (zero), the index is ignored and CsvFieldName is used instead
//
// If this value is negative, the index mapping is removed
CsvFieldIndex int
// CsvFieldName is the field name (header) in the CSV
//
// If this value is empty, the name is ignored and CsvFieldIndex is used instead
//
// If this value starts with a "-", the named mapping is removed
CsvFieldName string
}
type OverrideMappings ¶
type OverrideMappings []OverrideMapping
OverrideMappings is passed to Mapper.Adapt to override field to CSV mappings
type ReaderContext ¶
type ReaderContext[T any] interface { // Read reads the next CSV line as a struct or returns error io.EOF Read() (T, error) // ReadAll reads all CSV lines as structs ReadAll() ([]T, error) // Iterate iterates over CSV lines and calls the provided function with the read struct // // Iteration continues until the end of the CSV or when the provided function returns false or an error Iterate(fn func(T) (bool, error)) error // Iterator returns an iterator that can be ranged over // // range iteration stops when an error is encountered - unless an ErrorHandler is provided using WithErrorHandler Iterator() func(func(int, T) bool) // WithErrorHandler sets the error handler - which can be used to track errors during ReadAll and Iterate // // Setting an error handler means that errors are reported but don't necessarily halt further reading WithErrorHandler(eh ErrorHandler) ReaderContext[T] // SupplyHeaders enables CSV headers to be manually supplied // // Sometimes your csv may not have headers, or you may have already read (and normalised) them SupplyHeaders(headers []string) ReaderContext[T] }
ReaderContext is the interface used to actually read structs from CSV
A reader context is obtained from Mapper.Reader or Mapper.ReaderContext
type ReaderError ¶ added in v1.2.0
ReaderError is the wrapped error returned from ReaderContext.ReadAll / ReaderContext.Iterate
func (*ReaderError) Error ¶ added in v1.2.0
func (e *ReaderError) Error() string
func (*ReaderError) Unwrap ¶ added in v1.2.0
func (e *ReaderError) Unwrap() error
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
_examples
|
|
|
readme/example_1
command
|
|
|
readme/example_10
command
|
|
|
readme/example_11
command
|
|
|
readme/example_12
command
|
|
|
readme/example_13
command
|
|
|
readme/example_14
command
|
|
|
readme/example_15
command
|
|
|
readme/example_16
command
|
|
|
readme/example_17
command
|
|
|
readme/example_18
command
|
|
|
readme/example_19
command
|
|
|
readme/example_2
command
|
|
|
readme/example_20
command
|
|
|
readme/example_21
command
|
|
|
readme/example_22
command
|
|
|
readme/example_3
command
|
|
|
readme/example_4
command
|
|
|
readme/example_5
command
|
|
|
readme/example_6
command
|
|
|
readme/example_7
command
|
|
|
readme/example_8
command
|
|
|
readme/example_9
command
|
|
|
Package csv - Go package to replace stdlib encoding/csv reader - with more exposed info
|
Package csv - Go package to replace stdlib encoding/csv reader - with more exposed info |