Skip to content

Commit

Permalink
Add case invariant parsing (#45)
Browse files Browse the repository at this point in the history
* Add case insensitive parsing
  • Loading branch information
abice authored Feb 16, 2021
1 parent a1ca18f commit 5ed55ce
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 12 deletions.
2 changes: 1 addition & 1 deletion example/example.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:generate ../bin/go-enum -f=$GOFILE --marshal --lower --flag --names
//go:generate ../bin/go-enum -f=$GOFILE --marshal --nocase --flag --names

package example

Expand Down
8 changes: 8 additions & 0 deletions example/example_enum.go

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

23 changes: 19 additions & 4 deletions example/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var makeTests = []struct {
output *makeTest
errorExpected bool
err error
caseChanged bool
}{
{
name: "toyota",
Expand Down Expand Up @@ -91,6 +92,14 @@ var makeTests = []struct {
errorExpected: false,
err: nil,
},
{
name: "AUDI",
input: `{"make":"AUDI"}`,
output: &makeTest{M: MakeAudi},
errorExpected: false,
err: nil,
caseChanged: true,
},
{
name: "bmw",
input: `{"make":"BMW"}`,
Expand Down Expand Up @@ -130,10 +139,13 @@ func TestMakeUnmarshal(t *testing.T) {
require.NoError(tt, err, "failed unmarshalling the json.")
assert.Equal(tt, test.output.M, x.M)

// Marshal back
raw, err := json.Marshal(test.output)
require.NoError(tt, err, "failed marshalling back to json")
require.JSONEq(tt, test.input, string(raw), "json didn't match")
// Values won't be exactly the same, so we just validate that it was unmarshalled correctly.
if !test.caseChanged {
// Marshal back
raw, err := json.Marshal(test.output)
require.NoError(tt, err, "failed marshalling back to json")
require.JSONEq(tt, test.input, string(raw), "json didn't match")
}
} else {
require.Error(tt, err)
assert.EqualError(tt, err, test.err.Error())
Expand Down Expand Up @@ -175,6 +187,9 @@ func TestNoZeroValues(t *testing.T) {
tmp, _ := ParseNoZeros("ppps")
assert.Equal(tt, NoZerosPpps, tmp)

tmp, _ = ParseNoZeros("PppS")
assert.Equal(tt, NoZerosPpps, tmp)

val := map[string]*NoZeros{}

err = json.Unmarshal([]byte(`{"nz":"pppps"}`), &val)
Expand Down
46 changes: 45 additions & 1 deletion generator/.snapshots/generator-TestExampleFile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
([]string) (len=1272) {
([]string) (len=1316) {
(string) (len=28) "// Code generated by go-enum",
(string) (len=15) "// DO NOT EDIT!",
(string) "",
Expand Down Expand Up @@ -62,6 +62,10 @@
(string) (len=37) "\tif x, ok := _AnimalValue[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=54) "\tif x, ok := _AnimalValue[strings.ToLower(name)]; ok {",
(string) (len=15) "\t\treturn x, nil",
(string) (len=2) "\t}",
(string) (len=107) "\treturn Animal(0), fmt.Errorf(\"%s is not a valid Animal, try [%s]\", name, strings.Join(_AnimalNames, \", \"))",
(string) (len=1) "}",
(string) "",
Expand Down Expand Up @@ -160,6 +164,10 @@
(string) (len=36) "\tif x, ok := _CasesValue[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=53) "\tif x, ok := _CasesValue[strings.ToLower(name)]; ok {",
(string) (len=15) "\t\treturn x, nil",
(string) (len=2) "\t}",
(string) (len=104) "\treturn Cases(0), fmt.Errorf(\"%s is not a valid Cases, try [%s]\", name, strings.Join(_CasesNames, \", \"))",
(string) (len=1) "}",
(string) "",
Expand Down Expand Up @@ -282,6 +290,10 @@
(string) (len=36) "\tif x, ok := _ColorValue[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=53) "\tif x, ok := _ColorValue[strings.ToLower(name)]; ok {",
(string) (len=15) "\t\treturn x, nil",
(string) (len=2) "\t}",
(string) (len=104) "\treturn Color(0), fmt.Errorf(\"%s is not a valid Color, try [%s]\", name, strings.Join(_ColorNames, \", \"))",
(string) (len=1) "}",
(string) "",
Expand Down Expand Up @@ -405,6 +417,10 @@
(string) (len=47) "\tif x, ok := _ColorWithCommentValue[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=64) "\tif x, ok := _ColorWithCommentValue[strings.ToLower(name)]; ok {",
(string) (len=15) "\t\treturn x, nil",
(string) (len=2) "\t}",
(string) (len=137) "\treturn ColorWithComment(0), fmt.Errorf(\"%s is not a valid ColorWithComment, try [%s]\", name, strings.Join(_ColorWithCommentNames, \", \"))",
(string) (len=1) "}",
(string) "",
Expand Down Expand Up @@ -528,6 +544,10 @@
(string) (len=48) "\tif x, ok := _ColorWithComment2Value[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=65) "\tif x, ok := _ColorWithComment2Value[strings.ToLower(name)]; ok {",
(string) (len=15) "\t\treturn x, nil",
(string) (len=2) "\t}",
(string) (len=140) "\treturn ColorWithComment2(0), fmt.Errorf(\"%s is not a valid ColorWithComment2, try [%s]\", name, strings.Join(_ColorWithComment2Names, \", \"))",
(string) (len=1) "}",
(string) "",
Expand Down Expand Up @@ -670,6 +690,10 @@
(string) (len=48) "\tif x, ok := _ColorWithComment3Value[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=65) "\tif x, ok := _ColorWithComment3Value[strings.ToLower(name)]; ok {",
(string) (len=15) "\t\treturn x, nil",
(string) (len=2) "\t}",
(string) (len=140) "\treturn ColorWithComment3(0), fmt.Errorf(\"%s is not a valid ColorWithComment3, try [%s]\", name, strings.Join(_ColorWithComment3Names, \", \"))",
(string) (len=1) "}",
(string) "",
Expand Down Expand Up @@ -811,6 +835,10 @@
(string) (len=48) "\tif x, ok := _ColorWithComment4Value[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=65) "\tif x, ok := _ColorWithComment4Value[strings.ToLower(name)]; ok {",
(string) (len=15) "\t\treturn x, nil",
(string) (len=2) "\t}",
(string) (len=140) "\treturn ColorWithComment4(0), fmt.Errorf(\"%s is not a valid ColorWithComment4, try [%s]\", name, strings.Join(_ColorWithComment4Names, \", \"))",
(string) (len=1) "}",
(string) "",
Expand Down Expand Up @@ -913,6 +941,10 @@
(string) (len=36) "\tif x, ok := _ModelValue[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=53) "\tif x, ok := _ModelValue[strings.ToLower(name)]; ok {",
(string) (len=15) "\t\treturn x, nil",
(string) (len=2) "\t}",
(string) (len=104) "\treturn Model(0), fmt.Errorf(\"%s is not a valid Model, try [%s]\", name, strings.Join(_ModelNames, \", \"))",
(string) (len=1) "}",
(string) "",
Expand Down Expand Up @@ -1035,6 +1067,10 @@
(string) (len=41) "\tif x, ok := _SanitizingValue[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=58) "\tif x, ok := _SanitizingValue[strings.ToLower(name)]; ok {",
(string) (len=15) "\t\treturn x, nil",
(string) (len=2) "\t}",
(string) (len=119) "\treturn Sanitizing(0), fmt.Errorf(\"%s is not a valid Sanitizing, try [%s]\", name, strings.Join(_SanitizingNames, \", \"))",
(string) (len=1) "}",
(string) "",
Expand Down Expand Up @@ -1133,6 +1169,10 @@
(string) (len=35) "\tif x, ok := _SodaValue[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=52) "\tif x, ok := _SodaValue[strings.ToLower(name)]; ok {",
(string) (len=15) "\t\treturn x, nil",
(string) (len=2) "\t}",
(string) (len=101) "\treturn Soda(0), fmt.Errorf(\"%s is not a valid Soda, try [%s]\", name, strings.Join(_SodaNames, \", \"))",
(string) (len=1) "}",
(string) "",
Expand Down Expand Up @@ -1225,6 +1265,10 @@
(string) (len=43) "\tif x, ok := _StartNotZeroValue[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=60) "\tif x, ok := _StartNotZeroValue[strings.ToLower(name)]; ok {",
(string) (len=15) "\t\treturn x, nil",
(string) (len=2) "\t}",
(string) (len=125) "\treturn StartNotZero(0), fmt.Errorf(\"%s is not a valid StartNotZero, try [%s]\", name, strings.Join(_StartNotZeroNames, \", \"))",
(string) (len=1) "}",
(string) "",
Expand Down
Loading

0 comments on commit 5ed55ce

Please sign in to comment.