csvlib

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2024 License: MIT Imports: 15 Imported by: 2

README

Go Version GoDoc Build Status Coverage Status GoReport

High level CSV library for Go 1.18+

This is a library for decoding and encoding CSV at high level as it provides convenient methods and many configuration options to process the data the way you expect.

This library is inspired by the project https://github.com/jszwec/csvutil.

Functionalities

Decoding

  • Decode CSV data into Go struct
  • Support Go interface encoding.TextUnmarshaler (with function UnmarshalText)
  • Support custom interface CSVUnmarshaler (with function UnmarshalCSV)
  • Ability to continue decoding when error occurs (collect all errors at once)
  • Ability to perform custom preprocessor functions on cell data before decoding
  • Ability to perform custom validator functions on cell data after decoding
  • Ability to decode dynamic columns into Go struct field (inline columns)
  • Support rendering the result errors into human-readable content (row-by-row text and CSV)
  • Support localization to render the result errors into a specific language

Encoding

  • Encode Go struct into CSV data
  • Support Go interface encoding.TextMarshaler (with function MarshalText)
  • Support custom interface CSVMarshaler (with function MarshalCSV)
  • Ability to perform custom postprocessor functions on cell data after encoding
  • Ability to encode dynamic columns defined via inner Go struct (inline columns)
  • Ability to localize the header into a specific language

Installation

go get github.com/tiendc/go-csvlib

Usage

Benchmarks

csvlib vs csvutil vs gocsv vs easycsv

Benchmark code

Unmarshal
BenchmarkUnmarshal/csvlib.Unmarshal/100_records
BenchmarkUnmarshal/csvlib.Unmarshal/100_records-10       	   21572	     55520 ns/op
BenchmarkUnmarshal/csvlib.Unmarshal/1000_records
BenchmarkUnmarshal/csvlib.Unmarshal/1000_records-10      	    2641	    455794 ns/op
BenchmarkUnmarshal/csvlib.Unmarshal/10000_records
BenchmarkUnmarshal/csvlib.Unmarshal/10000_records-10     	     253	   4716323 ns/op
BenchmarkUnmarshal/csvlib.Unmarshal/100000_records
BenchmarkUnmarshal/csvlib.Unmarshal/100000_records-10    	      26	  44519502 ns/op

BenchmarkUnmarshal/csvutil.Unmarshal/100_records
BenchmarkUnmarshal/csvutil.Unmarshal/100_records-10      	   27848	     42927 ns/op
BenchmarkUnmarshal/csvutil.Unmarshal/1000_records
BenchmarkUnmarshal/csvutil.Unmarshal/1000_records-10     	    2952	    405309 ns/op
BenchmarkUnmarshal/csvutil.Unmarshal/10000_records
BenchmarkUnmarshal/csvutil.Unmarshal/10000_records-10    	     296	   4059881 ns/op
BenchmarkUnmarshal/csvutil.Unmarshal/100000_records
BenchmarkUnmarshal/csvutil.Unmarshal/100000_records-10   	      28	  40531973 ns/op

BenchmarkUnmarshal/gocsv.Unmarshal/100_records
BenchmarkUnmarshal/gocsv.Unmarshal/100_records-10        	    9830	    118919 ns/op
BenchmarkUnmarshal/gocsv.Unmarshal/1000_records
BenchmarkUnmarshal/gocsv.Unmarshal/1000_records-10       	    1022	   1164278 ns/op
BenchmarkUnmarshal/gocsv.Unmarshal/10000_records
BenchmarkUnmarshal/gocsv.Unmarshal/10000_records-10      	      86	  12609154 ns/op
BenchmarkUnmarshal/gocsv.Unmarshal/100000_records
BenchmarkUnmarshal/gocsv.Unmarshal/100000_records-10     	       9	 119912333 ns/op

