csvlib

package module
v0.0.0-...-9203f4f Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2023 License: MIT Imports: 15 Imported by: 0

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          = strings.TrimSpace
	ProcessorTrimPrefix    = strings.TrimPrefix
	ProcessorTrimSuffix    = strings.TrimSuffix
	ProcessorReplace       = strings.Replace
	ProcessorReplaceAll    = strings.ReplaceAll
	ProcessorLower         = strings.ToLower
	ProcessorUpper         = strings.ToUpper
	ProcessorNumberGroup   = gofn.NumberFmtGroup
	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

func ProcessorNumberUngroupComma

func ProcessorNumberUngroupComma(s string) string

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
}

func NewCSVRenderer

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

func (*CSVRenderer) Render

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

Render render Errors object as CSV rows data

func (*CSVRenderer) RenderAsString

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

func (*CSVRenderer) RenderTo

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

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
}

func NewCellError

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

func (*CellError) Column

func (e *CellError) Column() int

func (*CellError) Error

func (e *CellError) Error() string

func (*CellError) HasError

func (e *CellError) HasError() bool

func (*CellError) Header

func (e *CellError) Header() string

func (*CellError) Is

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

func (*CellError) LocalizationKey

func (e *CellError) LocalizationKey() string

func (*CellError) SetLocalizationKey

func (e *CellError) SetLocalizationKey(k string)

func (*CellError) Unwrap

func (e *CellError) Unwrap() error

func (*CellError) Value

func (e *CellError) Value() string

func (*CellError) WithParam

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

type ColumnDetail

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

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 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
}

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)

type DecodeResult

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

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
}

func NewDecoder

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

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
}

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
}

func (*EncodeConfig) ConfigureColumn

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

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)

type Encoder

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

func NewEncoder

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

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

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
}

func NewErrors

func NewErrors() *Errors

func (*Errors) Add

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

func (*Errors) Error

func (e *Errors) Error() string

func (*Errors) HasError

func (e *Errors) HasError() bool

func (*Errors) Header

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

func (*Errors) Is

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

func (*Errors) TotalCellError

func (e *Errors) TotalCellError() int

func (*Errors) TotalError

func (e *Errors) TotalError() int

func (*Errors) TotalRow

func (e *Errors) TotalRow() int

func (*Errors) TotalRowError

func (e *Errors) TotalRowError() int

func (*Errors) Unwrap

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

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
}

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
}

func NewRowErrors

func NewRowErrors(row, line int) *RowErrors

func (*RowErrors) Add

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

func (*RowErrors) Error

func (e *RowErrors) Error() string

func (*RowErrors) HasError

func (e *RowErrors) HasError() bool

func (*RowErrors) Is

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

func (*RowErrors) Line

func (e *RowErrors) Line() int

func (*RowErrors) Row

func (e *RowErrors) Row() int

func (*RowErrors) TotalCellError

func (e *RowErrors) TotalCellError() int

func (*RowErrors) TotalError

func (e *RowErrors) TotalError() int

func (*RowErrors) Unwrap

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

type SimpleRenderer

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

func NewRenderer

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

func (*SimpleRenderer) Render

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

Render render 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 validate a value to be greater than the given value

func ValidatorGTE

func ValidatorGTE[T LTComparable](val T) ValidatorFunc

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

func ValidatorIN

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

ValidatorIN validate a value to be one of the specific values

func ValidatorLT

func ValidatorLT[T LTComparable](val T) ValidatorFunc

ValidatorLT validate a value to be less than the given value

func ValidatorLTE

func ValidatorLTE[T LTComparable](val T) ValidatorFunc

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

func ValidatorRange

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

ValidatorRange validate 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 validate 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 validate a string to have prefix matching the given one

func ValidatorStrSuffix

func ValidatorStrSuffix[T StringEx](suffix string) ValidatorFunc

ValidatorStrSuffix validate 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