Documentation
¶
Index ¶
- 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
Constants ¶
const (
DefaultTagName = "csv"
)
Variables ¶
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") )
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 Marshal ¶
func Marshal(v any, options ...EncodeOption) ([]byte, error)
Marshal convenient method to encode a slice of structs into CSV format
func ProcessorNumberGroupComma ¶
ProcessorNumberGroupComma formats a number with grouping its digits by comma
func ProcessorNumberUngroupComma ¶
ProcessorNumberUngroupComma ungroups number digits by comma
Types ¶
type CSVMarshaler ¶
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
type CSVUnmarshaler ¶
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 ¶
NewCellError creates a new CellError
func (*CellError) LocalizationKey ¶
LocalizationKey gets localization key of error
func (*CellError) SetLocalizationKey ¶
SetLocalizationKey sets localization key 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 ¶
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 ¶
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 ¶
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 ¶
Encode encode input data stored in the given variable. The input var must be a slice, e.g. `[]Student` or `[]*Student`.
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 (*Errors) TotalCellError ¶
TotalCellError gets the total number of error of cells
func (*Errors) TotalError ¶
TotalError gets the total number of errors including row errors and cell errors
func (*Errors) TotalRowError ¶
TotalRowError gets the total number of error of rows
type InlineColumn ¶
InlineColumn represents inline columns of type `T`
type LTComparable ¶
type LocalizationFunc ¶
type LocalizationFunc func(key string, params ParameterMap) (string, error)
LocalizationFunc function to translate message into a specific language
type OnCellErrorFunc ¶
type OnCellErrorFunc func(e *CellError)
OnCellErrorFunc function to be called when error happens on decoding cell value
type ParameterMap ¶
type ProcessorFunc ¶
ProcessorFunc function to transform cell value before decoding or after encoding
type Reader ¶
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 ¶
NewRowErrors creates a new RowErrors
func (*RowErrors) Is ¶
Is checks if there is at least an error in the list kind of the specified error
func (*RowErrors) TotalCellError ¶
TotalCellError gets the total number of error of cells
func (*RowErrors) TotalError ¶
TotalError gets the total number of errors
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 ValidatorFunc ¶
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