xcsv

package module
v0.0.0-...-49de417 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2024 License: ISC Imports: 10 Imported by: 1

README

xcsv

Documentation

CSV marshaling and unmarshaling library.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ColumnNames

func ColumnNames[T any]() []string

ColumnNames returns the names of the fields in a struct. It can be used with Marshal to write a CSV file with a column header row. If a field has a `csv` tag, its value will be used instead of the field name. Note that this does not support `csv:"-"`.

func FieldsValue

func FieldsValue(v any) ([]string, error)

FieldsValue returns the values of the fields in a struct as a slice of strings (a CSV row).

func Marshal

func Marshal[T any](w *csv.Writer, values iter.Seq[T]) error

Marshal writes the CSV representation of values to w.

func MarshalFile

func MarshalFile[T any](filepath string, values iter.Seq[T]) error

MarshalFile writes the CSV representation of values to filepath.

func Unmarshal

func Unmarshal[T any](r *csv.Reader, opts ...UnmarshalOpt) iter.Seq2[T, error]

Unmarshal reads the CSV file from r and unmarshals it into v.

func UnmarshalFile

func UnmarshalFile[T any](filepath string, opts ...UnmarshalOpt) (iter.Seq2[T, error], error)

UnmarshalFile reads the CSV file from filepath and unmarshals it into v.

Types

type RecordUnmarshalingError

type RecordUnmarshalingError struct {
	Record []string `json:"record"`
	Line   int      `json:"line"`
	// contains filtered or unexported fields
}

RecordUnmarshalingError is an error that occurs when unmarshaling a single record. It contains thee record itself and the error that occurred.

func (*RecordUnmarshalingError) Error

func (e *RecordUnmarshalingError) Error() string

Error returns the error message.

func (*RecordUnmarshalingError) Unwrap

func (e *RecordUnmarshalingError) Unwrap() error

Unwrap returns the underlying error.

type UnmarshalOpt

type UnmarshalOpt func(o *unmarshalOpts)

UnmarshalOpt is a function that modifies the behavior of Unmarshal.

func AllowMissingFields

func AllowMissingFields() UnmarshalOpt

AllowMissingFields allows the CSV file to potentially have fewer columns than the struct. Columns that are missing will be set to their zero value.

Note that Unmarshal will always allow the CSV file to have *more* columns than the struct regardless if this option is used.

func ErrorEarly

func ErrorEarly() UnmarshalOpt

ErrorEarly stops unmarshalling as soon as an error is encountered.

func SkipHeader

func SkipHeader() UnmarshalOpt

SkipHeader skips the first row of the CSV file if it matches the column names of the struct.

Jump to

Keyboard shortcuts

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