Skip to content

Commit

Permalink
Merge pull request #10 from abice/CapitalStrings
Browse files Browse the repository at this point in the history
Made output strings use raw values rather than titled strings.
  • Loading branch information
abice authored Nov 3, 2017
2 parents 08e9df8 + 83d48cb commit 26c36a3
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 12 deletions.
2 changes: 1 addition & 1 deletion example/color_enum.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions example/color_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
59 changes: 57 additions & 2 deletions generator/.snapshots/generator-TestExampleFile
Original file line number Diff line number Diff line change
@@ -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) "",
Expand Down Expand Up @@ -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",
Expand All @@ -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],",
Expand Down
59 changes: 57 additions & 2 deletions generator/.snapshots/generator-TestNoPrefixExampleFile
Original file line number Diff line number Diff line change
@@ -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) "",
Expand Down Expand Up @@ -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",
Expand All @@ -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],",
Expand Down
8 changes: 8 additions & 0 deletions generator/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,11 @@ type Model int32
)
*/
type Soda int64

/* ENUM(
test_lower
Test_capital
anotherLowerCaseStart
)
*/
type Cases int64
6 changes: 4 additions & 2 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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++
}
Expand Down
2 changes: 1 addition & 1 deletion generator/template_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions update-snapshots.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

UPDATE_SNAPSHOTS=true make test

0 comments on commit 26c36a3

Please sign in to comment.