diff --git a/example/color_enum.go b/example/color_enum.go index 1c7d7267..fda30814 100755 --- a/example/color_enum.go +++ b/example/color_enum.go @@ -25,7 +25,7 @@ const ( ColorYellow Color = iota + 30 ) -const _ColorName = "BlackWhiteRedGreenBlueGreyYellow" +const _ColorName = "BlackWhiteRedGreenBluegreyyellow" var _ColorMap = map[Color]string{ 0: _ColorName[0:5], diff --git a/example/color_test.go b/example/color_test.go index 4aa72bc3..8268258c 100644 --- a/example/color_test.go +++ b/example/color_test.go @@ -101,7 +101,7 @@ func TestColorUnmarshal(t *testing.T) { }, { name: "grey", - input: `{"color":"Grey"}`, + input: `{"color":"grey"}`, output: &testData{ColorX: ColorGrey}, errorExpected: false, err: nil, @@ -115,7 +115,7 @@ func TestColorUnmarshal(t *testing.T) { }, { name: "yellow", - input: `{"color":"Yellow"}`, + input: `{"color":"yellow"}`, output: &testData{ColorX: ColorYellow}, errorExpected: false, err: nil, @@ -196,14 +196,14 @@ func TestColorMarshal(t *testing.T) { }, { name: "grey", - output: `{"color":"Grey"}`, + output: `{"color":"grey"}`, input: &testData{ColorX: ColorGrey}, errorExpected: false, err: nil, }, { name: "yellow", - output: `{"color":"Yellow"}`, + output: `{"color":"yellow"}`, input: &testData{ColorX: ColorYellow}, errorExpected: false, err: nil, diff --git a/generator/.snapshots/generator-TestExampleFile b/generator/.snapshots/generator-TestExampleFile index 7cfa033b..08098d2d 100644 --- a/generator/.snapshots/generator-TestExampleFile +++ b/generator/.snapshots/generator-TestExampleFile @@ -1,4 +1,4 @@ -([]string) (len=254) { +([]string) (len=309) { (string) (len=28) "// Code generated by go-enum", (string) (len=15) "// DO NOT EDIT!", (string) "", @@ -65,6 +65,61 @@ (string) (len=1) "}", (string) "", (string) (len=7) "const (", + (string) (len=49) "\t// CasesTest_lower is a Cases of type Test_lower", + (string) (len=29) "\tCasesTest_lower Cases = iota", + (string) (len=53) "\t// CasesTest_capital is a Cases of type Test_capital", + (string) (len=18) "\tCasesTest_capital", + (string) (len=71) "\t// CasesAnotherLowerCaseStart is a Cases of type AnotherLowerCaseStart", + (string) (len=27) "\tCasesAnotherLowerCaseStart", + (string) (len=1) ")", + (string) "", + (string) (len=64) "const _CasesName = \"test_lowerTest_capitalanotherLowerCaseStart\"", + (string) "", + (string) (len=33) "var _CasesMap = map[Cases]string{", + (string) (len=21) "\t0: _CasesName[0:10],", + (string) (len=22) "\t1: _CasesName[10:22],", + (string) (len=22) "\t2: _CasesName[22:43],", + (string) (len=1) "}", + (string) "", + (string) (len=32) "func (i Cases) String() string {", + (string) (len=33) "\tif str, ok := _CasesMap[i]; ok {", + (string) (len=12) "\t\treturn str", + (string) (len=2) "\t}", + (string) (len=35) "\treturn fmt.Sprintf(\"Cases(%d)\", i)", + (string) (len=1) "}", + (string) "", + (string) (len=35) "var _CasesValue = map[string]Cases{", + (string) (len=39) "\t_CasesName[0:10]: 0,", + (string) (len=39) "\tstrings.ToLower(_CasesName[0:10]): 0,", + (string) (len=39) "\t_CasesName[10:22]: 1,", + (string) (len=39) "\tstrings.ToLower(_CasesName[10:22]): 1,", + (string) (len=39) "\t_CasesName[22:43]: 2,", + (string) (len=39) "\tstrings.ToLower(_CasesName[22:43]): 2,", + (string) (len=1) "}", + (string) "", + (string) (len=53) "// ParseCases attempts to convert a string to a Cases", + (string) (len=45) "func ParseCases(name string) (Cases, error) {", + (string) (len=36) "\tif x, ok := _CasesValue[name]; ok {", + (string) (len=22) "\t\treturn Cases(x), nil", + (string) (len=2) "\t}", + (string) (len=61) "\treturn Cases(0), fmt.Errorf(\"%s is not a valid Cases\", name)", + (string) (len=1) "}", + (string) "", + (string) (len=47) "func (x *Cases) MarshalText() ([]byte, error) {", + (string) (len=31) "\treturn []byte(x.String()), nil", + (string) (len=1) "}", + (string) "", + (string) (len=50) "func (x *Cases) UnmarshalText(text []byte) error {", + (string) (len=21) "\tname := string(text)", + (string) (len=29) "\ttmp, err := ParseCases(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=7) "const (", (string) (len=39) "\t// ColorBlack is a Color of type Black", (string) (len=24) "\tColorBlack Color = iota", (string) (len=39) "\t// ColorWhite is a Color of type White", @@ -81,7 +136,7 @@ (string) (len=30) "\tColorYellow Color = iota + 29", (string) (len=1) ")", (string) "", - (string) (len=53) "const _ColorName = \"BlackWhiteRedGreenBlueGreyYellow\"", + (string) (len=53) "const _ColorName = \"BlackWhiteRedGreenBluegreyyellow\"", (string) "", (string) (len=33) "var _ColorMap = map[Color]string{", (string) (len=21) "\t0: _ColorName[0:5],", diff --git a/generator/.snapshots/generator-TestNoPrefixExampleFile b/generator/.snapshots/generator-TestNoPrefixExampleFile index f31631bc..a8388e52 100644 --- a/generator/.snapshots/generator-TestNoPrefixExampleFile +++ b/generator/.snapshots/generator-TestNoPrefixExampleFile @@ -1,4 +1,4 @@ -([]string) (len=254) { +([]string) (len=309) { (string) (len=28) "// Code generated by go-enum", (string) (len=15) "// DO NOT EDIT!", (string) "", @@ -65,6 +65,61 @@ (string) (len=1) "}", (string) "", (string) (len=7) "const (", + (string) (len=44) "\t// Test_lower is a Cases of type Test_lower", + (string) (len=24) "\tTest_lower Cases = iota", + (string) (len=48) "\t// Test_capital is a Cases of type Test_capital", + (string) (len=13) "\tTest_capital", + (string) (len=66) "\t// AnotherLowerCaseStart is a Cases of type AnotherLowerCaseStart", + (string) (len=22) "\tAnotherLowerCaseStart", + (string) (len=1) ")", + (string) "", + (string) (len=64) "const _CasesName = \"test_lowerTest_capitalanotherLowerCaseStart\"", + (string) "", + (string) (len=33) "var _CasesMap = map[Cases]string{", + (string) (len=21) "\t0: _CasesName[0:10],", + (string) (len=22) "\t1: _CasesName[10:22],", + (string) (len=22) "\t2: _CasesName[22:43],", + (string) (len=1) "}", + (string) "", + (string) (len=32) "func (i Cases) String() string {", + (string) (len=33) "\tif str, ok := _CasesMap[i]; ok {", + (string) (len=12) "\t\treturn str", + (string) (len=2) "\t}", + (string) (len=35) "\treturn fmt.Sprintf(\"Cases(%d)\", i)", + (string) (len=1) "}", + (string) "", + (string) (len=35) "var _CasesValue = map[string]Cases{", + (string) (len=39) "\t_CasesName[0:10]: 0,", + (string) (len=39) "\tstrings.ToLower(_CasesName[0:10]): 0,", + (string) (len=39) "\t_CasesName[10:22]: 1,", + (string) (len=39) "\tstrings.ToLower(_CasesName[10:22]): 1,", + (string) (len=39) "\t_CasesName[22:43]: 2,", + (string) (len=39) "\tstrings.ToLower(_CasesName[22:43]): 2,", + (string) (len=1) "}", + (string) "", + (string) (len=53) "// ParseCases attempts to convert a string to a Cases", + (string) (len=45) "func ParseCases(name string) (Cases, error) {", + (string) (len=36) "\tif x, ok := _CasesValue[name]; ok {", + (string) (len=22) "\t\treturn Cases(x), nil", + (string) (len=2) "\t}", + (string) (len=61) "\treturn Cases(0), fmt.Errorf(\"%s is not a valid Cases\", name)", + (string) (len=1) "}", + (string) "", + (string) (len=47) "func (x *Cases) MarshalText() ([]byte, error) {", + (string) (len=31) "\treturn []byte(x.String()), nil", + (string) (len=1) "}", + (string) "", + (string) (len=50) "func (x *Cases) UnmarshalText(text []byte) error {", + (string) (len=21) "\tname := string(text)", + (string) (len=29) "\ttmp, err := ParseCases(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=7) "const (", (string) (len=34) "\t// Black is a Color of type Black", (string) (len=19) "\tBlack Color = iota", (string) (len=34) "\t// White is a Color of type White", @@ -81,7 +136,7 @@ (string) (len=25) "\tYellow Color = iota + 29", (string) (len=1) ")", (string) "", - (string) (len=53) "const _ColorName = \"BlackWhiteRedGreenBlueGreyYellow\"", + (string) (len=53) "const _ColorName = \"BlackWhiteRedGreenBluegreyyellow\"", (string) "", (string) (len=33) "var _ColorMap = map[Color]string{", (string) (len=21) "\t0: _ColorName[0:5],", diff --git a/generator/example_test.go b/generator/example_test.go index f42aa818..3144057b 100644 --- a/generator/example_test.go +++ b/generator/example_test.go @@ -31,3 +31,11 @@ type Model int32 ) */ type Soda int64 + +/* ENUM( + test_lower + Test_capital + anotherLowerCaseStart +) +*/ +type Cases int64 diff --git a/generator/generator.go b/generator/generator.go index dee837b4..b85680a3 100644 --- a/generator/generator.go +++ b/generator/generator.go @@ -44,6 +44,7 @@ type Enum struct { // EnumValue holds the individual data for each enum value within the found enum. type EnumValue struct { + RawName string Name string PrefixedName string Value int @@ -203,13 +204,14 @@ func (g *Generator) parseEnum(ts *ast.TypeSpec) (*Enum, error) { fmt.Printf("Ignoring enum with '=' but no value after: %s\n", value) } } - name := strings.Title(strings.TrimSpace(value)) + rawName := strings.TrimSpace(value) + name := strings.Title(rawName) prefixedName := name if name != skipHolder { prefixedName = enum.Prefix + name } - ev := EnumValue{Name: name, PrefixedName: prefixedName, Value: data} + ev := EnumValue{Name: name, RawName: rawName, PrefixedName: prefixedName, Value: data} enum.Values = append(enum.Values, ev) data++ } diff --git a/generator/template_funcs.go b/generator/template_funcs.go index 935b8cbd..2dd8d4e0 100644 --- a/generator/template_funcs.go +++ b/generator/template_funcs.go @@ -8,7 +8,7 @@ import ( func Stringify(e Enum) (ret string, err error) { for _, val := range e.Values { if val.Name != skipHolder { - ret = ret + val.Name + ret = ret + val.RawName } } return diff --git a/update-snapshots.sh b/update-snapshots.sh new file mode 100755 index 00000000..68909b9f --- /dev/null +++ b/update-snapshots.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +UPDATE_SNAPSHOTS=true make test \ No newline at end of file