Skip to content

Commit

Permalink
Add non-ASCII enum values support (#97)
Browse files Browse the repository at this point in the history
* Add non-ASCII enum values support

* fix go1.18 tests

Co-authored-by: Egor Gartman <[email protected]>
  • Loading branch information
LastPossum and Egor Gartman authored May 12, 2022
1 parent 7e2e8bc commit 2485d8f
Show file tree
Hide file tree
Showing 10 changed files with 1,150 additions and 29 deletions.
264 changes: 263 additions & 1 deletion generator/.snapshots/Test118CustomPrefixExampleFile-og
Original file line number Diff line number Diff line change
@@ -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: -",
Expand Down Expand Up @@ -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.",
Expand Down
Loading

0 comments on commit 2485d8f

Please sign in to comment.