csvlib package - github.com/tiendc/go-csvlib - Go Packages (original) (raw)
- Constants
- Variables
- func GetHeader(v any, tagName string) ([]string, error)
- func Marshal(v any, options ...EncodeOption) ([]byte, error)
- func ProcessorNumberGroupComma(s string) string
- func ProcessorNumberUngroupComma(s string) string
- type CSVMarshaler
- type CSVRenderConfig
- type CSVRenderer
- type CSVUnmarshaler
- type CellError
- func (e *CellError) Column() int
- func (e *CellError) Error() string
- func (e *CellError) HasError() bool
- func (e *CellError) Header() string
- func (e *CellError) Is(err error) bool
- func (e *CellError) LocalizationKey() string
- func (e *CellError) SetLocalizationKey(k string)
- func (e *CellError) Unwrap() error
- func (e *CellError) Value() string
- func (e *CellError) WithParam(k string, v any) *CellError
- type ColumnDetail
- type DecodeColumnConfig
- type DecodeConfig
- type DecodeFunc
- type DecodeOption
- type DecodeResult
- type Decoder
- type EncodeColumnConfig
- type EncodeConfig
- type EncodeFunc
- type EncodeOption
- type Encoder
- type ErrorRenderConfig
- type Errors
- func (e *Errors) Add(errs ...error)
- func (e *Errors) Error() string
- func (e *Errors) HasError() bool
- func (e *Errors) Header() []string
- func (e *Errors) Is(err error) bool
- func (e *Errors) TotalCellError() int
- func (e *Errors) TotalError() int
- func (e *Errors) TotalRow() int
- func (e *Errors) TotalRowError() int
- func (e *Errors) Unwrap() []error
- type Float
- type FloatEx
- type InlineColumn
- type Int
- type IntEx
- type LTComparable
- type LocalizationFunc
- type Number
- type NumberEx
- type OnCellErrorFunc
- type ParameterMap
- type ProcessorFunc
- type Reader
- type RowErrors
- func (e *RowErrors) Add(errs ...error)
- func (e *RowErrors) Error() string
- func (e *RowErrors) HasError() bool
- func (e *RowErrors) Is(err error) bool
- func (e *RowErrors) Line() int
- func (e *RowErrors) Row() int
- func (e *RowErrors) TotalCellError() int
- func (e *RowErrors) TotalError() int
- func (e *RowErrors) Unwrap() []error
- type SimpleRenderer
- type String
- type StringEx
- type UInt
- type UIntEx
- type ValidatorFunc
- func ValidatorGT[T LTComparable](val T) ValidatorFunc
- func ValidatorGTE[T LTComparable](val T) ValidatorFunc
- func ValidatorIN[T LTComparable](vals ...T) ValidatorFunc
- func ValidatorLT[T LTComparable](val T) ValidatorFunc
- func ValidatorLTE[T LTComparable](val T) ValidatorFunc
- func ValidatorRange[T LTComparable](min, max T) ValidatorFunc
- func ValidatorStrLen[T StringEx](minLen, maxLen int, lenFuncs ...func(s string) int) ValidatorFunc
- func ValidatorStrPrefix[T StringEx](prefix string) ValidatorFunc
- func ValidatorStrSuffix[T StringEx](suffix string) ValidatorFunc
- type Writer
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](/errors).[New](/errors#New)("ErrTagOptionInvalid")
ErrConfigOptionInvalid = [errors](/errors).[New](/errors#New)("ErrConfigOptionInvalid")
ErrLocalization = [errors](/errors).[New](/errors#New)("ErrLocalization")
ErrHeaderColumnInvalid = [errors](/errors).[New](/errors#New)("ErrHeaderColumnInvalid")
ErrHeaderColumnUnrecognized = [errors](/errors).[New](/errors#New)("ErrHeaderColumnUnrecognized")
ErrHeaderColumnRequired = [errors](/errors).[New](/errors#New)("ErrHeaderColumnRequired")
ErrHeaderColumnDuplicated = [errors](/errors).[New](/errors#New)("ErrHeaderColumnDuplicated")
ErrHeaderColumnOrderInvalid = [errors](/errors).[New](/errors#New)("ErrHeaderColumnOrderInvalid")
ErrHeaderDynamicRequireColumnOrder = [errors](/errors).[New](/errors#New)("ErrHeaderDynamicRequireColumnOrder")
ErrHeaderDynamicNotAllowUnrecognizedColumns = [errors](/errors).[New](/errors#New)("ErrHeaderDynamicNotAllowUnrecognizedColumns")
ErrValidationConversion = [errors](/errors).[New](/errors#New)("ErrValidationConversion")
ErrValidation = [errors](/errors).[New](/errors#New)("ErrValidation")
ErrValidationLT = [fmt](/fmt).[Errorf](/fmt#Errorf)("%w: LT", [ErrValidation](#ErrValidation))
ErrValidationLTE = [fmt](/fmt).[Errorf](/fmt#Errorf)("%w: LTE", [ErrValidation](#ErrValidation))
ErrValidationGT = [fmt](/fmt).[Errorf](/fmt#Errorf)("%w: GT", [ErrValidation](#ErrValidation))
ErrValidationGTE = [fmt](/fmt).[Errorf](/fmt#Errorf)("%w: GTE", [ErrValidation](#ErrValidation))
ErrValidationRange = [fmt](/fmt).[Errorf](/fmt#Errorf)("%w: Range", [ErrValidation](#ErrValidation))
ErrValidationIN = [fmt](/fmt).[Errorf](/fmt#Errorf)("%w: IN", [ErrValidation](#ErrValidation))
ErrValidationStrLen = [fmt](/fmt).[Errorf](/fmt#Errorf)("%w: StrLen", [ErrValidation](#ErrValidation))
ErrValidationStrPrefix = [fmt](/fmt).[Errorf](/fmt#Errorf)("%w: StrPrefix", [ErrValidation](#ErrValidation))
ErrValidationStrSuffix = [fmt](/fmt).[Errorf](/fmt#Errorf)("%w: StrSuffix", [ErrValidation](#ErrValidation))
ErrDecodeValueType = [errors](/errors).[New](/errors#New)("ErrDecodeValueType")
ErrDecodeRowFieldCount = [errors](/errors).[New](/errors#New)("ErrDecodeRowFieldCount")
ErrDecodeQuoteInvalid = [errors](/errors).[New](/errors#New)("ErrDecodeQuoteInvalid")
ErrEncodeValueType = [errors](/errors).[New](/errors#New)("ErrEncodeValueType"))
GetHeader get CSV header from the given struct
Marshal convenient method to encode a slice of structs into CSV format
ProcessorNumberGroupComma formats a number with grouping its digits by comma
ProcessorNumberUngroupComma ungroups number digits by comma
type CSVMarshaler interface { MarshalCSV() ([]byte, error) }
CSVMarshaler marshaler interface for encoding custom type
type CSVRenderConfig struct {
CellSeparator [string](/builtin#string)
LineBreak [string](/builtin#string)
RenderHeader [bool](/builtin#bool)
RenderRowNumberColumnIndex [int](/builtin#int)
RenderLineNumberColumnIndex [int](/builtin#int)
RenderCommonErrorColumnIndex [int](/builtin#int)
LocalizeCellFields [bool](/builtin#bool)
LocalizeCellHeader [bool](/builtin#bool)
Params [ParameterMap](#ParameterMap)
LocalizationFunc [LocalizationFunc](#LocalizationFunc)
HeaderRenderFunc func([][string](/builtin#string), [ParameterMap](#ParameterMap))
CellRenderFunc func(*[RowErrors](#RowErrors), *[CellError](#CellError), [ParameterMap](#ParameterMap)) ([string](/builtin#string), [bool](/builtin#bool))
CommonErrorRenderFunc func([error](/builtin#error), [ParameterMap](#ParameterMap)) ([string](/builtin#string), [error](/builtin#error))}
type CSVRenderer struct {
}
CSVRenderer an implementation of error renderer which can produce messages for the input errors as CSV output data.
func NewCSVRenderer(err Errors, options ...func(CSVRenderConfig)) (*CSVRenderer, error)
NewCSVRenderer creates a new CSVRenderer
Render renders Errors object as CSV rows data
RenderAsString renders the input as CSV string
RenderTo renders the input as CSV string and writes it to the writer
type CSVUnmarshaler interface { UnmarshalCSV([]byte) error }
CSVUnmarshaler unmarshaler interface for decoding custom type
type CellError struct {
}
CellError data structure of error of a cell
NewCellError creates a new CellError
func (*CellError) Column ¶
func (e *CellError) Column() int
Column gets the column of the cell
Error implements Go error interface
func (e *CellError) HasError() bool
HasError checks if the error contains an error
Header gets the header of the column
Is checks if the inner error is kind of the specified error
func (e *CellError) LocalizationKey() string
LocalizationKey gets localization key of error
func (e *CellError) SetLocalizationKey(k string)
SetLocalizationKey sets localization key of error
Unwrap implements Go error unwrap function
Value gets the value of the cell
WithParam sets a param of error
type ColumnDetail ¶
ColumnDetail details of a column parsed from a struct tag
GetHeaderDetails get CSV header details from the given struct type
type DecodeColumnConfig ¶
type DecodeColumnConfig struct {
TrimSpace [bool](/builtin#bool)
StopOnError [bool](/builtin#bool)
DecodeFunc [DecodeFunc](#DecodeFunc)
PreprocessorFuncs [][ProcessorFunc](#ProcessorFunc)
ValidatorFuncs [][ValidatorFunc](#ValidatorFunc)
OnCellErrorFunc [OnCellErrorFunc](#OnCellErrorFunc)}
DecodeColumnConfig configuration for decoding a specific column
type DecodeConfig struct {
TagName [string](/builtin#string)
NoHeaderMode [bool](/builtin#bool)
StopOnError [bool](/builtin#bool)
TrimSpace [bool](/builtin#bool)
RequireColumnOrder [bool](/builtin#bool)
ParseLocalizedHeader [bool](/builtin#bool)
AllowUnrecognizedColumns [bool](/builtin#bool)
TreatIncorrectStructureAsError [bool](/builtin#bool)
DetectRowLine [bool](/builtin#bool)
LocalizationFunc [LocalizationFunc](#LocalizationFunc)}
DecodeConfig configuration for decoding CSV data as structs
DecodeFunc decode function for a given cell text
type DecodeOption func(cfg *DecodeConfig)
DecodeOption function to modify decoding config
type DecodeResult struct {
}
DecodeResult decoding result
Unmarshal convenient method to decode CVS data into a slice of structs
func (r *DecodeResult) TotalRow() int
Decoder data structure of the default decoder
func NewDecoder(r Reader, options ...DecodeOption) *Decoder
NewDecoder creates a new Decoder object
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`.
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.
Finish decoding, after calling this func, you can't decode more even there is data
type EncodeColumnConfig ¶
type EncodeColumnConfig struct {
Skip [bool](/builtin#bool)
EncodeFunc [EncodeFunc](#EncodeFunc)
PostprocessorFuncs [][ProcessorFunc](#ProcessorFunc)}
EncodeColumnConfig configuration for encoding a specific column
type EncodeConfig struct {
TagName [string](/builtin#string)
NoHeaderMode [bool](/builtin#bool)
LocalizeHeader [bool](/builtin#bool)
LocalizationFunc [LocalizationFunc](#LocalizationFunc)}
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
EncodeFunc encode function for a given Go value
type EncodeOption func(cfg *EncodeConfig)
EncodeOption function to modify encoding config
Encoder data structure of the default encoder
func NewEncoder(w Writer, options ...EncodeOption) *Encoder
NewEncoder creates a new Encoder object
Encode encode input data stored in the given variable. The input var must be a slice, e.g. `[]Student` or `[]*Student`.
EncodeOne encode single object into a single CSV row
Finish encoding, after calling this func, you can't encode more
Errors represents errors returned by the encoder or decoder
NewErrors creates a new Errors object
func (e *Errors) Add(errs ...error)
Add appends errors to the list
Error implements Go error interface
func (e *Errors) HasError() bool
HasError checks if there is at least one error in the list
Header gets list of column headers
Is checks if there is at least an error in the list kind of the specified error
func (e *Errors) TotalCellError() int
TotalCellError gets the total number of error of cells
func (e *Errors) TotalError() int
TotalError gets the total number of errors including row errors and cell errors
func (e *Errors) TotalRow() int
TotalRow gets total rows of CSV data
func (e *Errors) TotalRowError() int
TotalRowError gets the total number of error of rows
Unwrap implements Go error unwrap function
type InlineColumn ¶
type InlineColumn[T any] struct { Header []string Values []T }
InlineColumn represents inline columns of type `T`
type LTComparable interface { Int | IntEx | UInt | UIntEx | Float | FloatEx | String | StringEx }
LocalizationFunc function to translate message into a specific language
type Number interface { Int | UInt | Float }
type NumberEx interface { IntEx | UIntEx | FloatEx }
type OnCellErrorFunc func(e *CellError)
OnCellErrorFunc function to be called when error happens on decoding cell value
ProcessorFunc function to transform cell value before decoding or after encoding
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 struct {
}
RowErrors data structure of error of a row
func NewRowErrors(row, line int) *RowErrors
NewRowErrors creates a new RowErrors
Add appends errors to the list
Error implements Go error interface
func (e *RowErrors) HasError() bool
HasError checks if there is at least one error in the list
Is checks if there is at least an error in the list kind of the specified error
func (e *RowErrors) Line() int
Line gets the line contains the error (line equals to row in most cases)
Row gets the row contains the error
func (e *RowErrors) TotalCellError() int
TotalCellError gets the total number of error of cells
func (e *RowErrors) TotalError() int
TotalError gets the total number of errors
Unwrap implements Go error unwrap function
type SimpleRenderer struct {
}
SimpleRenderer a simple implementation of error renderer which can produce a text message for the input errors.
func NewRenderer(err Errors, options ...func(ErrorRenderConfig)) (*SimpleRenderer, error)
NewRenderer creates a new SimpleRenderer
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 interface { string }
type StringEx interface { ~string }
ValidatorFunc function to validate the values of decoded cells
func ValidatorGT[T LTComparable](val T) ValidatorFunc
ValidatorGT validates a value to be greater than the given value
func ValidatorGTE[T LTComparable](val T) ValidatorFunc
ValidatorGTE validates a value to be greater than or equal to the given value
func ValidatorIN[T LTComparable](vals ...T) ValidatorFunc
ValidatorIN validates a value to be one of the specific values
func ValidatorLT[T LTComparable](val T) ValidatorFunc
ValidatorLT validates a value to be less than the given value
func ValidatorLTE[T LTComparable](val T) ValidatorFunc
ValidatorLTE validates a value to be less than or equal to the given value
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[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[T StringEx](prefix string) ValidatorFunc
ValidatorStrPrefix validates a string to have prefix matching the given one
func ValidatorStrSuffix[T StringEx](suffix string) ValidatorFunc
ValidatorStrSuffix validates a string to have suffix matching the given one
Writer writer object interface required by the lib to write CSV data to. Should use csv.Writer from the built-in package "encoding/csv".