BenchmarkUnmarshal/easycsv.ReadAll/100_records
BenchmarkUnmarshal/easycsv.ReadAll/100_records-10        	    3831	    315302 ns/op
BenchmarkUnmarshal/easycsv.ReadAll/1000_records
BenchmarkUnmarshal/easycsv.ReadAll/1000_records-10       	     384	   3083931 ns/op
BenchmarkUnmarshal/easycsv.ReadAll/10000_records
BenchmarkUnmarshal/easycsv.ReadAll/10000_records-10      	      34	  31440493 ns/op
BenchmarkUnmarshal/easycsv.ReadAll/100000_records
BenchmarkUnmarshal/easycsv.ReadAll/100000_records-10     	       4	 324321531 ns/op
Marshal
BenchmarkMarshal/csvlib.Marshal/100_records
BenchmarkMarshal/csvlib.Marshal/100_records-10         	   19753	     58890 ns/op
BenchmarkMarshal/csvlib.Marshal/1000_records
BenchmarkMarshal/csvlib.Marshal/1000_records-10        	    2149	    554537 ns/op
BenchmarkMarshal/csvlib.Marshal/10000_records
BenchmarkMarshal/csvlib.Marshal/10000_records-10       	     214	   5575920 ns/op
BenchmarkMarshal/csvlib.Marshal/100000_records
BenchmarkMarshal/csvlib.Marshal/100000_records-10      	      19	  55735281 ns/op

BenchmarkMarshal/csvutil.Marshal/100_records
BenchmarkMarshal/csvutil.Marshal/100_records-10        	   24388	     48931 ns/op
BenchmarkMarshal/csvutil.Marshal/1000_records
BenchmarkMarshal/csvutil.Marshal/1000_records-10       	    2557	    467704 ns/op
BenchmarkMarshal/csvutil.Marshal/10000_records
BenchmarkMarshal/csvutil.Marshal/10000_records-10      	     256	   4720885 ns/op
BenchmarkMarshal/csvutil.Marshal/100000_records
BenchmarkMarshal/csvutil.Marshal/100000_records-10     	      22	  48627754 ns/op

BenchmarkMarshal/gocsv.Marshal/100_records
BenchmarkMarshal/gocsv.Marshal/100_records-10          	   13254	     90873 ns/op
BenchmarkMarshal/gocsv.Marshal/1000_records
BenchmarkMarshal/gocsv.Marshal/1000_records-10         	    1294	    898938 ns/op
BenchmarkMarshal/gocsv.Marshal/10000_records
BenchmarkMarshal/gocsv.Marshal/10000_records-10        	     132	   9017481 ns/op
BenchmarkMarshal/gocsv.Marshal/100000_records
BenchmarkMarshal/gocsv.Marshal/100000_records-10       	      12	  90260420 ns/op

Contributing

  • You are welcome to make pull requests for new functions and bug fixes.

Authors

License

Documentation

Index

Constants

View Source
const (
	DefaultTagName = "csv"
)

Variables

