Documentation
¶
Overview ¶
Package csv returns which columns have syntax errors on a per-line basis when reading CSV. It also has the capability to convert the character encoding to UTF-8 if the CSV character encoding is not UTF-8.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrStructSlicePointerID is the error ID used when the value is not a pointer to a struct slice. ErrStructSlicePointerID = "ErrStructSlicePointer" // ErrInvalidOneOfFormatID is the error ID used when the target is not one of the specified values. ErrInvalidOneOfFormatID = "ErrInvalidOneOfFormat" // ErrInvalidThresholdFormatID is the error ID used when the threshold format is invalid. ErrInvalidThresholdFormatID = "ErrInvalidThresholdFormat" // ErrInvalidBooleanID is the error ID used when the target is not a boolean. ErrInvalidBooleanID = "ErrInvalidBoolean" // ErrInvalidAlphabetID is the error ID used when the target is not an alphabetic character. ErrInvalidAlphabetID = "ErrInvalidAlphabet" // ErrInvalidNumericID is the error ID used when the target is not a numeric character. ErrInvalidNumericID = "ErrInvalidNumeric" // ErrInvalidAlphanumericID is the error ID used when the target is not an alphanumeric character. ErrInvalidAlphanumericID = "ErrInvalidAlphanumeric" // ErrRequiredID is the error ID used when the target is required but is empty. ErrRequiredID = "ErrRequired" // ErrEqualID is the error ID used when the target is not equal to the threshold value. ErrEqualID = "ErrEqual" // ErrInvalidThresholdID is the error ID used when the threshold value is invalid. ErrInvalidThresholdID = "ErrInvalidThreshold" // ErrNotEqualID is the error ID used when the target is equal to the threshold value. ErrNotEqualID = "ErrNotEqual" // ErrGreaterThanID is the error ID used when the target is not greater than the threshold value. ErrGreaterThanID = "ErrGreaterThan" // ErrGreaterThanEqualID is the error ID used when the target is not greater than or equal to the threshold value. ErrGreaterThanEqualID = "ErrGreaterThanEqual" // ErrLessThanID is the error ID used when the target is not less than the threshold value. ErrLessThanID = "ErrLessThan" // ErrLessThanEqualID is the error ID used when the target is not less than or equal to the threshold value. ErrLessThanEqualID = "ErrLessThanEqual" // ErrMinID is the error ID used when the target is less than the minimum value. ErrMinID = "ErrMin" // ErrMaxID is the error ID used when the target is greater than the maximum value. ErrMaxID = "ErrMax" // ErrLengthID is the error ID used when the target length is not equal to the threshold value. ErrLengthID = "ErrLength" // ErrOneOfID is the error ID used when the target is not one of the specified values. ErrOneOfID = "ErrOneOf" // ErrInvalidStructID is the error ID used when the target is not a struct. ErrInvalidStructID = "ErrInvalidStruct" // ErrUnsupportedTypeID is the error ID used when the target is an unsupported type. ErrUnsupportedTypeID = "ErrUnsupportedType" // ErrLowercaseID is the error ID used when the target is not a lowercase character. ErrLowercaseID = "ErrLowercase" // ErrUppercaseID is the error ID used when the target is not an uppercase character. ErrUppercaseID = "ErrUppercase" // ErrASCIIID is the error ID used when the target is not an ASCII character. ErrASCIIID = "ErrASCII" // ErrEmailID is the error ID used when the target is not an email. ErrEmailID = "ErrEmail" // ErrContainsID is the error ID used when the target does not contain the specified value. ErrContainsID = "ErrContains" // ErrInvalidContainsFormatID is the error ID used when the contains format is invalid. ErrInvalidContainsFormatID = "ErrInvalidContainsFormat" // ErrContainsAnyID is the error ID used when the target does not contain any of the specified values. ErrContainsAnyID = "ErrContainsAny" // ErrInvalidContainsAnyFormatID is the error ID used when the contains any format is invalid. ErrInvalidContainsAnyFormatID = "ErrInvalidContainsAnyFormat" )
var LocaleFS embed.FS
Functions ¶
This section is empty.
Types ¶
type CSV ¶
type CSV struct {
// contains filtered or unexported fields
}
CSV is a struct that implements CSV Reader and Writer.
Example ¶
package main import ( "bytes" "fmt" "github.com/nao1215/csv" ) func main() { input := `id,name,age 1,Gina,23 a,Yulia,25 3,Den1s,30 ` buf := bytes.NewBufferString(input) c, err := csv.NewCSV(buf) if err != nil { panic(err) } type person struct { ID int `validate:"numeric"` Name string `validate:"alpha"` Age int `validate:"gt=24"` } people := make([]person, 0) errs := c.Decode(&people) if len(errs) != 0 { for _, err := range errs { fmt.Println(err.Error()) } } }
Output: line:2 column age: target is not greater than the threshold value: threshold=24, value=23 line:3 column id: target is not a numeric character: value=a line:4 column name: target is not an alphabetic character: value=Den1s
type Error ¶ added in v0.1.0
type Error struct {
// contains filtered or unexported fields
}
Error is an error that is used to localize error messages.
type Option ¶
Option is a function that sets a configuration option for CSV struct.
func WithHeaderless ¶
func WithHeaderless() Option
WithHeaderless is an Option that sets the headerless flag to true.
func WithJapaneseLanguage ¶ added in v0.1.0
func WithJapaneseLanguage() Option
WithJapaneseLanguage is an Option that sets the i18n bundle to Japanese.
Example ¶
package main import ( "bytes" "fmt" "github.com/nao1215/csv" ) func main() { input := `id,name,age 1,Gina,23 a,Yulia,25 3,Den1s,30 ` buf := bytes.NewBufferString(input) c, err := csv.NewCSV(buf, csv.WithJapaneseLanguage()) if err != nil { panic(err) } type person struct { ID int `validate:"numeric"` Name string `validate:"alpha"` Age int `validate:"gt=24"` } people := make([]person, 0) errs := c.Decode(&people) if len(errs) != 0 { for _, err := range errs { fmt.Println(err.Error()) } } }
Output: line:2 column age: 値がしきい値より大きくありません: threshold=24, value=23 line:3 column id: 値が数字ではありません: value=a line:4 column name: 値がアルファベット文字ではありません: value=Den1s
func WithRussianLanguage ¶ added in v0.1.0
func WithRussianLanguage() Option
WithRussianLanguage is an Option that sets the i18n bundle to Russian.
Example ¶
package main import ( "bytes" "fmt" "github.com/nao1215/csv" ) func main() { input := `id,name,age 1,Gina,23 a,Yulia,25 3,Den1s,30 ` buf := bytes.NewBufferString(input) c, err := csv.NewCSV(buf, csv.WithRussianLanguage()) if err != nil { panic(err) } type person struct { ID int `validate:"numeric"` Name string `validate:"alpha"` Age int `validate:"gt=24"` } people := make([]person, 0) errs := c.Decode(&people) if len(errs) != 0 { for _, err := range errs { fmt.Println(err.Error()) } } }
Output: line:2 column age: целевое значение не больше порогового значения: threshold=24, value=23 line:3 column id: целевое значение не является числовым символом: value=a line:4 column name: целевое значение не является алфавитным символом: value=Den1s
func WithTabDelimiter ¶
func WithTabDelimiter() Option
WithTabDelimiter is an Option that sets the delimiter to a tab character.