Made output strings use raw values rather than titled strings.

This commit is contained in:
Alex Bice committed 2017-11-03 13:44:40 -07:00
1 parent 08e9df8a26
commit 83d48cbaa4
8 files changed
+135 -12

No files matched your search

+1 -1
View File
@@ -25,7 +25,7 @@ const (
ColorYellow Color = iota + 30
)
const _ColorName = "BlackWhiteRedGreenBlueGreyYellow"
const _ColorName = "BlackWhiteRedGreenBluegreyyellow"
var _ColorMap = map[Color]string{
0: _ColorName[0:5],
+4 -4
View File
@@ -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,
+57 -2
View File
@@ -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],",
@@ -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],",
+8
View File
@@ -31,3 +31,11 @@ type Model int32
)
*/
type Soda int64
/* ENUM(
test_lower
Test_capital
anotherLowerCaseStart
)
*/
type Cases int64
+4 -2
View File
@@ -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++
}
+1 -1
View File
@@ -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
+3
View File
@@ -0,0 +1,3 @@
#!/bin/sh
UPDATE_SNAPSHOTS=true make test