csvdec

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 18, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package csvdec provides a generic CSV decoder. Wraps the encoding/csv package with a decoder that can populate structs and slices.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Iter

func Iter[T any](r *Reader) iter.Seq2[T, error]

Iter iterates over the remaining entries, yielding instances of T.

func IterFile added in v0.6.0

func IterFile[T any](file string, readerFunc func(*Reader),
) iter.Seq2[T, error]

IterFile iterates over the contents of the given file, yielding an instance of T for each row. If readerFunc is non-nil, applies it to the reader before going over the file (for changing delimiter, etc.).

Types

type Reader

type Reader struct {
	*csv.Reader
	SkipCols uint // How many columns to skip from the beginning of each line.
}

A Reader reads CSV lines and converts them to data objects. Embeds a csv.Reader, so it can be used the same way.

func New

func New(r io.Reader) *Reader

New returns a new reader that reads from r. skipRows and skipCols indicate how many of the first rows and columns should be ignored.

func (*Reader) ReadInto

func (d *Reader) ReadInto(a interface{}) error

ReadInto reads the next CSV line and populates the given object with parsed values. Accepted input types are struct pointers and slice pointers, as explained below.

Struct pointer: all fields must be exported and of type int*, uint* float*, string or bool. Fields will be populated by order of appearance. Too few values in the CSV line will result in an error. Excess values in the CSV line will be ignored. The struct's last field may be a slice, in which case all the remaining values will be parsed for that slice's type, according to the restrictions below.

Slice pointer of type int*, uint*, float*, string: the pointer will be populated with a slice of parsed values, according to the length of the CSV line.

Any other type will cause a panic.

func (*Reader) SkipRow

func (d *Reader) SkipRow() error

SkipRow skips a row and returns an error if reading failed.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL