From 2485d8f8373fccc23f5de42b4b3215ae3c9aef03 Mon Sep 17 00:00:00 2001 From: Egor Gartman Date: Thu, 12 May 2022 20:23:27 +0300 Subject: [PATCH] Add non-ASCII enum values support (#97) * Add non-ASCII enum values support * fix go1.18 tests Co-authored-by: Egor Gartman --- .../Test118CustomPrefixExampleFile-og | 264 +++++++++++++++++- generator/.snapshots/Test118ExampleFile-og | 150 +++++++++- .../.snapshots/Test118NoPrefixExampleFile-og | 77 ++++- ...t118NoPrefixExampleFileWithSnakeToCamel-og | 77 ++++- .../.snapshots/TestCustomPrefixExampleFile | 264 +++++++++++++++++- generator/.snapshots/TestExampleFile | 150 +++++++++- generator/.snapshots/TestNoPrefixExampleFile | 77 ++++- .../TestNoPrefixExampleFileWithSnakeToCamel | 77 ++++- generator/example_test.go | 8 + generator/generator.go | 35 +-- 10 files changed, 1150 insertions(+), 29 deletions(-) diff --git a/generator/.snapshots/Test118CustomPrefixExampleFile-og b/generator/.snapshots/Test118CustomPrefixExampleFile-og index e92966fa..531abf35 100644 --- a/generator/.snapshots/Test118CustomPrefixExampleFile-og +++ b/generator/.snapshots/Test118CustomPrefixExampleFile-og @@ -1,4 +1,4 @@ -([]string) (len=3377) { +([]string) (len=3639) { (string) (len=41) "// Code generated by go-enum DO NOT EDIT.", (string) (len=13) "// Version: -", (string) (len=14) "// Revision: -", @@ -2576,6 +2576,268 @@ (string) (len=1) "}", (string) "", (string) (len=7) "const (", + (string) (len=66) "\t// Custom_prefix_Продам is a NonASCII of type Продам.", + (string) (len=50) "\tCustom_prefix_Продам NonASCII = iota + 1114", + (string) (len=54) "\t// Custom_prefix_車庫 is a NonASCII of type 車庫.", + (string) (len=43) "\tCustom_prefix_車庫 NonASCII = iota + 299", + (string) (len=58) "\t// Custom_prefix_Էժան is a NonASCII of type Էժան.", + (string) (len=44) "\tCustom_prefix_Էժան NonASCII = iota + -1", + (string) (len=1) ")", + (string) "", + (string) (len=50) "const _NonASCIIName = \"Продам車庫էժան\"", + (string) "", + (string) (len=39) "var _NonASCIIMap = map[NonASCII]string{", + (string) (len=49) "\tCustom_prefix_Продам: _NonASCIIName[0:12],", + (string) (len=48) "\tCustom_prefix_車庫: _NonASCIIName[12:18],", + (string) (len=48) "\tCustom_prefix_Էժան: _NonASCIIName[18:26],", + (string) (len=1) "}", + (string) "", + (string) (len=44) "// String implements the Stringer interface.", + (string) (len=35) "func (x NonASCII) String() string {", + (string) (len=36) "\tif str, ok := _NonASCIIMap[x]; ok {", + (string) (len=12) "\t\treturn str", + (string) (len=2) "\t}", + (string) (len=38) "\treturn fmt.Sprintf(\"NonASCII(%d)\", x)", + (string) (len=1) "}", + (string) "", + (string) (len=41) "var _NonASCIIValue = map[string]NonASCII{", + (string) (len=67) "\t_NonASCIIName[0:12]: Custom_prefix_Продам,", + (string) (len=67) "\tstrings.ToLower(_NonASCIIName[0:12]): Custom_prefix_Продам,", + (string) (len=61) "\t_NonASCIIName[12:18]: Custom_prefix_車庫,", + (string) (len=61) "\tstrings.ToLower(_NonASCIIName[12:18]): Custom_prefix_車庫,", + (string) (len=63) "\t_NonASCIIName[18:26]: Custom_prefix_Էժան,", + (string) (len=63) "\tstrings.ToLower(_NonASCIIName[18:26]): Custom_prefix_Էժան,", + (string) (len=1) "}", + (string) "", + (string) (len=60) "// ParseNonASCII attempts to convert a string to a NonASCII.", + (string) (len=51) "func ParseNonASCII(name string) (NonASCII, error) {", + (string) (len=39) "\tif x, ok := _NonASCIIValue[name]; ok {", + (string) (len=15) "\t\treturn x, nil", + (string) (len=2) "\t}", + (string) (len=67) "\treturn NonASCII(0), fmt.Errorf(\"%s is not a valid NonASCII\", name)", + (string) (len=1) "}", + (string) "", + (string) (len=35) "func (x NonASCII) Ptr() *NonASCII {", + (string) (len=10) "\treturn &x", + (string) (len=1) "}", + (string) "", + (string) (len=53) "// MarshalText implements the text marshaller method.", + (string) (len=49) "func (x NonASCII) MarshalText() ([]byte, error) {", + (string) (len=31) "\treturn []byte(x.String()), nil", + (string) (len=1) "}", + (string) "", + (string) (len=57) "// UnmarshalText implements the text unmarshaller method.", + (string) (len=53) "func (x *NonASCII) UnmarshalText(text []byte) error {", + (string) (len=21) "\tname := string(text)", + (string) (len=32) "\ttmp, err := ParseNonASCII(name)", + (string) (len=16) "\tif err != nil {", + (string) (len=12) "\t\treturn err", + (string) (len=2) "\t}", + (string) (len=9) "\t*x = tmp", + (string) (len=11) "\treturn nil", + (string) (len=1) "}", + (string) "", + (string) (len=95) "var _NonASCIIErrNilPtr = errors.New(\"value pointer is nil\") // one per type for package clashes", + (string) "", + (string) (len=41) "// Scan implements the Scanner interface.", + (string) (len=56) "func (x *NonASCII) Scan(value interface{}) (err error) {", + (string) (len=18) "\tif value == nil {", + (string) (len=18) "\t\t*x = NonASCII(0)", + (string) (len=8) "\t\treturn", + (string) (len=2) "\t}", + (string) "", + (string) (len=37) "\t// A wider range of scannable types.", + (string) (len=61) "\t// driver.Value values at the top of the list for expediency", + (string) (len=27) "\tswitch v := value.(type) {", + (string) (len=12) "\tcase int64:", + (string) (len=18) "\t\t*x = NonASCII(v)", + (string) (len=13) "\tcase string:", + (string) (len=28) "\t\t*x, err = ParseNonASCII(v)", + (string) (len=17) "\t\tif err != nil {", + (string) (len=47) "\t\t\t// try parsing the integer value as a string", + (string) (len=49) "\t\t\tif val, verr := strconv.Atoi(v); verr == nil {", + (string) (len=32) "\t\t\t\t*x, err = NonASCII(val), nil", + (string) (len=4) "\t\t\t}", + (string) (len=3) "\t\t}", + (string) (len=13) "\tcase []byte:", + (string) (len=36) "\t\t*x, err = ParseNonASCII(string(v))", + (string) (len=17) "\t\tif err != nil {", + (string) (len=47) "\t\t\t// try parsing the integer value as a string", + (string) (len=57) "\t\t\tif val, verr := strconv.Atoi(string(v)); verr == nil {", + (string) (len=32) "\t\t\t\t*x, err = NonASCII(val), nil", + (string) (len=4) "\t\t\t}", + (string) (len=3) "\t\t}", + (string) (len=15) "\tcase NonASCII:", + (string) (len=8) "\t\t*x = v", + (string) (len=10) "\tcase int:", + (string) (len=18) "\t\t*x = NonASCII(v)", + (string) (len=16) "\tcase *NonASCII:", + (string) (len=15) "\t\tif v == nil {", + (string) (len=28) "\t\t\treturn _NonASCIIErrNilPtr", + (string) (len=3) "\t\t}", + (string) (len=9) "\t\t*x = *v", + (string) (len=11) "\tcase uint:", + (string) (len=18) "\t\t*x = NonASCII(v)", + (string) (len=13) "\tcase uint64:", + (string) (len=18) "\t\t*x = NonASCII(v)", + (string) (len=11) "\tcase *int:", + (string) (len=15) "\t\tif v == nil {", + (string) (len=28) "\t\t\treturn _NonASCIIErrNilPtr", + (string) (len=3) "\t\t}", + (string) (len=19) "\t\t*x = NonASCII(*v)", + (string) (len=13) "\tcase *int64:", + (string) (len=15) "\t\tif v == nil {", + (string) (len=28) "\t\t\treturn _NonASCIIErrNilPtr", + (string) (len=3) "\t\t}", + (string) (len=19) "\t\t*x = NonASCII(*v)", + (string) (len=72) "\tcase float64: // json marshals everything as a float64 if it's a number", + (string) (len=18) "\t\t*x = NonASCII(v)", + (string) (len=73) "\tcase *float64: // json marshals everything as a float64 if it's a number", + (string) (len=15) "\t\tif v == nil {", + (string) (len=28) "\t\t\treturn _NonASCIIErrNilPtr", + (string) (len=3) "\t\t}", + (string) (len=19) "\t\t*x = NonASCII(*v)", + (string) (len=12) "\tcase *uint:", + (string) (len=15) "\t\tif v == nil {", + (string) (len=28) "\t\t\treturn _NonASCIIErrNilPtr", + (string) (len=3) "\t\t}", + (string) (len=19) "\t\t*x = NonASCII(*v)", + (string) (len=14) "\tcase *uint64:", + (string) (len=15) "\t\tif v == nil {", + (string) (len=28) "\t\t\treturn _NonASCIIErrNilPtr", + (string) (len=3) "\t\t}", + (string) (len=19) "\t\t*x = NonASCII(*v)", + (string) (len=14) "\tcase *string:", + (string) (len=15) "\t\tif v == nil {", + (string) (len=28) "\t\t\treturn _NonASCIIErrNilPtr", + (string) (len=3) "\t\t}", + (string) (len=29) "\t\t*x, err = ParseNonASCII(*v)", + (string) (len=17) "\t\tif err != nil {", + (string) (len=47) "\t\t\t// try parsing the integer value as a string", + (string) (len=50) "\t\t\tif val, verr := strconv.Atoi(*v); verr == nil {", + (string) (len=32) "\t\t\t\t*x, err = NonASCII(val), nil", + (string) (len=4) "\t\t\t}", + (string) (len=3) "\t\t}", + (string) (len=2) "\t}", + (string) "", + (string) (len=7) "\treturn", + (string) (len=1) "}", + (string) "", + (string) (len=48) "// Value implements the driver Valuer interface.", + (string) (len=49) "func (x NonASCII) Value() (driver.Value, error) {", + (string) (len=23) "\treturn x.String(), nil", + (string) (len=1) "}", + (string) "", + (string) (len=55) "// Set implements the Golang flag.Value interface func.", + (string) (len=42) "func (x *NonASCII) Set(val string) error {", + (string) (len=29) "\tv, err := ParseNonASCII(val)", + (string) (len=7) "\t*x = v", + (string) (len=11) "\treturn err", + (string) (len=1) "}", + (string) "", + (string) (len=56) "// Get implements the Golang flag.Getter interface func.", + (string) (len=38) "func (x *NonASCII) Get() interface{} {", + (string) (len=10) "\treturn *x", + (string) (len=1) "}", + (string) "", + (string) (len=62) "// Type implements the github.com/spf13/pFlag Value interface.", + (string) (len=34) "func (x *NonASCII) Type() string {", + (string) (len=18) "\treturn \"NonASCII\"", + (string) (len=1) "}", + (string) "", + (string) (len=26) "type NullNonASCII struct {", + (string) (len=18) "\tNonASCII NonASCII", + (string) (len=14) "\tValid bool", + (string) (len=14) "\tSet bool", + (string) (len=1) "}", + (string) "", + (string) (len=56) "func NewNullNonASCII(val interface{}) (x NullNonASCII) {", + (string) (len=76) "\tx.Scan(val) // yes, we ignore this error, it will just be an invalid value.", + (string) (len=7) "\treturn", + (string) (len=1) "}", + (string) "", + (string) (len=41) "// Scan implements the Scanner interface.", + (string) (len=60) "func (x *NullNonASCII) Scan(value interface{}) (err error) {", + (string) (len=13) "\tx.Set = true", + (string) (len=18) "\tif value == nil {", + (string) (len=42) "\t\tx.NonASCII, x.Valid = NonASCII(0), false", + (string) (len=8) "\t\treturn", + (string) (len=2) "\t}", + (string) "", + (string) (len=29) "\terr = x.NonASCII.Scan(value)", + (string) (len=23) "\tx.Valid = (err == nil)", + (string) (len=7) "\treturn", + (string) (len=1) "}", + (string) "", + (string) (len=48) "// Value implements the driver Valuer interface.", + (string) (len=53) "func (x NullNonASCII) Value() (driver.Value, error) {", + (string) (len=14) "\tif !x.Valid {", + (string) (len=17) "\t\treturn nil, nil", + (string) (len=2) "\t}", + (string) (len=46) "\t// driver.Value accepts int64 for int values.", + (string) (len=30) "\treturn int64(x.NonASCII), nil", + (string) (len=1) "}", + (string) "", + (string) (len=59) "// MarshalJSON correctly serializes a NullNonASCII to JSON.", + (string) (len=53) "func (n NullNonASCII) MarshalJSON() ([]byte, error) {", + (string) (len=23) "\tconst nullStr = \"null\"", + (string) (len=13) "\tif n.Valid {", + (string) (len=33) "\t\treturn json.Marshal(n.NonASCII)", + (string) (len=2) "\t}", + (string) (len=28) "\treturn []byte(nullStr), nil", + (string) (len=1) "}", + (string) "", + (string) (len=65) "// UnmarshalJSON correctly deserializes a NullNonASCII from JSON.", + (string) (len=54) "func (n *NullNonASCII) UnmarshalJSON(b []byte) error {", + (string) (len=13) "\tn.Set = true", + (string) (len=18) "\tvar x interface{}", + (string) (len=29) "\terr := json.Unmarshal(b, &x)", + (string) (len=16) "\tif err != nil {", + (string) (len=12) "\t\treturn err", + (string) (len=2) "\t}", + (string) (len=16) "\terr = n.Scan(x)", + (string) (len=11) "\treturn err", + (string) (len=1) "}", + (string) "", + (string) (len=29) "type NullNonASCIIStr struct {", + (string) (len=13) "\tNullNonASCII", + (string) (len=1) "}", + (string) "", + (string) (len=62) "func NewNullNonASCIIStr(val interface{}) (x NullNonASCIIStr) {", + (string) (len=76) "\tx.Scan(val) // yes, we ignore this error, it will just be an invalid value.", + (string) (len=7) "\treturn", + (string) (len=1) "}", + (string) "", + (string) (len=48) "// Value implements the driver Valuer interface.", + (string) (len=56) "func (x NullNonASCIIStr) Value() (driver.Value, error) {", + (string) (len=14) "\tif !x.Valid {", + (string) (len=17) "\t\treturn nil, nil", + (string) (len=2) "\t}", + (string) (len=32) "\treturn x.NonASCII.String(), nil", + (string) (len=1) "}", + (string) "", + (string) (len=59) "// MarshalJSON correctly serializes a NullNonASCII to JSON.", + (string) (len=56) "func (n NullNonASCIIStr) MarshalJSON() ([]byte, error) {", + (string) (len=23) "\tconst nullStr = \"null\"", + (string) (len=13) "\tif n.Valid {", + (string) (len=33) "\t\treturn json.Marshal(n.NonASCII)", + (string) (len=2) "\t}", + (string) (len=28) "\treturn []byte(nullStr), nil", + (string) (len=1) "}", + (string) "", + (string) (len=65) "// UnmarshalJSON correctly deserializes a NullNonASCII from JSON.", + (string) (len=57) "func (n *NullNonASCIIStr) UnmarshalJSON(b []byte) error {", + (string) (len=13) "\tn.Set = true", + (string) (len=18) "\tvar x interface{}", + (string) (len=29) "\terr := json.Unmarshal(b, &x)", + (string) (len=16) "\tif err != nil {", + (string) (len=12) "\t\treturn err", + (string) (len=2) "\t}", + (string) (len=16) "\terr = n.Scan(x)", + (string) (len=11) "\treturn err", + (string) (len=1) "}", + (string) "", + (string) (len=7) "const (", (string) (len=65) "\t// Custom_prefix_TestHyphen is a Sanitizing of type Test-Hyphen.", (string) (len=43) "\tCustom_prefix_TestHyphen Sanitizing = iota", (string) (len=67) "\t// Custom_prefix_HyphenStart is a Sanitizing of type -HyphenStart.", diff --git a/generator/.snapshots/Test118ExampleFile-og b/generator/.snapshots/Test118ExampleFile-og index 66263575..559ddadf 100644 --- a/generator/.snapshots/Test118ExampleFile-og +++ b/generator/.snapshots/Test118ExampleFile-og @@ -1,4 +1,4 @@ -([]string) (len=2047) { +([]string) (len=2195) { (string) (len=41) "// Code generated by go-enum DO NOT EDIT.", (string) (len=13) "// Version: -", (string) (len=14) "// Revision: -", @@ -1585,6 +1585,154 @@ (string) (len=1) "}", (string) "", (string) (len=7) "const (", + (string) (len=60) "\t// NonASCIIПродам is a NonASCII of type Продам.", + (string) (len=44) "\tNonASCIIПродам NonASCII = iota + 1114", + (string) (len=48) "\t// NonASCII車庫 is a NonASCII of type 車庫.", + (string) (len=37) "\tNonASCII車庫 NonASCII = iota + 299", + (string) (len=52) "\t// NonASCIIԷժան is a NonASCII of type Էժան.", + (string) (len=38) "\tNonASCIIԷժան NonASCII = iota + -1", + (string) (len=1) ")", + (string) "", + (string) (len=50) "const _NonASCIIName = \"Продам車庫էժան\"", + (string) "", + (string) (len=30) "var _NonASCIINames = []string{", + (string) (len=21) "\t_NonASCIIName[0:12],", + (string) (len=22) "\t_NonASCIIName[12:18],", + (string) (len=22) "\t_NonASCIIName[18:26],", + (string) (len=1) "}", + (string) "", + (string) (len=70) "// NonASCIINames returns a list of possible string values of NonASCII.", + (string) (len=31) "func NonASCIINames() []string {", + (string) (len=43) "\ttmp := make([]string, len(_NonASCIINames))", + (string) (len=26) "\tcopy(tmp, _NonASCIINames)", + (string) (len=11) "\treturn tmp", + (string) (len=1) "}", + (string) "", + (string) (len=39) "var _NonASCIIMap = map[NonASCII]string{", + (string) (len=43) "\tNonASCIIПродам: _NonASCIIName[0:12],", + (string) (len=42) "\tNonASCII車庫: _NonASCIIName[12:18],", + (string) (len=42) "\tNonASCIIԷժան: _NonASCIIName[18:26],", + (string) (len=1) "}", + (string) "", + (string) (len=44) "// String implements the Stringer interface.", + (string) (len=35) "func (x NonASCII) String() string {", + (string) (len=36) "\tif str, ok := _NonASCIIMap[x]; ok {", + (string) (len=12) "\t\treturn str", + (string) (len=2) "\t}", + (string) (len=38) "\treturn fmt.Sprintf(\"NonASCII(%d)\", x)", + (string) (len=1) "}", + (string) "", + (string) (len=41) "var _NonASCIIValue = map[string]NonASCII{", + (string) (len=61) "\t_NonASCIIName[0:12]: NonASCIIПродам,", + (string) (len=61) "\tstrings.ToLower(_NonASCIIName[0:12]): NonASCIIПродам,", + (string) (len=55) "\t_NonASCIIName[12:18]: NonASCII車庫,", + (string) (len=55) "\tstrings.ToLower(_NonASCIIName[12:18]): NonASCII車庫,", + (string) (len=57) "\t_NonASCIIName[18:26]: NonASCIIԷժան,", + (string) (len=57) "\tstrings.ToLower(_NonASCIIName[18:26]): NonASCIIԷժան,", + (string) (len=1) "}", + (string) "", + (string) (len=60) "// ParseNonASCII attempts to convert a string to a NonASCII.", + (string) (len=51) "func ParseNonASCII(name string) (NonASCII, error) {", + (string) (len=39) "\tif x, ok := _NonASCIIValue[name]; ok {", + (string) (len=15) "\t\treturn x, nil", + (string) (len=2) "\t}", + (string) (len=121) "\t// Case insensitive parse, do a separate lookup to prevent unnecessary cost of lowercasing a string if we don't need to.", + (string) (len=56) "\tif x, ok := _NonASCIIValue[strings.ToLower(name)]; ok {", + (string) (len=15) "\t\treturn x, nil", + (string) (len=2) "\t}", + (string) (len=113) "\treturn NonASCII(0), fmt.Errorf(\"%s is not a valid NonASCII, try [%s]\", name, strings.Join(_NonASCIINames, \", \"))", + (string) (len=1) "}", + (string) "", + (string) (len=53) "// MarshalText implements the text marshaller method.", + (string) (len=49) "func (x NonASCII) MarshalText() ([]byte, error) {", + (string) (len=31) "\treturn []byte(x.String()), nil", + (string) (len=1) "}", + (string) "", + (string) (len=57) "// UnmarshalText implements the text unmarshaller method.", + (string) (len=53) "func (x *NonASCII) UnmarshalText(text []byte) error {", + (string) (len=21) "\tname := string(text)", + (string) (len=32) "\ttmp, err := ParseNonASCII(name)", + (string) (len=16) "\tif err != nil {", + (string) (len=12) "\t\treturn err", + (string) (len=2) "\t}", + (string) (len=9) "\t*x = tmp", + (string) (len=11) "\treturn nil", + (string) (len=1) "}", + (string) "", + (string) (len=95) "var _NonASCIIErrNilPtr = errors.New(\"value pointer is nil\") // one per type for package clashes", + (string) "", + (string) (len=41) "// Scan implements the Scanner interface.", + (string) (len=56) "func (x *NonASCII) Scan(value interface{}) (err error) {", + (string) (len=18) "\tif value == nil {", + (string) (len=18) "\t\t*x = NonASCII(0)", + (string) (len=8) "\t\treturn", + (string) (len=2) "\t}", + (string) "", + (string) (len=37) "\t// A wider range of scannable types.", + (string) (len=61) "\t// driver.Value values at the top of the list for expediency", + (string) (len=27) "\tswitch v := value.(type) {", + (string) (len=12) "\tcase int64:", + (string) (len=18) "\t\t*x = NonASCII(v)", + (string) (len=13) "\tcase string:", + (string) (len=28) "\t\t*x, err = ParseNonASCII(v)", + (string) (len=13) "\tcase []byte:", + (string) (len=36) "\t\t*x, err = ParseNonASCII(string(v))", + (string) (len=15) "\tcase NonASCII:", + (string) (len=8) "\t\t*x = v", + (string) (len=10) "\tcase int:", + (string) (len=18) "\t\t*x = NonASCII(v)", + (string) (len=16) "\tcase *NonASCII:", + (string) (len=15) "\t\tif v == nil {", + (string) (len=28) "\t\t\treturn _NonASCIIErrNilPtr", + (string) (len=3) "\t\t}", + (string) (len=9) "\t\t*x = *v", + (string) (len=11) "\tcase uint:", + (string) (len=18) "\t\t*x = NonASCII(v)", + (string) (len=13) "\tcase uint64:", + (string) (len=18) "\t\t*x = NonASCII(v)", + (string) (len=11) "\tcase *int:", + (string) (len=15) "\t\tif v == nil {", + (string) (len=28) "\t\t\treturn _NonASCIIErrNilPtr", + (string) (len=3) "\t\t}", + (string) (len=19) "\t\t*x = NonASCII(*v)", + (string) (len=13) "\tcase *int64:", + (string) (len=15) "\t\tif v == nil {", + (string) (len=28) "\t\t\treturn _NonASCIIErrNilPtr", + (string) (len=3) "\t\t}", + (string) (len=19) "\t\t*x = NonASCII(*v)", + (string) (len=72) "\tcase float64: // json marshals everything as a float64 if it's a number", + (string) (len=18) "\t\t*x = NonASCII(v)", + (string) (len=73) "\tcase *float64: // json marshals everything as a float64 if it's a number", + (string) (len=15) "\t\tif v == nil {", + (string) (len=28) "\t\t\treturn _NonASCIIErrNilPtr", + (string) (len=3) "\t\t}", + (string) (len=19) "\t\t*x = NonASCII(*v)", + (string) (len=12) "\tcase *uint:", + (string) (len=15) "\t\tif v == nil {", + (string) (len=28) "\t\t\treturn _NonASCIIErrNilPtr", + (string) (len=3) "\t\t}", + (string) (len=19) "\t\t*x = NonASCII(*v)", + (string) (len=14) "\tcase *uint64:", + (string) (len=15) "\t\tif v == nil {", + (string) (len=28) "\t\t\treturn _NonASCIIErrNilPtr", + (string) (len=3) "\t\t}", + (string) (len=19) "\t\t*x = NonASCII(*v)", + (string) (len=14) "\tcase *string:", + (string) (len=15) "\t\tif v == nil {", + (string) (len=28) "\t\t\treturn _NonASCIIErrNilPtr", + (string) (len=3) "\t\t}", + (string) (len=29) "\t\t*x, err = ParseNonASCII(*v)", + (string) (len=2) "\t}", + (string) "", + (string) (len=7) "\treturn", + (string) (len=1) "}", + (string) "", + (string) (len=48) "// Value implements the driver Valuer interface.", + (string) (len=49) "func (x NonASCII) Value() (driver.Value, error) {", + (string) (len=23) "\treturn x.String(), nil", + (string) (len=1) "}", + (string) "", + (string) (len=7) "const (", (string) (len=61) "\t// SanitizingTestHyphen is a Sanitizing of type Test-Hyphen.", (string) (len=39) "\tSanitizingTestHyphen Sanitizing = iota", (string) (len=63) "\t// SanitizingHyphenStart is a Sanitizing of type -HyphenStart.", diff --git a/generator/.snapshots/Test118NoPrefixExampleFile-og b/generator/.snapshots/Test118NoPrefixExampleFile-og index 4834ad98..c263fa4e 100644 --- a/generator/.snapshots/Test118NoPrefixExampleFile-og +++ b/generator/.snapshots/Test118NoPrefixExampleFile-og @@ -1,4 +1,4 @@ -([]string) (len=1128) { +([]string) (len=1203) { (string) (len=41) "// Code generated by go-enum DO NOT EDIT.", (string) (len=13) "// Version: -", (string) (len=14) "// Revision: -", @@ -888,6 +888,81 @@ (string) (len=1) "}", (string) "", (string) (len=7) "const (", + (string) (len=52) "\t// Продам is a NonASCII of type Продам.", + (string) (len=36) "\tПродам NonASCII = iota + 1114", + (string) (len=40) "\t// 車庫 is a NonASCII of type 車庫.", + (string) (len=29) "\t車庫 NonASCII = iota + 299", + (string) (len=44) "\t// Էժան is a NonASCII of type Էժան.", + (string) (len=30) "\tԷժան NonASCII = iota + -1", + (string) (len=1) ")", + (string) "", + (string) (len=50) "const _NonASCIIName = \"Продам車庫էժան\"", + (string) "", + (string) (len=39) "var _NonASCIIMap = map[NonASCII]string{", + (string) (len=35) "\tПродам: _NonASCIIName[0:12],", + (string) (len=34) "\t車庫: _NonASCIIName[12:18],", + (string) (len=34) "\tԷժան: _NonASCIIName[18:26],", + (string) (len=1) "}", + (string) "", + (string) (len=44) "// String implements the Stringer interface.", + (string) (len=35) "func (x NonASCII) String() string {", + (string) (len=36) "\tif str, ok := _NonASCIIMap[x]; ok {", + (string) (len=12) "\t\treturn str", + (string) (len=2) "\t}", + (string) (len=38) "\treturn fmt.Sprintf(\"NonASCII(%d)\", x)", + (string) (len=1) "}", + (string) "", + (string) (len=41) "var _NonASCIIValue = map[string]NonASCII{", + (string) (len=53) "\t_NonASCIIName[0:12]: Продам,", + (string) (len=53) "\tstrings.ToLower(_NonASCIIName[0:12]): Продам,", + (string) (len=47) "\t_NonASCIIName[12:18]: 車庫,", + (string) (len=47) "\tstrings.ToLower(_NonASCIIName[12:18]): 車庫,", + (string) (len=49) "\t_NonASCIIName[18:26]: Էժան,", + (string) (len=49) "\tstrings.ToLower(_NonASCIIName[18:26]): Էժան,", + (string) (len=1) "}", + (string) "", + (string) (len=60) "// ParseNonASCII attempts to convert a string to a NonASCII.", + (string) (len=51) "func ParseNonASCII(name string) (NonASCII, error) {", + (string) (len=39) "\tif x, ok := _NonASCIIValue[name]; ok {", + (string) (len=15) "\t\treturn x, nil", + (string) (len=2) "\t}", + (string) (len=67) "\treturn NonASCII(0), fmt.Errorf(\"%s is not a valid NonASCII\", name)", + (string) (len=1) "}", + (string) "", + (string) (len=53) "// MarshalText implements the text marshaller method.", + (string) (len=49) "func (x NonASCII) MarshalText() ([]byte, error) {", + (string) (len=31) "\treturn []byte(x.String()), nil", + (string) (len=1) "}", + (string) "", + (string) (len=57) "// UnmarshalText implements the text unmarshaller method.", + (string) (len=53) "func (x *NonASCII) UnmarshalText(text []byte) error {", + (string) (len=21) "\tname := string(text)", + (string) (len=32) "\ttmp, err := ParseNonASCII(name)", + (string) (len=16) "\tif err != nil {", + (string) (len=12) "\t\treturn err", + (string) (len=2) "\t}", + (string) (len=9) "\t*x = tmp", + (string) (len=11) "\treturn nil", + (string) (len=1) "}", + (string) "", + (string) (len=55) "// Set implements the Golang flag.Value interface func.", + (string) (len=42) "func (x *NonASCII) Set(val string) error {", + (string) (len=29) "\tv, err := ParseNonASCII(val)", + (string) (len=7) "\t*x = v", + (string) (len=11) "\treturn err", + (string) (len=1) "}", + (string) "", + (string) (len=56) "// Get implements the Golang flag.Getter interface func.", + (string) (len=38) "func (x *NonASCII) Get() interface{} {", + (string) (len=10) "\treturn *x", + (string) (len=1) "}", + (string) "", + (string) (len=62) "// Type implements the github.com/spf13/pFlag Value interface.", + (string) (len=34) "func (x *NonASCII) Type() string {", + (string) (len=18) "\treturn \"NonASCII\"", + (string) (len=1) "}", + (string) "", + (string) (len=7) "const (", (string) (len=51) "\t// TestHyphen is a Sanitizing of type Test-Hyphen.", (string) (len=29) "\tTestHyphen Sanitizing = iota", (string) (len=54) "\t// XHyphenStart is a Sanitizing of type -HyphenStart.", diff --git a/generator/.snapshots/Test118NoPrefixExampleFileWithSnakeToCamel-og b/generator/.snapshots/Test118NoPrefixExampleFileWithSnakeToCamel-og index e920dd2e..59670ee8 100644 --- a/generator/.snapshots/Test118NoPrefixExampleFileWithSnakeToCamel-og +++ b/generator/.snapshots/Test118NoPrefixExampleFileWithSnakeToCamel-og @@ -1,4 +1,4 @@ -([]string) (len=1128) { +([]string) (len=1203) { (string) (len=41) "// Code generated by go-enum DO NOT EDIT.", (string) (len=13) "// Version: -", (string) (len=14) "// Revision: -", @@ -888,6 +888,81 @@ (string) (len=1) "}", (string) "", (string) (len=7) "const (", + (string) (len=52) "\t// Продам is a NonASCII of type Продам.", + (string) (len=36) "\tПродам NonASCII = iota + 1114", + (string) (len=40) "\t// 車庫 is a NonASCII of type 車庫.", + (string) (len=29) "\t車庫 NonASCII = iota + 299", + (string) (len=44) "\t// Էժան is a NonASCII of type Էժան.", + (string) (len=30) "\tԷժան NonASCII = iota + -1", + (string) (len=1) ")", + (string) "", + (string) (len=50) "const _NonASCIIName = \"Продам車庫էժան\"", + (string) "", + (string) (len=39) "var _NonASCIIMap = map[NonASCII]string{", + (string) (len=35) "\tПродам: _NonASCIIName[0:12],", + (string) (len=34) "\t車庫: _NonASCIIName[12:18],", + (string) (len=34) "\tԷժան: _NonASCIIName[18:26],", + (string) (len=1) "}", + (string) "", + (string) (len=44) "// String implements the Stringer interface.", + (string) (len=35) "func (x NonASCII) String() string {", + (string) (len=36) "\tif str, ok := _NonASCIIMap[x]; ok {", + (string) (len=12) "\t\treturn str", + (string) (len=2) "\t}", + (string) (len=38) "\treturn fmt.Sprintf(\"NonASCII(%d)\", x)", + (string) (len=1) "}", + (string) "", + (string) (len=41) "var _NonASCIIValue = map[string]NonASCII{", + (string) (len=53) "\t_NonASCIIName[0:12]: Продам,", + (string) (len=53) "\tstrings.ToLower(_NonASCIIName[0:12]): Продам,", + (string) (len=47) "\t_NonASCIIName[12:18]: 車庫,", + (string) (len=47) "\tstrings.ToLower(_NonASCIIName[12:18]): 車庫,", + (string) (len=49) "\t_NonASCIIName[18:26]: Էժան,", + (string) (len=49) "\tstrings.ToLower(_NonASCIIName[18:26]): Էժան,", + (string) (len=1) "}", + (string) "", + (string) (len=60) "// ParseNonASCII attempts to convert a string to a NonASCII.", + (string) (len=51) "func ParseNonASCII(name string) (NonASCII, error) {", + (string) (len=39) "\tif x, ok := _NonASCIIValue[name]; ok {", + (string) (len=15) "\t\treturn x, nil", + (string) (len=2) "\t}", + (string) (len=67) "\treturn NonASCII(0), fmt.Errorf(\"%s is not a valid NonASCII\", name)", + (string) (len=1) "}", + (string) "", + (string) (len=53) "// MarshalText implements the text marshaller method.", + (string) (len=49) "func (x NonASCII) MarshalText() ([]byte, error) {", + (string) (len=31) "\treturn []byte(x.String()), nil", + (string) (len=1) "}", + (string) "", + (string) (len=57) "// UnmarshalText implements the text unmarshaller method.", + (string) (len=53) "func (x *NonASCII) UnmarshalText(text []byte) error {", + (string) (len=21) "\tname := string(text)", + (string) (len=32) "\ttmp, err := ParseNonASCII(name)", + (string) (len=16) "\tif err != nil {", + (string) (len=12) "\t\treturn err", + (string) (len=2) "\t}", + (string) (len=9) "\t*x = tmp", + (string) (len=11) "\treturn nil", + (string) (len=1) "}", + (string) "", + (string) (len=55) "// Set implements the Golang flag.Value interface func.", + (string) (len=42) "func (x *NonASCII) Set(val string) error {", + (string) (len=29) "\tv, err := ParseNonASCII(val)", + (string) (len=7) "\t*x = v", + (string) (len=11) "\treturn err", + (string) (len=1) "}", + (string) "", + (string) (len=56) "// Get implements the Golang flag.Getter interface func.", + (string) (len=38) "func (x *NonASCII) Get() interface{} {", + (string) (len=10) "\treturn *x", + (string) (len=1) "}", + (string) "", + (string) (len=62) "// Type implements the github.com/spf13/pFlag Value interface.", + (string) (len=34) "func (x *NonASCII) Type() string {", + (string) (len=18) "\treturn \"NonASCII\"", + (string) (len=1) "}", + (string) "", + (string) (len=7) "const (", (string) (len=51) "\t// TestHyphen is a Sanitizing of type Test-Hyphen.", (string) (len=29) "\tTestHyphen Sanitizing = iota", (string) (len=54) "\t// XHyphenStart is a Sanitizing of type -HyphenStart.", diff --git a/generator/.snapshots/TestCustomPrefixExampleFile b/generator/.snapshots/TestCustomPrefixExampleFile index e92966fa..531abf35 100644 --- a/generator/.snapshots/TestCustomPrefixExampleFile +++ b/generator/.snapshots/TestCustomPrefixExampleFile @@ -1,4 +1,4 @@ -([]string) (len=3377) { +([]string) (len=3639) { (string) (len=41) "// Code generated by go-enum DO NOT EDIT.", (string) (len=13) "// Version: -", (string) (len=14) "// Revision: -", @@ -2576,6 +2576,268 @@ (string) (len=1) "}", (string) "", (string) (len=7) "const (", + (string) (len=66) "\t// Custom_prefix_Продам is a NonASCII of type Продам.", + (string) (len=50) "\tCustom_prefix_Продам NonASCII = iota + 1114", + (string) (len=54) "\t// Custom_prefix_車庫 is a NonASCII of type 車庫.", + (string) (len=43) "\tCustom_prefix_車庫 NonASCII = iota + 299", + (string) (len=58) "\t// Custom_prefix_Էժան is a NonASCII of type Էժան.", + (string) (len=44) "\tCustom_prefix_Էժան NonASCII = iota + -1", + (string) (len=1) ")", + (string) "", + (string) (len=50) "const _NonASCIIName = \"Продам車庫էժան\"", + (string) "", + (string) (len=39) "var _NonASCIIMap = map[NonASCII]string{", + (string) (len=49) "\tCustom_prefix_Продам: _NonASCIIName[0:12],", + (string) (len=48) "\tCustom_prefix_車庫: _NonASCIIName[12:18],", + (string) (len=48) "\tCustom_prefix_Էժան: _NonASCIIName[18:26],", + (string) (len=1) "}", + (string) "", + (string) (len=44) "// String implements the Stringer interface.", + (string) (len=35) "func (x NonASCII) String() string {", + (string) (len=36) "\tif str, ok := _NonASCIIMap[x]; ok {", + (string) (len=12) "\t\treturn str", + (string) (len=2) "\t}", + (string) (len=38) "\treturn fmt.Sprintf(\"NonASCII(%d)\", x)", + (string) (len=1) "}", + (string) "", + (string) (len=41) "var _NonASCIIValue = map[string]NonASCII{", + (string) (len=67) "\t_NonASCIIName[0:12]: Custom_prefix_Продам,", + (string) (len=67) "\tstrings.ToLower(_NonASCIIName[0:12]): Custom_prefix_Продам,", + (string) (len=61) "\t_NonASCIIName[12:18]: Custom_prefix_車庫,", + (string) (len=61) "\tstrings.ToLower(_NonASCIIName[12:18]): Custom_prefix_車庫,", + (string) (len=63) "\t_NonASCIIName[18:26]: Custom_prefix_Էժան,", + (string) (len=63) "\tstrings.ToLower(_NonASCIIName[18:26]): Custom_prefix_Էժան,", + (string) (len=1) "}", + (string) "", + (string) (len=60) "// ParseNonASCII attempts to convert a string to a NonASCII.", + (string) (len=51) "func ParseNonASCII(name string) (NonASCII, error) {", + (string) (len=39) "\tif x, ok := _NonASCIIValue[name]; ok {", + (string) (len=15) "\t\treturn x, nil", + (string) (len=2) "\t}", + (string) (len=67) "\treturn NonASCII(0), fmt.Errorf(\"%s is not a valid NonASCII\", name)", + (string) (len=1) "}", + (string) "", + (string) (len=35) "func (x NonASCII) Ptr() *NonASCII {", + (string) (len=10) "\treturn &x", + (string) (len=1) "}", + (string) "", + (string) (len=53) "// MarshalText implements the text marshaller method.", + (string) (len=49) "func (x NonASCII) MarshalText() ([]byte, error) {", + (string) (len=31) "\treturn []byte(x.String()), nil", + (string) (len=1) "}", + (string) "", + (string) (len=57) "// UnmarshalText implements the text unmarshaller method.", + (string) (len=53) "func (x *NonASCII) UnmarshalText(text []byte) error {", + (string) (len=21) "\tname := string(text)", + (string) (len=32) "\ttmp, err := ParseNonASCII(name)", + (string) (len=16) "\tif err != nil {", + (string) (len=12) "\t\treturn err", + (string) (len=2) "\t}", + (string) (len=9) "\t*x = tmp", + (string) (len=11) "\treturn nil", + (string) (len=1) "}", + (string) "", + (string) (len=95) "var _NonASCIIErrNilPtr = errors.New(\"value pointer is nil\") // one per type for package clashes", + (string) "", + (string) (len=41) "// Scan implements the Scanner interface.", + (string) (len=56) "func (x *NonASCII) Scan(value interface{}) (err error) {", + (string) (len=18) "\tif value == nil {", + (string) (len=18) "\t\t*x = NonASCII(0)", + (string) (len=8) "\t\treturn", + (string) (len=2) "\t}", + (string) "", + (string) (len=37) "\t// A wider range of scannable types.", + (string) (len=61) "\t// driver.Value values at the top of the list for expediency", + (string) (len=27) "\tswitch v := value.(type) {", + (string) (len=12) "\tcase int64:", + (string) (len=18) "\t\t*x = NonASCII(v)", + (string) (len=13) "\tcase string:", + (string) (len=28) "\t\t*x, err = ParseNonASCII(v)", + (string) (len=17) "\t\tif err != nil {", + (string) (len=47) "\t\t\t// try parsing the integer value as a string", + (string) (len=49) "\t\t\tif val, verr := strconv.Atoi(v); verr == nil {", + (string) (len=32) "\t\t\t\t*x, err = NonASCII(val), nil", + (string) (len=4) "\t\t\t}", + (string) (len=3) "\t\t}", + (string) (len=13) "\tcase []byte:", + (string) (len=36) "\t\t*x, err = ParseNonASCII(string(v))", + (string) (len=17) "\t\tif err != nil {", + (string) (len=47) "\t\t\t// try parsing the integer value as a string", + (string) (len=57) "\t\t\tif val, verr := strconv.Atoi(string(v)); verr == nil {", + (string) (len=32) "\t\t\t\t*x, err = NonASCII(val), nil", + (string) (len=4) "\t\t\t}", + (string) (len=3) "\t\t}", + (string) (len=15) "\tcase NonASCII:", + (string) (len=8) "\t\t*x = v", + (string) (len=10) "\tcase int:", + (string) (len=18) "\t\t*x = NonASCII(v)", + (string) (len=16) "\tcase *NonASCII:", + (string) (len=15) "\t\tif v == nil {", + (string) (len=28) "\t\t\treturn _NonASCIIErrNilPtr", + (string) (len=3) "\t\t}", + (string) (len=9) "\t\t*x = *v", + (string) (len=11) "\tcase uint:", + (string) (len=18) "\t\t*x = NonASCII(v)", + (string) (len=13) "\tcase uint64:", + (string) (len=18) "\t\t*x = NonASCII(v)", + (string) (len=11) "\tcase *int:", + (string) (len=15) "\t\tif v == nil {", + (string) (len=28) "\t\t\treturn _NonASCIIErrNilPtr", + (string) (len=3) "\t\t}", + (string) (len=19) "\t\t*x = NonASCII(*v)", + (string) (len=13) "\tcase *int64:", + (string) (len=15) "\t\tif v == nil {", + (string) (len=28) "\t\t\treturn _NonASCIIErrNilPtr", + (string) (len=3) "\t\t}", + (string) (len=19) "\t\t*x = NonASCII(*v)", + (string) (len=72) "\tcase float64: // json marshals everything as a float64 if it's a number", + (string) (len=18) "\t\t*x = NonASCII(v)", + (string) (len=73) "\tcase *float64: // json marshals everything as a float64 if it's a number", + (string) (len=15) "\t\tif v == nil {", + (string) (len=28) "\t\t\treturn _NonASCIIErrNilPtr", + (string) (len=3) "\t\t}", + (string) (len=19) "\t\t*x = NonASCII(*v)", + (string) (len=12) "\tcase *uint:", + (string) (len=15) "\t\tif v == nil {", + (string) (len=28) "\t\t\treturn _NonASCIIErrNilPtr", + (string) (len=3) "\t\t}", + (string) (len=19) "\t\t*x = NonASCII(*v)", + (string) (len=14) "\tcase *uint64:", + (string) (len=15) "\t\tif v == nil {", + (string) (len=28) "\t\t\treturn _NonASCIIErrNilPtr", + (string) (len=3) "\t\t}", + (string) (len=19) "\t\t*x = NonASCII(*v)", + (string) (len=14) "\tcase *string:", + (string) (len=15) "\t\tif v == nil {", + (string) (len=28) "\t\t\treturn _NonASCIIErrNilPtr", + (string) (len=3) "\t\t}", + (string) (len=29) "\t\t*x, err = ParseNonASCII(*v)", + (string) (len=17) "\t\tif err != nil {", + (string) (len=47) "\t\t\t// try parsing the integer value as a string", + (string) (len=50) "\t\t\tif val, verr := strconv.Atoi(*v); verr == nil {", + (string) (len=32) "\t\t\t\t*x, err = NonASCII(val), nil", + (string) (len=4) "\t\t\t}", + (string) (len=3) "\t\t}", + (string) (len=2) "\t}", + (string) "", + (string) (len=7) "\treturn", + (string) (len=1) "}", + (string) "", + (string) (len=48) "// Value implements the driver Valuer interface.", + (string) (len=49) "func (x NonASCII) Value() (driver.Value, error) {", + (string) (len=23) "\treturn x.String(), nil", + (string) (len=1) "}", + (string) "", + (string) (len=55) "// Set implements the Golang flag.Value interface func.", + (string) (len=42) "func (x *NonASCII) Set(val string) error {", + (string) (len=29) "\tv, err := ParseNonASCII(val)", + (string) (len=7) "\t*x = v", + (string) (len=11) "\treturn err", + (string) (len=1) "}", + (string) "", + (string) (len=56) "// Get implements the Golang flag.Getter interface func.", + (string) (len=38) "func (x *NonASCII) Get() interface{} {", + (string) (len=10) "\treturn *x", + (string) (len=1) "}", + (string) "", + (string) (len=62) "// Type implements the github.com/spf13/pFlag Value interface.", + (string) (len=34) "func (x *NonASCII) Type() string {", + (string) (len=18) "\treturn \"NonASCII\"", + (string) (len=1) "}", + (string) "", + (string) (len=26) "type NullNonASCII struct {", + (string) (len=18) "\tNonASCII NonASCII", + (string) (len=14) "\tValid bool", + (string) (len=14) "\tSet bool", + (string) (len=1) "}", + (string) "", + (string) (len=56) "func NewNullNonASCII(val interface{}) (x NullNonASCII) {", + (string) (len=76) "\tx.Scan(val) // yes, we ignore this error, it will just be an invalid value.", + (string) (len=7) "\treturn", + (string) (len=1) "}", + (string) "", + (string) (len=41) "// Scan implements the Scanner interface.", + (string) (len=60) "func (x *NullNonASCII) Scan(value interface{}) (err error) {", + (string) (len=13) "\tx.Set = true", + (string) (len=18) "\tif value == nil {", + (string) (len=42) "\t\tx.NonASCII, x.Valid = NonASCII(0), false", + (string) (len=8) "\t\treturn", + (string) (len=2) "\t}", + (string) "", + (string) (len=29) "\terr = x.NonASCII.Scan(value)", + (string) (len=23) "\tx.Valid = (err == nil)", + (string) (len=7) "\treturn", + (string) (len=1) "}", + (string) "", + (string) (len=48) "// Value implements the driver Valuer interface.", + (string) (len=53) "func (x NullNonASCII) Value() (driver.Value, error) {", + (string) (len=14) "\tif !x.Valid {", + (string) (len=17) "\t\treturn nil, nil", + (string) (len=2) "\t}", + (string) (len=46) "\t// driver.Value accepts int64 for int values.", + (string) (len=30) "\treturn int64(x.NonASCII), nil", + (string) (len=1) "}", + (string) "", + (string) (len=59) "// MarshalJSON correctly serializes a NullNonASCII to JSON.", + (string) (len=53) "func (n NullNonASCII) MarshalJSON() ([]byte, error) {", + (string) (len=23) "\tconst nullStr = \"null\"", + (string) (len=13) "\tif n.Valid {", + (string) (len=33) "\t\treturn json.Marshal(n.NonASCII)", + (string) (len=2) "\t}", + (string) (len=28) "\treturn []byte(nullStr), nil", + (string) (len=1) "}", + (string) "", + (string) (len=65) "// UnmarshalJSON correctly deserializes a NullNonASCII from JSON.", + (string) (len=54) "func (n *NullNonASCII) UnmarshalJSON(b []byte) error {", + (string) (len=13) "\tn.Set = true", + (string) (len=18) "\tvar x interface{}", + (string) (len=29) "\terr := json.Unmarshal(b, &x)", + (string) (len=16) "\tif err != nil {", + (string) (len=12) "\t\treturn err", + (string) (len=2) "\t}", + (string) (len=16) "\terr = n.Scan(x)", + (string) (len=11) "\treturn err", + (string) (len=1) "}", + (string) "", + (string) (len=29) "type NullNonASCIIStr struct {", + (string) (len=13) "\tNullNonASCII", + (string) (len=1) "}", + (string) "", + (string) (len=62) "func NewNullNonASCIIStr(val interface{}) (x NullNonASCIIStr) {", + (string) (len=76) "\tx.Scan(val) // yes, we ignore this error, it will just be an invalid value.", + (string) (len=7) "\treturn", + (string) (len=1) "}", + (string) "", + (string) (len=48) "// Value implements the driver Valuer interface.", + (string) (len=56) "func (x NullNonASCIIStr) Value() (driver.Value, error) {", + (string) (len=14) "\tif !x.Valid {", + (string) (len=17) "\t\treturn nil, nil", + (string) (len=2) "\t}", + (string) (len=32) "\treturn x.NonASCII.String(), nil", + (string) (len=1) "}", + (string) "", + (string) (len=59) "// MarshalJSON correctly serializes a NullNonASCII to JSON.", + (string) (len=56) "func (n NullNonASCIIStr) MarshalJSON() ([]byte, error) {", + (string) (len=23) "\tconst nullStr = \"null\"", + (string) (len=13) "\tif n.Valid {", + (string) (len=33) "\t\treturn json.Marshal(n.NonASCII)", + (string) (len=2) "\t}", + (string) (len=28) "\treturn []byte(nullStr), nil", + (string) (len=1) "}", + (string) "", + (string) (len=65) "// UnmarshalJSON correctly deserializes a NullNonASCII from JSON.", + (string) (len=57) "func (n *NullNonASCIIStr) UnmarshalJSON(b []byte) error {", + (string) (len=13) "\tn.Set = true", + (string) (len=18) "\tvar x interface{}", + (string) (len=29) "\terr := json.Unmarshal(b, &x)", + (string) (len=16) "\tif err != nil {", + (string) (len=12) "\t\treturn err", + (string) (len=2) "\t}", + (string) (len=16) "\terr = n.Scan(x)", + (string) (len=11) "\treturn err", + (string) (len=1) "}", + (string) "", + (string) (len=7) "const (", (string) (len=65) "\t// Custom_prefix_TestHyphen is a Sanitizing of type Test-Hyphen.", (string) (len=43) "\tCustom_prefix_TestHyphen Sanitizing = iota", (string) (len=67) "\t// Custom_prefix_HyphenStart is a Sanitizing of type -HyphenStart.", diff --git a/generator/.snapshots/TestExampleFile b/generator/.snapshots/TestExampleFile index 66263575..559ddadf 100644 --- a/generator/.snapshots/TestExampleFile +++ b/generator/.snapshots/TestExampleFile @@ -1,4 +1,4 @@ -([]string) (len=2047) { +([]string) (len=2195) { (string) (len=41) "// Code generated by go-enum DO NOT EDIT.", (string) (len=13) "// Version: -", (string) (len=14) "// Revision: -", @@ -1585,6 +1585,154 @@ (string) (len=1) "}", (string) "", (string) (len=7) "const (", + (string) (len=60) "\t// NonASCIIПродам is a NonASCII of type Продам.", + (string) (len=44) "\tNonASCIIПродам NonASCII = iota + 1114", + (string) (len=48) "\t// NonASCII車庫 is a NonASCII of type 車庫.", + (string) (len=37) "\tNonASCII車庫 NonASCII = iota + 299", + (string) (len=52) "\t// NonASCIIԷժան is a NonASCII of type Էժան.", + (string) (len=38) "\tNonASCIIԷժան NonASCII = iota + -1", + (string) (len=1) ")", + (string) "", + (string) (len=50) "const _NonASCIIName = \"Продам車庫էժան\"", + (string) "", + (string) (len=30) "var _NonASCIINames = []string{", + (string) (len=21) "\t_NonASCIIName[0:12],", + (string) (len=22) "\t_NonASCIIName[12:18],", + (string) (len=22) "\t_NonASCIIName[18:26],", + (string) (len=1) "}", + (string) "", + (string) (len=70) "// NonASCIINames returns a list of possible string values of NonASCII.", + (string) (len=31) "func NonASCIINames() []string {", + (string) (len=43) "\ttmp := make([]string, len(_NonASCIINames))", + (string) (len=26) "\tcopy(tmp, _NonASCIINames)", + (string) (len=11) "\treturn tmp", + (string) (len=1) "}", + (string) "", + (string) (len=39) "var _NonASCIIMap = map[NonASCII]string{", + (string) (len=43) "\tNonASCIIПродам: _NonASCIIName[0:12],", + (string) (len=42) "\tNonASCII車庫: _NonASCIIName[12:18],", + (string) (len=42) "\tNonASCIIԷժան: _NonASCIIName[18:26],", + (string) (len=1) "}", + (string) "", + (string) (len=44) "// String implements the Stringer interface.", + (string) (len=35) "func (x NonASCII) String() string {", + (string) (len=36) "\tif str, ok := _NonASCIIMap[x]; ok {", + (string) (len=12) "\t\treturn str", + (string) (len=2) "\t}", + (string) (len=38) "\treturn fmt.Sprintf(\"NonASCII(%d)\", x)", + (string) (len=1) "}", + (string) "", + (string) (len=41) "var _NonASCIIValue = map[string]NonASCII{", + (string) (len=61) "\t_NonASCIIName[0:12]: NonASCIIПродам,", + (string) (len=61) "\tstrings.ToLower(_NonASCIIName[0:12]): NonASCIIПродам,", + (string) (len=55) "\t_NonASCIIName[12:18]: NonASCII車庫,", + (string) (len=55) "\tstrings.ToLower(_NonASCIIName[12:18]): NonASCII車庫,", + (string) (len=57) "\t_NonASCIIName[18:26]: NonASCIIԷժան,", + (string) (len=57) "\tstrings.ToLower(_NonASCIIName[18:26]): NonASCIIԷժան,", + (string) (len=1) "}", + (string) "", + (string) (len=60) "// ParseNonASCII attempts to convert a string to a NonASCII.", + (string) (len=51) "func ParseNonASCII(name string) (NonASCII, error) {", + (string) (len=39) "\tif x, ok := _NonASCIIValue[name]; ok {", + (string) (len=15) "\t\treturn x, nil", + (string) (len=2) "\t}", + (string) (len=121) "\t// Case insensitive parse, do a separate lookup to prevent unnecessary cost of lowercasing a string if we don't need to.", + (string) (len=56) "\tif x, ok := _NonASCIIValue[strings.ToLower(name)]; ok {", + (string) (len=15) "\t\treturn x, nil", + (string) (len=2) "\t}", + (string) (len=113) "\treturn NonASCII(0), fmt.Errorf(\"%s is not a valid NonASCII, try [%s]\", name, strings.Join(_NonASCIINames, \", \"))", + (string) (len=1) "}", + (string) "", + (string) (len=53) "// MarshalText implements the text marshaller method.", + (string) (len=49) "func (x NonASCII) MarshalText() ([]byte, error) {", + (string) (len=31) "\treturn []byte(x.String()), nil", + (string) (len=1) "}", + (string) "", + (string) (len=57) "// UnmarshalText implements the text unmarshaller method.", + (string) (len=53) "func (x *NonASCII) UnmarshalText(text []byte) error {", + (string) (len=21) "\tname := string(text)", + (string) (len=32) "\ttmp, err := ParseNonASCII(name)", + (string) (len=16) "\tif err != nil {", + (string) (len=12) "\t\treturn err", + (string) (len=2) "\t}", + (string) (len=9) "\t*x = tmp", + (string) (len=11) "\treturn nil", + (string) (len=1) "}", + (string) "", + (string) (len=95) "var _NonASCIIErrNilPtr = errors.New(\"value pointer is nil\") // one per type for package clashes", + (string) "", + (string) (len=41) "// Scan implements the Scanner interface.", + (string) (len=56) "func (x *NonASCII) Scan(value interface{}) (err error) {", + (string) (len=18) "\tif value == nil {", + (string) (len=18) "\t\t*x = NonASCII(0)", + (string) (len=8) "\t\treturn", + (string) (len=2) "\t}", + (string) "", + (string) (len=37) "\t// A wider range of scannable types.", + (string) (len=61) "\t// driver.Value values at the top of the list for expediency", + (string) (len=27) "\tswitch v := value.(type) {", + (string) (len=12) "\tcase int64:", + (string) (len=18) "\t\t*x = NonASCII(v)", + (string) (len=13) "\tcase string:", + (string) (len=28) "\t\t*x, err = ParseNonASCII(v)", + (string) (len=13) "\tcase []byte:", + (string) (len=36) "\t\t*x, err = ParseNonASCII(string(v))", + (string) (len=15) "\tcase NonASCII:", + (string) (len=8) "\t\t*x = v", + (string) (len=10) "\tcase int:", + (string) (len=18) "\t\t*x = NonASCII(v)", + (string) (len=16) "\tcase *NonASCII:", + (string) (len=15) "\t\tif v == nil {", + (string) (len=28) "\t\t\treturn _NonASCIIErrNilPtr", + (string) (len=3) "\t\t}", + (string) (len=9) "\t\t*x = *v", + (string) (len=11) "\tcase uint:", + (string) (len=18) "\t\t*x = NonASCII(v)", + (string) (len=13) "\tcase uint64:", + (string) (len=18) "\t\t*x = NonASCII(v)", + (string) (len=11) "\tcase *int:", + (string) (len=15) "\t\tif v == nil {", + (string) (len=28) "\t\t\treturn _NonASCIIErrNilPtr", + (string) (len=3) "\t\t}", + (string) (len=19) "\t\t*x = NonASCII(*v)", + (string) (len=13) "\tcase *int64:", + (string) (len=15) "\t\tif v == nil {", + (string) (len=28) "\t\t\treturn _NonASCIIErrNilPtr", + (string) (len=3) "\t\t}", + (string) (len=19) "\t\t*x = NonASCII(*v)", + (string) (len=72) "\tcase float64: // json marshals everything as a float64 if it's a number", + (string) (len=18) "\t\t*x = NonASCII(v)", + (string) (len=73) "\tcase *float64: // json marshals everything as a float64 if it's a number", + (string) (len=15) "\t\tif v == nil {", + (string) (len=28) "\t\t\treturn _NonASCIIErrNilPtr", + (string) (len=3) "\t\t}", + (string) (len=19) "\t\t*x = NonASCII(*v)", + (string) (len=12) "\tcase *uint:", + (string) (len=15) "\t\tif v == nil {", + (string) (len=28) "\t\t\treturn _NonASCIIErrNilPtr", + (string) (len=3) "\t\t}", + (string) (len=19) "\t\t*x = NonASCII(*v)", + (string) (len=14) "\tcase *uint64:", + (string) (len=15) "\t\tif v == nil {", + (string) (len=28) "\t\t\treturn _NonASCIIErrNilPtr", + (string) (len=3) "\t\t}", + (string) (len=19) "\t\t*x = NonASCII(*v)", + (string) (len=14) "\tcase *string:", + (string) (len=15) "\t\tif v == nil {", + (string) (len=28) "\t\t\treturn _NonASCIIErrNilPtr", + (string) (len=3) "\t\t}", + (string) (len=29) "\t\t*x, err = ParseNonASCII(*v)", + (string) (len=2) "\t}", + (string) "", + (string) (len=7) "\treturn", + (string) (len=1) "}", + (string) "", + (string) (len=48) "// Value implements the driver Valuer interface.", + (string) (len=49) "func (x NonASCII) Value() (driver.Value, error) {", + (string) (len=23) "\treturn x.String(), nil", + (string) (len=1) "}", + (string) "", + (string) (len=7) "const (", (string) (len=61) "\t// SanitizingTestHyphen is a Sanitizing of type Test-Hyphen.", (string) (len=39) "\tSanitizingTestHyphen Sanitizing = iota", (string) (len=63) "\t// SanitizingHyphenStart is a Sanitizing of type -HyphenStart.", diff --git a/generator/.snapshots/TestNoPrefixExampleFile b/generator/.snapshots/TestNoPrefixExampleFile index 4834ad98..c263fa4e 100644 --- a/generator/.snapshots/TestNoPrefixExampleFile +++ b/generator/.snapshots/TestNoPrefixExampleFile @@ -1,4 +1,4 @@ -([]string) (len=1128) { +([]string) (len=1203) { (string) (len=41) "// Code generated by go-enum DO NOT EDIT.", (string) (len=13) "// Version: -", (string) (len=14) "// Revision: -", @@ -888,6 +888,81 @@ (string) (len=1) "}", (string) "", (string) (len=7) "const (", + (string) (len=52) "\t// Продам is a NonASCII of type Продам.", + (string) (len=36) "\tПродам NonASCII = iota + 1114", + (string) (len=40) "\t// 車庫 is a NonASCII of type 車庫.", + (string) (len=29) "\t車庫 NonASCII = iota + 299", + (string) (len=44) "\t// Էժան is a NonASCII of type Էժան.", + (string) (len=30) "\tԷժան NonASCII = iota + -1", + (string) (len=1) ")", + (string) "", + (string) (len=50) "const _NonASCIIName = \"Продам車庫էժան\"", + (string) "", + (string) (len=39) "var _NonASCIIMap = map[NonASCII]string{", + (string) (len=35) "\tПродам: _NonASCIIName[0:12],", + (string) (len=34) "\t車庫: _NonASCIIName[12:18],", + (string) (len=34) "\tԷժան: _NonASCIIName[18:26],", + (string) (len=1) "}", + (string) "", + (string) (len=44) "// String implements the Stringer interface.", + (string) (len=35) "func (x NonASCII) String() string {", + (string) (len=36) "\tif str, ok := _NonASCIIMap[x]; ok {", + (string) (len=12) "\t\treturn str", + (string) (len=2) "\t}", + (string) (len=38) "\treturn fmt.Sprintf(\"NonASCII(%d)\", x)", + (string) (len=1) "}", + (string) "", + (string) (len=41) "var _NonASCIIValue = map[string]NonASCII{", + (string) (len=53) "\t_NonASCIIName[0:12]: Продам,", + (string) (len=53) "\tstrings.ToLower(_NonASCIIName[0:12]): Продам,", + (string) (len=47) "\t_NonASCIIName[12:18]: 車庫,", + (string) (len=47) "\tstrings.ToLower(_NonASCIIName[12:18]): 車庫,", + (string) (len=49) "\t_NonASCIIName[18:26]: Էժան,", + (string) (len=49) "\tstrings.ToLower(_NonASCIIName[18:26]): Էժան,", + (string) (len=1) "}", + (string) "", + (string) (len=60) "// ParseNonASCII attempts to convert a string to a NonASCII.", + (string) (len=51) "func ParseNonASCII(name string) (NonASCII, error) {", + (string) (len=39) "\tif x, ok := _NonASCIIValue[name]; ok {", + (string) (len=15) "\t\treturn x, nil", + (string) (len=2) "\t}", + (string) (len=67) "\treturn NonASCII(0), fmt.Errorf(\"%s is not a valid NonASCII\", name)", + (string) (len=1) "}", + (string) "", + (string) (len=53) "// MarshalText implements the text marshaller method.", + (string) (len=49) "func (x NonASCII) MarshalText() ([]byte, error) {", + (string) (len=31) "\treturn []byte(x.String()), nil", + (string) (len=1) "}", + (string) "", + (string) (len=57) "// UnmarshalText implements the text unmarshaller method.", + (string) (len=53) "func (x *NonASCII) UnmarshalText(text []byte) error {", + (string) (len=21) "\tname := string(text)", + (string) (len=32) "\ttmp, err := ParseNonASCII(name)", + (string) (len=16) "\tif err != nil {", + (string) (len=12) "\t\treturn err", + (string) (len=2) "\t}", + (string) (len=9) "\t*x = tmp", + (string) (len=11) "\treturn nil", + (string) (len=1) "}", + (string) "", + (string) (len=55) "// Set implements the Golang flag.Value interface func.", + (string) (len=42) "func (x *NonASCII) Set(val string) error {", + (string) (len=29) "\tv, err := ParseNonASCII(val)", + (string) (len=7) "\t*x = v", + (string) (len=11) "\treturn err", + (string) (len=1) "}", + (string) "", + (string) (len=56) "// Get implements the Golang flag.Getter interface func.", + (string) (len=38) "func (x *NonASCII) Get() interface{} {", + (string) (len=10) "\treturn *x", + (string) (len=1) "}", + (string) "", + (string) (len=62) "// Type implements the github.com/spf13/pFlag Value interface.", + (string) (len=34) "func (x *NonASCII) Type() string {", + (string) (len=18) "\treturn \"NonASCII\"", + (string) (len=1) "}", + (string) "", + (string) (len=7) "const (", (string) (len=51) "\t// TestHyphen is a Sanitizing of type Test-Hyphen.", (string) (len=29) "\tTestHyphen Sanitizing = iota", (string) (len=54) "\t// XHyphenStart is a Sanitizing of type -HyphenStart.", diff --git a/generator/.snapshots/TestNoPrefixExampleFileWithSnakeToCamel b/generator/.snapshots/TestNoPrefixExampleFileWithSnakeToCamel index e920dd2e..59670ee8 100644 --- a/generator/.snapshots/TestNoPrefixExampleFileWithSnakeToCamel +++ b/generator/.snapshots/TestNoPrefixExampleFileWithSnakeToCamel @@ -1,4 +1,4 @@ -([]string) (len=1128) { +([]string) (len=1203) { (string) (len=41) "// Code generated by go-enum DO NOT EDIT.", (string) (len=13) "// Version: -", (string) (len=14) "// Revision: -", @@ -888,6 +888,81 @@ (string) (len=1) "}", (string) "", (string) (len=7) "const (", + (string) (len=52) "\t// Продам is a NonASCII of type Продам.", + (string) (len=36) "\tПродам NonASCII = iota + 1114", + (string) (len=40) "\t// 車庫 is a NonASCII of type 車庫.", + (string) (len=29) "\t車庫 NonASCII = iota + 299", + (string) (len=44) "\t// Էժան is a NonASCII of type Էժան.", + (string) (len=30) "\tԷժան NonASCII = iota + -1", + (string) (len=1) ")", + (string) "", + (string) (len=50) "const _NonASCIIName = \"Продам車庫էժան\"", + (string) "", + (string) (len=39) "var _NonASCIIMap = map[NonASCII]string{", + (string) (len=35) "\tПродам: _NonASCIIName[0:12],", + (string) (len=34) "\t車庫: _NonASCIIName[12:18],", + (string) (len=34) "\tԷժան: _NonASCIIName[18:26],", + (string) (len=1) "}", + (string) "", + (string) (len=44) "// String implements the Stringer interface.", + (string) (len=35) "func (x NonASCII) String() string {", + (string) (len=36) "\tif str, ok := _NonASCIIMap[x]; ok {", + (string) (len=12) "\t\treturn str", + (string) (len=2) "\t}", + (string) (len=38) "\treturn fmt.Sprintf(\"NonASCII(%d)\", x)", + (string) (len=1) "}", + (string) "", + (string) (len=41) "var _NonASCIIValue = map[string]NonASCII{", + (string) (len=53) "\t_NonASCIIName[0:12]: Продам,", + (string) (len=53) "\tstrings.ToLower(_NonASCIIName[0:12]): Продам,", + (string) (len=47) "\t_NonASCIIName[12:18]: 車庫,", + (string) (len=47) "\tstrings.ToLower(_NonASCIIName[12:18]): 車庫,", + (string) (len=49) "\t_NonASCIIName[18:26]: Էժան,", + (string) (len=49) "\tstrings.ToLower(_NonASCIIName[18:26]): Էժան,", + (string) (len=1) "}", + (string) "", + (string) (len=60) "// ParseNonASCII attempts to convert a string to a NonASCII.", + (string) (len=51) "func ParseNonASCII(name string) (NonASCII, error) {", + (string) (len=39) "\tif x, ok := _NonASCIIValue[name]; ok {", + (string) (len=15) "\t\treturn x, nil", + (string) (len=2) "\t}", + (string) (len=67) "\treturn NonASCII(0), fmt.Errorf(\"%s is not a valid NonASCII\", name)", + (string) (len=1) "}", + (string) "", + (string) (len=53) "// MarshalText implements the text marshaller method.", + (string) (len=49) "func (x NonASCII) MarshalText() ([]byte, error) {", + (string) (len=31) "\treturn []byte(x.String()), nil", + (string) (len=1) "}", + (string) "", + (string) (len=57) "// UnmarshalText implements the text unmarshaller method.", + (string) (len=53) "func (x *NonASCII) UnmarshalText(text []byte) error {", + (string) (len=21) "\tname := string(text)", + (string) (len=32) "\ttmp, err := ParseNonASCII(name)", + (string) (len=16) "\tif err != nil {", + (string) (len=12) "\t\treturn err", + (string) (len=2) "\t}", + (string) (len=9) "\t*x = tmp", + (string) (len=11) "\treturn nil", + (string) (len=1) "}", + (string) "", + (string) (len=55) "// Set implements the Golang flag.Value interface func.", + (string) (len=42) "func (x *NonASCII) Set(val string) error {", + (string) (len=29) "\tv, err := ParseNonASCII(val)", + (string) (len=7) "\t*x = v", + (string) (len=11) "\treturn err", + (string) (len=1) "}", + (string) "", + (string) (len=56) "// Get implements the Golang flag.Getter interface func.", + (string) (len=38) "func (x *NonASCII) Get() interface{} {", + (string) (len=10) "\treturn *x", + (string) (len=1) "}", + (string) "", + (string) (len=62) "// Type implements the github.com/spf13/pFlag Value interface.", + (string) (len=34) "func (x *NonASCII) Type() string {", + (string) (len=18) "\treturn \"NonASCII\"", + (string) (len=1) "}", + (string) "", + (string) (len=7) "const (", (string) (len=51) "\t// TestHyphen is a Sanitizing of type Test-Hyphen.", (string) (len=29) "\tTestHyphen Sanitizing = iota", (string) (len=54) "\t// XHyphenStart is a Sanitizing of type -HyphenStart.", diff --git a/generator/example_test.go b/generator/example_test.go index 4dc49f76..e90e82c1 100644 --- a/generator/example_test.go +++ b/generator/example_test.go @@ -122,3 +122,11 @@ E2P33 = 8454967296 ) */ type Enum64bit uint64 + +// NonASCII +// ENUM( +// Продам = 1114 +// 車庫 = 300 +// էժան = 1 +// ) +type NonASCII int diff --git a/generator/generator.go b/generator/generator.go index a02deb0d..13e36895 100644 --- a/generator/generator.go +++ b/generator/generator.go @@ -407,39 +407,32 @@ func unescapeComment(comment string) string { // identifier = letter { letter | unicode_digit } // where letter can be unicode_letter or '_' func sanitizeValue(value string) string { - // Keep skip value holders if value == skipHolder { return skipHolder } - name := value - + replacedValue := value for k, v := range replacementNames { - name = strings.ReplaceAll(name, k, v) + replacedValue = strings.ReplaceAll(replacedValue, k, v) } - // If the start character is not a unicode letter (this check includes the case of '_') - // then we need to add an exported prefix, so tack on a 'X' at the beginning - if !(unicode.IsLetter(rune(name[0]))) { - name = `X` + name - } + nameBuilder := strings.Builder{} + nameBuilder.Grow(len(replacedValue)) - // Loop through all the runes and remove any that aren't valid. - for i := 0; i < len(name); i++ { - r := rune(name[i]) - if !(unicode.IsLetter(r) || unicode.IsNumber(r) || r == '_') { - if i < len(name) { - name = name[:i] + name[i+1:] - } else { - // At the end of the string, take off the last char - name = name[:i-1] - } - i-- + for i, r := range replacedValue { + // If the start character is not a unicode letter (this check includes the case of '_') + // then we need to add an exported prefix, so tack on a 'X' at the beginning + if i == 0 && !unicode.IsLetter(r) { + nameBuilder.WriteRune('X') + } + + if unicode.IsLetter(r) || unicode.IsNumber(r) || r == '_' { + nameBuilder.WriteRune(r) } } - return name + return nameBuilder.String() } func snakeToCamelCase(value string) string {