View Source
var (
	ErrTypeInvalid     = errors.New("ErrTypeInvalid")
	ErrTypeUnsupported = errors.New("ErrTypeUnsupported")
	ErrTypeUnmatched   = errors.New("ErrTypeUnmatched")
	ErrValueNil        = errors.New("ErrValueNil")
	ErrAlreadyFailed   = errors.New("ErrAlreadyFailed")
	ErrFinished        = errors.New("ErrFinished")
	ErrUnexpected      = errors.New("ErrUnexpected")

	ErrTagOptionInvalid    = errors.New("ErrTagOptionInvalid")
	ErrConfigOptionInvalid = errors.New("ErrConfigOptionInvalid")
	ErrLocalization        = errors.New("ErrLocalization")

	ErrHeaderColumnInvalid                      = errors.New("ErrHeaderColumnInvalid")
	ErrHeaderColumnUnrecognized                 = errors.New("ErrHeaderColumnUnrecognized")
	ErrHeaderColumnRequired                     = errors.New("ErrHeaderColumnRequired")
	ErrHeaderColumnDuplicated                   = errors.New("ErrHeaderColumnDuplicated")
	ErrHeaderColumnOrderInvalid                 = errors.New("ErrHeaderColumnOrderInvalid")
	ErrHeaderDynamicTypeInvalid                 = errors.New("ErrHeaderDynamicTypeInvalid")
	ErrHeaderDynamicNotAllowNoHeaderMode        = errors.New("ErrHeaderDynamicNotAllowNoHeaderMode")
	ErrHeaderDynamicRequireColumnOrder          = errors.New("ErrHeaderDynamicRequireColumnOrder")
	ErrHeaderDynamicNotAllowUnrecognizedColumns = errors.New("ErrHeaderDynamicNotAllowUnrecognizedColumns")
	ErrHeaderDynamicNotAllowLocalizedHeader     = errors.New("ErrHeaderDynamicNotAllowLocalizedHeader")

	ErrValidationConversion = errors.New("ErrValidationConversion")
	ErrValidation           = errors.New("ErrValidation")
	ErrValidationLT         = fmt.Errorf("%w: LT", ErrValidation)
	ErrValidationLTE        = fmt.Errorf("%w: LTE", ErrValidation)
	ErrValidationGT         = fmt.Errorf("%w: GT", ErrValidation)
	ErrValidationGTE        = fmt.Errorf("%w: GTE", ErrValidation)
	ErrValidationRange      = fmt.Errorf("%w: Range", ErrValidation)
	ErrValidationIN         = fmt.Errorf("%w: IN", ErrValidation)
	ErrValidationStrLen     = fmt.Errorf("%w: StrLen", ErrValidation)
	ErrValidationStrPrefix  = fmt.Errorf("%w: StrPrefix", ErrValidation)
	ErrValidationStrSuffix  = fmt.Errorf("%w: StrSuffix", ErrValidation)

	ErrDecodeValueType     = errors.New("ErrDecodeValueType")
	ErrDecodeRowFieldCount = errors.New("ErrDecodeRowFieldCount")
	ErrDecodeQuoteInvalid  = errors.New("ErrDecodeQuoteInvalid")

	ErrEncodeValueType = errors.New("ErrEncodeValueType")
)
View Source
var (
	// ProcessorTrim trims space of string
	ProcessorTrim = strings.TrimSpace
	// ProcessorTrimPrefix trims prefix from a string
	ProcessorTrimPrefix = strings.TrimPrefix
	// ProcessorTrimSuffix trims suffix from a string
	ProcessorTrimSuffix = strings.TrimSuffix
	// ProcessorReplace replaces a substring in a string
	ProcessorReplace = strings.Replace
	// ProcessorReplaceAll replaces all occurrences of a substring in a string
	ProcessorReplaceAll = strings.ReplaceAll
	// ProcessorLower converts a string to lowercase
	ProcessorLower = strings.ToLower
	// ProcessorUpper converts a string to uppercase
	ProcessorUpper = strings.ToUpper
	// ProcessorNumberGroup formats a number with grouping its digits
	ProcessorNumberGroup = gofn.NumberFmtGroup
	// ProcessorNumberUngroup ungroups number digits
	ProcessorNumberUngroup = gofn.NumberFmtUngroup
)

Functions

func GetHeader

func GetHeader(v any, tagName string) ([]string, error)

GetHeader get CSV header from the given struct

func Marshal

func Marshal(v any, options ...EncodeOption) ([]byte, error)

Marshal convenient method to encode a slice of structs into CSV format

func ProcessorNumberGroupComma

func ProcessorNumberGroupComma(s string) string

ProcessorNumberGroupComma formats a number with grouping its digits by comma

func ProcessorNumberUngroupComma

func ProcessorNumberUngroupComma(s string) string

ProcessorNumberUngroupComma ungroups number digits by comma

Types

type CSVMarshaler

type CSVMarshaler interface {
	MarshalCSV() ([]byte, error)
}

CSVMarshaler marshaler interface for encoding custom type

type CSVRenderConfig

type CSVRenderConfig struct {
	// CellSeparator separator to join cell error details within a row, normally a comma (`,`)
	CellSeparator string

	// LineBreak custom new line character (default is `\n`)
	LineBreak string

	// RenderHeader whether render header row or not
	RenderHeader bool

	// RenderRowNumberColumnIndex index of `row` column to render, set `-1` to not render it (default is `0`)
	RenderRowNumberColumnIndex int

	// RenderLineNumberColumnIndex index of `line` column to render, set `-1` to not render it (default is `-1`)
	RenderLineNumberColumnIndex int

	// RenderCommonErrorColumnIndex index of `common error` column to render, set `-1` to not render it
	// (default is `1`)
	RenderCommonErrorColumnIndex int

	// LocalizeCellFields localize cell's fields before rendering the cell error (default is `true`)
	LocalizeCellFields bool

	// LocalizeCellHeader localize cell header before rendering the cell error (default is `true`)
	LocalizeCellHeader bool

	// Params custom params user wants to send to the localization (optional)
	Params ParameterMap

	// LocalizationFunc function to translate message (optional)
	LocalizationFunc LocalizationFunc

	// HeaderRenderFunc custom render function for rendering header row (optional)
	HeaderRenderFunc func([]string, ParameterMap)

	// CellRenderFunc custom render function for rendering a cell error (optional).
	// The func can return ("", false) to skip rendering the cell error, return ("", true) to let the
	// renderer continue using its solution, and return ("<str>", true) to override the value.
	//
	// Supported params:
	//   {{.Column}}       - column index (0-based)
	//   {{.ColumnHeader}} - column name
	//   {{.Value}}        - cell value
	//   {{.Error}}        - error detail which is result of calling err.Error()
	//
	// Use cellErr.WithParam() to add more extra params
	CellRenderFunc func(*RowErrors, *CellError, ParameterMap) (string, bool)

	// CommonErrorRenderFunc renders common error (not RowErrors, CellError) (optional)
	CommonErrorRenderFunc func(error, ParameterMap) (string, error)
}

type CSVRenderer

type CSVRenderer struct {
	// contains filtered or unexported fields
}

CSVRenderer an implementation of error renderer which can produce messages for the input errors as CSV output data.

func NewCSVRenderer

func NewCSVRenderer(err *Errors, options ...func(*CSVRenderConfig)) (*CSVRenderer, error)

NewCSVRenderer creates a new CSVRenderer

func (*CSVRenderer) Render

func (r *CSVRenderer) Render() (data [][]string, transErr error, err error)

Render renders Errors object as CSV rows data

func (*CSVRenderer) RenderAsString

func (r *CSVRenderer) RenderAsString() (msg string, transErr error, err error)

RenderAsString renders the input as CSV string

func (*CSVRenderer) RenderTo

func (r *CSVRenderer) RenderTo(w Writer) (transErr error, err error)

RenderTo renders the input as CSV string and writes it to the writer

type CSVUnmarshaler

type CSVUnmarshaler interface {
	UnmarshalCSV([]byte) error
}

CSVUnmarshaler unmarshaler interface for decoding custom type

type CellError

type CellError struct {
	// contains filtered or unexported fields
}

CellError data structure of error of a cell

func NewCellError

func NewCellError(err error, column int, header string) *CellError

NewCellError creates a new CellError

func (*CellError) Column

func (e *CellError) Column() int

Column gets the column of the cell

func (*CellError) Error

func (e *CellError) Error() string

Error implements Go error interface

func (*CellError) HasError

func (e *CellError) HasError() bool

HasError checks if the error contains an error

func (*CellError) Header

func (e *CellError) Header() string

Header gets the header of the column

func (*CellError) Is

func (e *CellError) Is(err error) bool

Is checks if the inner error is kind of the specified error

func (*CellError) LocalizationKey

func (e *CellError) LocalizationKey() string

LocalizationKey gets localization key of error

func (*CellError) SetLocalizationKey

func (e *CellError) SetLocalizationKey(k string)

SetLocalizationKey sets localization key of error

func (*CellError) Unwrap

func (e *CellError) Unwrap() error

Unwrap implements Go error unwrap function

func (*CellError) Value

func (e *CellError) Value() string

Value gets the value of the cell

func (*CellError) WithParam

func (e *CellError) WithParam(k string, v any) *CellError

WithParam sets a param of error

type ColumnDetail

type ColumnDetail struct {
	Name      string
	Optional  bool
	OmitEmpty bool
	Inline    bool
	DataType  reflect.Type
}

ColumnDetail details of a column parsed from a struct tag

func GetHeaderDetails

func GetHeaderDetails(v any, tagName string) (columnDetails []ColumnDetail, err error)

GetHeaderDetails get CSV header details from the given struct type

type DecodeColumnConfig

type DecodeColumnConfig struct {
	// TrimSpace if `true` and DecodeConfig.TrimSpace is `false`, only trim space this column
	// (default is "false")
	TrimSpace bool

	// StopOnError if `true` and DecodeConfig.StopOnError is `false`, only stop when error occurs
	// within this column processing (default is "false")
	StopOnError bool

	// DecodeFunc custom decode function (optional)
	DecodeFunc DecodeFunc

	// PreprocessorFuncs a list of functions will be called before decoding a cell value (optional)
	PreprocessorFuncs []ProcessorFunc

	// ValidatorFuncs a list of functions will be called after decoding (optional)
	ValidatorFuncs []ValidatorFunc

	// OnCellErrorFunc function will be called every time an error happens when decode a cell.
	// This func can be helpful to set localization key and additional params for the error
	// to localize the error message later on. (optional)
	OnCellErrorFunc OnCellErrorFunc
}

DecodeColumnConfig configuration for decoding a specific column

type DecodeConfig

type DecodeConfig struct {
	// TagName tag name to parse the struct (default is `csv`)
	TagName string

	// NoHeaderMode indicates the input data have no header (default is `false`)
	NoHeaderMode bool

	// StopOnError when error occurs, stop the processing (default is `true`)
	StopOnError bool

	// TrimSpace trim all cell values before processing (default is `false`)
	TrimSpace bool

	// RequireColumnOrder order of columns defined in struct must match the order of columns
	// in the input data (default is "true")
	RequireColumnOrder bool

	// ParseLocalizedHeader header in the input data is localized (default is `false`)
	//
	// For example:
	//  type Student struct {
	//     Name string `csv:"name"`  -> `name` is header key now, the actual header is localized based on the key
	//     Age  int    `csv:"age"`
	//  }
	ParseLocalizedHeader bool

	// AllowUnrecognizedColumns allow a column in the input data but not in the struct tag definition
	// (default is "false")
	AllowUnrecognizedColumns bool

	// TreatIncorrectStructureAsError treat incorrect data structure as error (default is `true`)
	//
	// For example: header has 5 columns, if there is a row having 6 columns, it will be treated as error
	// and the decoding process will stop even StopOnError flag is false.
	TreatIncorrectStructureAsError bool

	// DetectRowLine detect exact lines of rows (default is `false`)
	//
	// If turn this flag on, the input reader should be an instance of "encoding/csv" Reader
	// as this lib uses Reader.FieldPos() function to get the line of a row.
	DetectRowLine bool

	// LocalizationFunc localization function, required when ParseLocalizedHeader is true
	LocalizationFunc LocalizationFunc
	// contains filtered or unexported fields
}

DecodeConfig configuration for decoding CSV data as structs

func (*DecodeConfig) ConfigureColumn

func (c *DecodeConfig) ConfigureColumn(name string, fn func(*DecodeColumnConfig))

type DecodeFunc

type DecodeFunc func(text string, v reflect.Value) error

DecodeFunc decode function for a given cell text

type DecodeOption

type DecodeOption func(cfg *DecodeConfig)

DecodeOption function to modify decoding config

type DecodeResult

type DecodeResult struct {
	// contains filtered or unexported fields
}

DecodeResult decoding result

func Unmarshal

func Unmarshal(data []byte, v any, options ...DecodeOption) (*DecodeResult, error)

Unmarshal convenient method to decode CVS data into a slice of structs

func (*DecodeResult) MissingOptionalColumns

func (r *DecodeResult) MissingOptionalColumns() []string

func (*DecodeResult) TotalRow

func (r *DecodeResult) TotalRow() int

func (*DecodeResult) UnrecognizedColumns

func (r *DecodeResult) UnrecognizedColumns() []string

type Decoder

type Decoder struct {
	// contains filtered or unexported fields
}

Decoder data structure of the default decoder

func NewDecoder

func NewDecoder(r Reader, options ...DecodeOption) *Decoder

NewDecoder creates a new Decoder object

func (*Decoder) Decode

func (d *Decoder) Decode(v any) (*DecodeResult, error)

Decode decode input data and store the result in the given variable. The input var must be a pointer to a slice, e.g. `*[]Student` (recommended) or `*[]*Student`.

func (*Decoder) DecodeOne

func (d *Decoder) DecodeOne(v any) error

DecodeOne decode the next one row data. The input var must be a pointer to a struct (e.g. *Student). This func returns error of the current row processing only, after finishing the last row decoding, call Finish() to get the overall result and error.

func (*Decoder) Finish

func (d *Decoder) Finish() (*DecodeResult, error)

Finish decoding, after calling this func, you can't decode more even there is data

type EncodeColumnConfig

type EncodeColumnConfig struct {
	// Skip whether skip encoding the column or not (this is equivalent to use `csv:"-"` in struct tag)
	// (default is `false`)
	Skip bool

	// EncodeFunc custom encode function (optional)
	EncodeFunc EncodeFunc

	// PostprocessorFuncs a list of functions will be called after encoding a cell value (optional)
	PostprocessorFuncs []ProcessorFunc
}

EncodeColumnConfig configuration for encoding a specific column

type EncodeConfig

type EncodeConfig struct {
	// TagName tag name to parse the struct (default is `csv`)
	TagName string

	// NoHeaderMode indicates whether to write header or not (default is `false`)
	NoHeaderMode bool

	// LocalizeHeader indicates whether to localize the header or not (default is `false`)
	LocalizeHeader bool

	// LocalizationFunc localization function, required when LocalizeHeader is true
	LocalizationFunc LocalizationFunc
	// contains filtered or unexported fields
}

EncodeConfig configuration for encoding Go structs as CSV data

func (*EncodeConfig) ConfigureColumn

func (c *EncodeConfig) ConfigureColumn(name string, fn func(*EncodeColumnConfig))

ConfigureColumn configures encoding for a column by name

type EncodeFunc

type EncodeFunc func(v reflect.Value, omitempty bool) (string, error)

EncodeFunc encode function for a given Go value

type EncodeOption

type EncodeOption func(cfg *EncodeConfig)

EncodeOption function to modify encoding config

type Encoder

type Encoder struct {
	// contains filtered or unexported fields
}

Encoder data structure of the default encoder

func NewEncoder

func NewEncoder(w Writer, options ...EncodeOption) *Encoder

NewEncoder creates a new Encoder object

func (*Encoder) Encode

func (e *Encoder) Encode(v any) error

Encode encode input data stored in the given variable. The input var must be a slice, e.g. `[]Student` or `[]*Student`.

func (*Encoder) EncodeOne

func (e *Encoder) EncodeOne(v any) error

EncodeOne encode single object into a single CSV row

func (*Encoder) Finish

func (e *Encoder) Finish() error

Finish encoding, after calling this func, you can't encode more

type ErrorRenderConfig

type ErrorRenderConfig struct {
	// HeaderFormatKey header format string.
	// You can use a localization key as the value to force the renderer to translate the key first.
	// If the translation fails, the original value is used for next step.
	//
	// For example, header format key can be:
	//   - "HEADER_FORMAT_KEY" (a localization key you define in your localization data such as a json file,
	//         HEADER_FORMAT_KEY = "CSV decoding result: total errors is {{.TotalError}}")
	//   - "CSV decoding result: total errors is {{.TotalError}}" (direct string)
	//
	// Supported params:
	//   {{.TotalRow}}       - number of rows in the CSV data
	//   {{.TotalRowError}}  - number of rows have error
	//   {{.TotalCellError}} - number of cells have error
	//   {{.TotalError}}     - number of errors
	//
	// Extra params:
	//   {{.CrLf}} - line break
	//   {{.Tab}}  - tab character
	HeaderFormatKey string

	// RowFormatKey format string for each row.
	// Similar to the header format key, this can be a localization key or a direct string.
	//
	// For example, row format key can be:
	//   - "ROW_FORMAT_KEY" (a localization key you define in your localization data such as a json file)
	//   - "Row {{.Row}} (line {{.Line}}): {{.Error}}" (direct string)
	//
	// Supported params:
	//   {{.Row}}   - row index (1-based, row 1 can be the header row if present)
	//   {{.Line}}  - line of row in source file (can be -1 if undetected)
	//   {{.Error}} - error content of the row which is a list of cell errors
	RowFormatKey string

	// RowSeparator separator to join row error details, normally a row is in a separated line
	RowSeparator string

	// CellSeparator separator to join cell error details within a row, normally a comma (`,`)
	CellSeparator string

	// LineBreak custom new line character (default is `\n`)
	LineBreak string

	// LocalizeCellFields localize cell's fields before rendering the cell error (default is `true`)
	LocalizeCellFields bool

	// LocalizeCellHeader localize cell header before rendering the cell error (default is `true`)
	LocalizeCellHeader bool

	// Params custom params user wants to send to the localization (optional)
	Params ParameterMap

	// LocalizationFunc function to translate message (optional)
	LocalizationFunc LocalizationFunc

	// CellRenderFunc custom render function for rendering a cell error (optional).
	// The func can return ("", false) to skip rendering the cell error, return ("", true) to let the
	// renderer continue using its solution, and return ("<str>", true) to override the value.
	//
	// Supported params:
	//   {{.Column}}       - column index (0-based)
	//   {{.ColumnHeader}} - column name
	//   {{.Value}}        - cell value
	//   {{.Error}}        - error detail which is result of calling err.Error()
	//
	// Use cellErr.WithParam() to add more extra params
	CellRenderFunc func(*RowErrors, *CellError, ParameterMap) (string, bool)

	// CommonErrorRenderFunc renders common error (not RowErrors, CellError) (optional)
	CommonErrorRenderFunc func(error, ParameterMap) (string, error)
}

type Errors

type Errors struct {
	// contains filtered or unexported fields
}

Errors represents errors returned by the encoder or decoder

func NewErrors

func NewErrors() *Errors

NewErrors creates a new Errors object

func (*Errors) Add

func (e *Errors) Add(errs ...error)

Add appends errors to the list

func (*Errors) Error

func (e *Errors) Error() string

Error implements Go error interface

func (*Errors) HasError

func (e *Errors) HasError() bool

HasError checks if there is at least one error in the list

func (*Errors) Header

func (e *Errors) Header() []string

Header gets list of column headers

func (*Errors) Is

func (e *Errors) Is(err error) bool

Is checks if there is at least an error in the list kind of the specified error

func (*Errors) TotalCellError

func (e *Errors) TotalCellError() int

TotalCellError gets the total number of error of cells

func (*Errors) TotalError

func (e *Errors) TotalError() int

TotalError gets the total number of errors including row errors and cell errors

func (*Errors) TotalRow

func (e *Errors) TotalRow() int

TotalRow gets total rows of CSV data

func (*Errors) TotalRowError

func (e *Errors) TotalRowError() int

TotalRowError gets the total number of error of rows

func (*Errors) Unwrap

func (e *Errors) Unwrap() []error

Unwrap implements Go error unwrap function

type Float

type Float interface {
	float32 | float64
}

type FloatEx

type FloatEx interface {
	~float32 | ~float64
}

type InlineColumn

type InlineColumn[T any] struct {
	Header []string
	Values []T
}

InlineColumn represents inline columns of type `T`

type Int

type Int interface {
	int | int8 | int16 | int32 | int64
}

type IntEx

type IntEx interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

type LTComparable

type LTComparable interface {
	Int | IntEx | UInt | UIntEx | Float | FloatEx | String | StringEx
}

type LocalizationFunc

type LocalizationFunc func(key string, params ParameterMap) (string, error)

LocalizationFunc function to translate message into a specific language

type Number

type Number interface {
	Int | UInt | Float
}

type NumberEx

type NumberEx interface {
	IntEx | UIntEx | FloatEx
}

type OnCellErrorFunc

type OnCellErrorFunc func(e *CellError)

OnCellErrorFunc function to be called when error happens on decoding cell value

type ParameterMap

type ParameterMap map[string]any

type ProcessorFunc

type ProcessorFunc func(s string) string

ProcessorFunc function to transform cell value before decoding or after encoding

type Reader

type Reader interface {
	Read() ([]string, error)
}

Reader reader object interface required by the lib to read CSV data. Should use csv.Reader from the built-in package "encoding/csv".

type RowErrors

type RowErrors struct {
	// contains filtered or unexported fields
}

RowErrors data structure of error of a row

func NewRowErrors

func NewRowErrors(row, line int) *RowErrors

NewRowErrors creates a new RowErrors

func (*RowErrors) Add

func (e *RowErrors) Add(errs ...error)

Add appends errors to the list

func (*RowErrors) Error

func (e *RowErrors) Error() string

Error implements Go error interface

func (*RowErrors) HasError

func (e *RowErrors) HasError() bool

HasError checks if there is at least one error in the list

func (*RowErrors) Is

func (e *RowErrors) Is(err error) bool

Is checks if there is at least an error in the list kind of the specified error

func (*RowErrors) Line

func (e *RowErrors) Line() int

Line gets the line contains the error (line equals to row in most cases)

func (*RowErrors) Row

func (e *RowErrors) Row() int

Row gets the row contains the error

func (*RowErrors) TotalCellError

func (e *RowErrors) TotalCellError() int

TotalCellError gets the total number of error of cells

func (*RowErrors) TotalError

func (e *RowErrors) TotalError() int

TotalError gets the total number of errors

func (*RowErrors) Unwrap

func (e *RowErrors) Unwrap() []error

Unwrap implements Go error unwrap function

type SimpleRenderer

type SimpleRenderer struct {
	// contains filtered or unexported fields
}

SimpleRenderer a simple implementation of error renderer which can produce a text message for the input errors.

func NewRenderer

func NewRenderer(err *Errors, options ...func(*ErrorRenderConfig)) (*SimpleRenderer, error)

NewRenderer creates a new SimpleRenderer

func (*SimpleRenderer) Render

func (r *SimpleRenderer) Render() (msg string, transErr error, err error)

Render renders Errors object as text.

Sample output:

There are 5 total errors in your CSV file
Row 20 (line 21): column 2: invalid type (Int), column 4: value (12345) too big
Row 30 (line 33): column 2: invalid type (Int), column 4: value (12345) too big, column 6: unexpected
Row 35 (line 38): column 2: invalid type (Int), column 4: value (12345) too big
Row 40 (line 44): column 2: invalid type (Int), column 4: value (12345) too big
Row 41 (line 50): invalid number of columns (10)

type String

type String interface {
	string
}

type StringEx

type StringEx interface {
	~string
}

type UInt

type UInt interface {
	uint | uint8 | uint16 | uint32 | uint64
}

type UIntEx

type UIntEx interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
}

type ValidatorFunc

type ValidatorFunc func(v any) error

ValidatorFunc function to validate the values of decoded cells

func ValidatorGT

func ValidatorGT[T LTComparable](val T) ValidatorFunc

ValidatorGT validates a value to be greater than the given value

func ValidatorGTE

func ValidatorGTE[T LTComparable](val T) ValidatorFunc

ValidatorGTE validates a value to be greater than or equal to the given value

func ValidatorIN

func ValidatorIN[T LTComparable](vals ...T) ValidatorFunc

ValidatorIN validates a value to be one of the specific values

func ValidatorLT

func ValidatorLT[T LTComparable](val T) ValidatorFunc

ValidatorLT validates a value to be less than the given value

func ValidatorLTE

func ValidatorLTE[T LTComparable](val T) ValidatorFunc

ValidatorLTE validates a value to be less than or equal to the given value

func ValidatorRange

func ValidatorRange[T LTComparable](min, max T) ValidatorFunc

ValidatorRange validates a value to be in the given range (min and max are inclusive)

func ValidatorStrLen

func ValidatorStrLen[T StringEx](minLen, maxLen int, lenFuncs ...func(s string) int) ValidatorFunc

ValidatorStrLen validates a string to have length in the given range. Pass argument -1 to skip the equivalent validation.

func ValidatorStrPrefix

func ValidatorStrPrefix[T StringEx](prefix string) ValidatorFunc

ValidatorStrPrefix validates a string to have prefix matching the given one

func ValidatorStrSuffix

func ValidatorStrSuffix[T StringEx](suffix string) ValidatorFunc

ValidatorStrSuffix validates a string to have suffix matching the given one

type Writer

type Writer interface {
	Write(record []string) error
}

Writer writer object interface required by the lib to write CSV data to. Should use csv.Writer from the built-in package "encoding/csv".

Jump to

Keyboard shortcuts

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