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 = 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 Marshal ¶
func Marshal(v any, options ...EncodeOption) ([]byte, error)
Marshal convenient method to encode a slice of structs into CSV format
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
}
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)
type CSVUnmarshaler ¶
CSVUnmarshaler unmarshaler interface for decoding custom type
type CellError ¶
type CellError struct {
// contains filtered or unexported fields
}
func (*CellError) LocalizationKey ¶
func (*CellError) SetLocalizationKey ¶
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 ¶
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 ¶
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 ¶
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 ¶
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
}
func (*Errors) TotalCellError ¶
func (*Errors) TotalError ¶
func (*Errors) TotalRowError ¶
type InlineColumn ¶
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
}
func NewRowErrors ¶
func (*RowErrors) TotalCellError ¶
func (*RowErrors) TotalError ¶
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 ValidatorFunc ¶
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