From 00beba327920dfc7238e68e38998bab2ea782d4e Mon Sep 17 00:00:00 2001 From: Alex Bice Date: Tue, 16 Feb 2021 11:02:34 -0700 Subject: [PATCH 1/3] Add case invariant parsing --- example/example.go | 2 +- example/example_enum.go | 10 ++++++++++ example/example_test.go | 20 ++++++++++++++++---- generator/assets/assets.go | 6 +++--- generator/enum.tmpl | 7 ++++++- generator/generator.go | 9 +++++++++ main.go | 11 ++++++++++- 7 files changed, 55 insertions(+), 10 deletions(-) diff --git a/example/example.go b/example/example.go index ed92570c..1f60dec1 100644 --- a/example/example.go +++ b/example/example.go @@ -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 diff --git a/example/example_enum.go b/example/example_enum.go index 6c152ee6..8e5ef5b9 100644 --- a/example/example_enum.go +++ b/example/example_enum.go @@ -128,6 +128,11 @@ func ParseMake(name string) (Make, error) { if x, ok := _MakeValue[name]; ok { return x, nil } + // Case Invariant Parse enabled, do a separate lookup. + // This is a separate lookup to remove unnecessary cost of lowercasing a string if we don't need to. + if x, ok := _MakeValue[strings.ToLower(name)]; ok { + return x, nil + } return Make(0), fmt.Errorf("%s is not a valid Make, try [%s]", name, strings.Join(_MakeNames, ", ")) } @@ -234,6 +239,11 @@ func ParseNoZeros(name string) (NoZeros, error) { if x, ok := _NoZerosValue[name]; ok { return x, nil } + // Case Invariant Parse enabled, do a separate lookup. + // This is a separate lookup to remove unnecessary cost of lowercasing a string if we don't need to. + if x, ok := _NoZerosValue[strings.ToLower(name)]; ok { + return x, nil + } return NoZeros(0), fmt.Errorf("%s is not a valid NoZeros, try [%s]", name, strings.Join(_NoZerosNames, ", ")) } diff --git a/example/example_test.go b/example/example_test.go index 1bd29efa..80a1c12b 100644 --- a/example/example_test.go +++ b/example/example_test.go @@ -34,6 +34,7 @@ var makeTests = []struct { output *makeTest errorExpected bool err error + caseChanged bool }{ { name: "toyota", @@ -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"}`, @@ -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()) diff --git a/generator/assets/assets.go b/generator/assets/assets.go index 82af3431..a8f45b49 100644 --- a/generator/assets/assets.go +++ b/generator/assets/assets.go @@ -1,6 +1,6 @@ // Code generated by go-bindata. DO NOT EDIT. // sources: -// enum.tmpl (3.311kB) +// enum.tmpl (3.575kB) package assets @@ -69,7 +69,7 @@ func (fi bindataFileInfo) Sys() interface{} { return nil } -var _enumTmpl = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xb4\x56\x5f\x6f\xdb\x36\x10\x7f\xb6\x3e\xc5\x55\x48\x00\x29\x73\xe4\x0c\x7b\xeb\xe0\xa7\xb6\x0b\x3a\x20\x4d\x81\x64\x7b\x09\x82\x80\x91\x4e\x36\x11\x89\x52\x49\xda\xb5\xa1\xf1\xbb\x0f\x47\x52\x32\xad\xc8\xe9\x1e\xd6\x17\xc3\xe2\xfd\xff\xdd\xef\x8e\xec\xba\x4b\x28\xb0\xe4\x02\x21\x5e\x23\x2b\x50\xc6\xc6\x44\x8b\x05\x7c\x68\x0a\x84\x15\x0a\x94\x4c\x63\x01\xcf\x7b\x58\x35\x97\x28\x36\x35\x09\x3f\xde\xc2\x97\xdb\x7b\xf8\xf4\xf1\xf3\xfd\xbb\x28\x6a\x59\xfe\xc2\x56\x08\x5d\x97\xf9\xbf\xc6\x44\x11\xaf\xdb\x46\x6a\x48\x22\x00\x80\xb8\xac\x75\x1c\xa5\x51\xd7\xa1\x28\xe0\x92\xe4\x61\x64\xf2\x4b\x71\xf3\x46\x28\x32\x21\xd9\x19\x1d\x7e\x61\x35\xc2\xfb\x25\x64\xf4\x91\xd9\x2f\x32\xb6\xf2\x2d\x93\x8a\x64\x05\xcf\x35\xc4\x15\x53\xba\x29\x4b\x85\x3a\x86\x2b\xaf\x04\x92\x89\x15\xc2\x99\xfc\x2c\x0a\xdc\xcd\xc9\xa4\xda\x04\xfe\xfe\xa6\x4f\x05\xc6\x44\x33\xeb\x91\x7c\xdc\x5a\x1f\xa4\xd3\x56\x9b\xfc\xe5\xd8\xb1\x8b\xf9\x0f\x94\x5c\x2a\x0d\xc6\x74\x1d\x9c\x35\x83\x81\xda\x3c\xfb\x10\xce\x73\x1f\xd8\x07\x00\x5e\x02\x7e\xeb\x35\x6c\x2d\xf1\x53\x6c\xcc\x62\x01\x77\x2f\xbc\x6d\xb1\x00\x2b\xea\x3a\xac\x14\xda\xf3\xae\xf3\xda\x5f\x25\x96\x7c\x87\x05\x59\x19\x03\x5c\x01\x23\x61\x0f\x91\x31\xd0\x94\xa0\xf7\x2d\x1e\x4c\xdc\xb9\x05\xbc\x2f\x90\x97\x7d\xf4\x0f\x4d\x5d\xa3\xd0\x24\x08\xc3\x04\xc7\xa4\xef\x4c\xa9\x7f\xa7\x12\x39\x54\xe5\x4b\xbd\xb2\xa8\x84\x89\x2d\x81\x37\x9a\x39\x45\x81\x70\x35\x20\x66\x0c\xfc\x02\x01\x82\x43\xb2\x0e\x00\xaf\x1f\x36\x25\xd4\x7c\x1d\xe2\xa4\xb7\xb3\x27\xdb\x1d\x72\x60\xfb\x77\xdc\x52\xf7\xc7\x93\xca\x55\x9c\x12\x3b\x41\x63\xdd\x56\x4c\x23\xc4\x4a\x4b\x2e\x56\x28\x63\xc8\xa8\x97\x34\x01\x5f\x99\x54\xd8\x75\x07\x5e\x1a\x03\x4c\x93\x89\x56\xa0\x1b\xc8\x1b\xb1\x45\xa9\x81\x81\x33\xa6\x33\x6a\x59\x68\x10\x95\x1b\x91\x4f\x79\x4a\x04\x91\xc3\x19\xa6\x90\x1c\x0b\xe7\x80\x52\x36\x32\x85\x2e\x9a\xf1\x12\x76\x73\x68\x5e\xa8\xbe\xa7\x63\x35\xcb\xc0\x07\x72\xf4\xf8\x3b\x69\x74\xd1\x6c\x26\x51\x6f\xa4\x20\x13\xc1\xab\x68\x66\xbb\xcc\x4b\xc8\x48\x4b\xd9\x99\xe9\x55\x46\xf9\x5c\xa5\x73\x28\x6b\x9d\x7d\xa2\xc8\x65\x12\x9f\x2b\xa2\xa0\x68\xa8\xbe\x2d\xab\x78\x01\xe3\x1c\xb5\xdc\xc3\xc3\xb9\x7a\x8c\xe7\x40\xde\xe7\xbe\x1a\x95\xfd\xd9\x70\x91\x8c\x72\xa5\x5f\x35\x87\x78\x0e\x71\x9a\x7a\xea\x11\x03\xfe\xc7\x8c\x7c\x1e\x69\x48\x6c\xbb\x84\x88\x66\x59\xcd\xa4\x5a\xb3\x0a\xdc\xea\xbb\x71\x5f\xf7\xb8\xd3\xc0\xeb\xb6\x42\x9a\x09\x05\x7a\x8d\xa0\xe9\xcc\x6b\x57\x28\xa1\x46\xbd\x6e\x0a\xd7\xc8\x64\x37\x8a\x99\x86\x9e\x92\x14\x92\x87\xc7\xe7\xbd\xc6\xb0\x81\xbe\x38\x27\x48\x76\xd9\x9d\x45\x29\x49\x53\xd7\x22\xc7\xb5\xbf\x44\xfd\x83\x8c\x36\xe2\x74\x4e\x17\xe3\xa4\x8e\xdc\x25\xd6\xde\xc5\x4f\x5d\x62\x94\x97\xf0\x7b\xd7\x75\xcd\x2a\xa5\xd1\x4c\xd7\xad\x4d\x9e\x24\xa7\x68\x9b\x5a\x56\x92\xd2\xbb\x25\xd5\x10\x12\x0f\xa5\xb4\xac\xbb\xd8\xc1\x12\x74\xdd\x0e\xf5\xbb\x5a\xfb\x5d\xd5\x77\x45\x7d\xeb\x3b\x72\x97\x33\x31\x2e\x9c\xce\x04\x4a\xe0\x42\xa3\x2c\x59\x8e\xd9\xe9\x92\x49\x37\x71\xcb\x7f\x50\xef\x4c\x50\xef\x96\x49\x08\x86\x2e\x8a\x66\xea\x3b\xd7\xf9\x1a\xb6\x54\xab\x5b\x7d\x09\xed\x57\xdb\xb5\x9c\xa9\x5e\xf3\x7d\x34\x73\x60\x2d\x61\xeb\x05\x0e\xcc\x40\xe0\x41\xdc\xa6\x5e\x41\xf0\x8a\xa4\x16\x85\x57\xb4\x3e\xa0\xe5\x67\xf4\xa7\xc3\x4e\x00\xbb\xfb\x6a\x84\x70\x21\xf9\x16\xa5\x93\x4d\xe2\x3c\x86\xd9\x6a\x12\xd1\x9d\xa5\xbb\x05\x27\xe8\x7e\xe0\xf9\xfc\x44\xeb\xcb\x8a\xad\xfa\xde\xe3\x2b\xce\x5f\x37\x15\x13\x2b\x20\x25\x7f\xd3\x0e\xc9\x01\xe5\xf6\x16\x13\x50\x13\x11\x86\xe5\x7a\x60\xc0\x9b\x10\x6f\x59\x95\x7a\x00\xb7\x51\x88\xab\x83\xef\xfa\xed\x1c\xaf\x51\xeb\x10\xc1\x1f\x25\x79\x8d\xb4\x2e\x02\xa6\x06\xd8\x5d\xec\x7c\xcc\x7b\xba\xed\x47\x41\x57\x5c\xaf\x37\xcf\x59\xde\xd4\x0b\xd5\x96\xbf\xfe\xb6\x68\xff\x20\x20\x47\x18\xbd\x11\x99\x9c\x26\x69\x7f\x67\x1d\xa2\xc6\xa3\x6d\x7a\xdc\x32\xff\xe7\xe8\x49\x37\xdc\x99\xc3\xb3\x6e\x62\xe9\xc3\x92\x3c\xfb\x70\xbc\xdc\xbb\x77\x19\x50\x80\x9e\x09\xee\x6e\x32\x86\x26\x74\xea\xda\xb0\x33\x24\x58\x3d\x58\xfb\x0b\x7a\x4a\xd5\x15\x43\x6f\xa7\x8a\x2b\x4d\x4f\xa6\xb6\x51\x8a\x3f\x57\xfd\x34\xbb\x49\x57\x24\x39\xb6\xf7\x90\x4d\x38\x4d\x52\x78\x78\x3c\xe0\xa5\xeb\x96\x38\x54\xb3\x17\x4c\xfa\xf3\x39\x54\x38\x7d\xe7\xd1\x6d\x97\x37\xed\x3e\xb1\x23\x3e\xa9\x31\xb4\x80\x06\xd7\x3e\x6a\x87\x17\xf4\x04\x24\x37\xac\xb5\x80\x40\xcd\xda\x10\x4f\x0b\x89\x1b\xba\x57\x7b\xd4\x37\xea\xbf\x0c\x78\x3f\xb6\x01\x41\x78\x49\x1f\x27\x1e\x21\x37\xac\x7d\xd8\xbd\x7a\x7f\x28\xed\xf6\x91\xff\xa4\x6b\xfc\xae\x95\x5c\xe8\x32\x19\xf1\x2c\x39\x2f\xd2\x78\x0e\xbb\x34\x9a\x2e\xd7\x11\xdb\x16\x4c\x57\x60\x50\x72\x56\x35\xdf\x51\xda\x75\x1b\x72\xf4\xdf\x00\x00\x00\xff\xff\xd3\x7c\x46\xc2\xef\x0c\x00\x00") +var _enumTmpl = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xb4\x57\x5f\x6f\xdb\x36\x10\x7f\xb6\x3e\xc5\x55\x48\x31\xa9\x53\xe5\x0c\x7b\xeb\xe0\xa7\xb6\x0b\x3a\xac\x4d\x81\x78\x7b\x09\x82\x80\x96\x4e\x36\x61\x89\x54\x49\x5a\xb1\xa1\xe9\xbb\x0f\x47\x52\xb2\xac\xd8\x69\x1f\xb6\x97\xc0\xe2\xfd\xff\xdd\xef\x78\x4c\xdb\xbe\x85\x1c\x0b\x2e\x10\xc2\x0d\xb2\x1c\x55\xd8\x75\xc1\x7c\x0e\xef\x65\x8e\xb0\x46\x81\x8a\x19\xcc\x61\x75\x80\xb5\x7c\x8b\x62\x57\x91\xf0\xc3\x2d\x7c\xb9\x5d\xc2\xc7\x0f\x9f\x96\xaf\x82\xa0\x66\xd9\x96\xad\x11\xda\x36\xf5\x3f\xbb\x2e\x08\x78\x55\x4b\x65\x20\x0a\x00\x00\xc2\xa2\x32\x61\x10\x07\x6d\x8b\x22\x87\xb7\x24\x1f\x47\x26\xbf\x14\x37\x93\x42\x93\x09\xc9\xae\xe8\xf0\x0b\xab\x10\xde\x2d\x20\xa5\x8f\xd4\x7e\x91\xb1\x95\x37\x4c\x69\x92\xe5\x3c\x33\x10\x96\x4c\x1b\x59\x14\x1a\x4d\x08\xd7\x5e\x09\x14\x13\x6b\x84\x2b\xf5\x49\xe4\xb8\x4f\xc8\xa4\xdc\x8d\xfc\xfd\x4d\x9f\x1a\xba\x2e\x98\x59\x8f\xe4\xe3\xd6\xfa\x20\x9d\xba\xdc\x65\xdb\x53\xc7\x2e\xe6\x3f\x50\x70\xa5\x0d\x74\x5d\xdb\xc2\x95\x1c\x0c\xf4\x6e\xe5\x43\x38\xcf\x7d\x60\x1f\x00\x78\x01\xf8\xad\xd7\xb0\xb5\x84\x8f\x61\xd7\xcd\xe7\x70\xb7\xe5\x75\x8d\x39\x58\x51\xdb\x62\xa9\xd1\x9e\xb7\xad\xd7\xfe\xaa\xb0\xe0\x7b\xcc\xc9\xaa\xeb\x80\x6b\x60\x24\xec\x21\xea\x3a\x90\x05\x98\x43\x8d\x47\x13\x77\x6e\x01\xef\x0b\xe4\x45\x1f\xfd\xbd\xac\x2a\x14\x86\x04\xe3\x30\xa3\x63\xd2\x77\xa6\xd4\xbf\x4b\x89\x1c\xab\xf2\xa5\x5e\x5b\x54\xc6\x89\x2d\x80\x4b\xc3\x9c\xa2\x40\xb8\x1e\x10\xeb\x3a\xf8\x19\x46\x08\x0e\xc9\x3a\x00\xbc\xfe\xb8\x29\x63\xcd\xe7\x21\x2e\x7a\xbb\x7a\xb4\xdd\x21\x07\xb6\x7f\xa7\x2d\x75\x3f\x3c\xa9\x5c\xc5\x31\xb1\x13\x0c\x56\x75\xc9\x0c\x42\xa8\x8d\xe2\x62\x8d\x2a\x84\x94\x7a\x49\x13\xf0\x95\x29\x8d\x6d\x7b\xe4\x65\xd7\x01\x33\x64\x62\x34\x18\x09\x99\x14\x0d\x2a\x03\x0c\x9c\x31\x9d\x51\xcb\xc6\x06\x41\xb1\x13\xd9\x39\x4f\x91\x20\x72\x38\xc3\x18\xa2\x53\x61\x02\xa8\x94\x54\x31\xb4\xc1\x8c\x17\xb0\x4f\x40\x6e\xa9\xbe\xc7\x53\x35\xcb\xc0\x7b\x72\xf4\xf0\x1b\x69\xb4\xc1\x6c\xa6\xd0\xec\x94\x20\x13\xc1\xcb\x60\xd6\xb5\x2d\x2f\x20\x15\x32\x63\x1a\xc1\x73\xe1\x3d\xfd\xfe\x24\x1a\xa6\x38\x13\xc6\x65\x07\x28\xd8\xaa\xc4\x3c\x81\x9c\xaa\xd0\x58\x33\xba\x15\xa0\x94\x72\xbb\xab\x53\x6b\xb7\xdc\x70\xed\x78\x39\x11\x53\xe5\x0a\x2b\xd9\x20\xec\x84\xc0\x0c\xb5\x66\xea\x00\x99\xd4\x86\x48\x5b\xca\x27\x54\x19\xd3\x84\xd1\x00\x16\x2f\xe0\x09\x21\x97\xe2\x27\x03\x02\x31\x07\x23\xd3\x1f\xa8\xd6\x59\xeb\x74\x29\xff\x24\xaf\x16\xc6\xf8\xa5\xf2\xfb\x86\xcf\x3c\x12\xac\x42\x6d\xef\x8e\x5e\x77\xd2\x97\xeb\x38\x81\xa2\x32\xe9\x47\xea\x40\x11\x85\xaf\x6d\xc9\x42\x52\x9f\x1b\x56\xf2\x1c\xa6\xbd\x32\xea\x00\xf7\xaf\xf5\x43\x98\x00\x79\x4f\xa0\xcf\xf1\x0f\xc9\x45\x34\xa9\x82\xfe\xea\x04\xc2\x04\xc2\x38\xf6\x23\x48\x93\xf0\x1f\x66\xe4\xf3\x88\xc7\x03\x6e\x2f\x63\xc2\x3c\xad\x98\xd2\x1b\x56\x82\x5b\x01\x9f\xdd\xd7\x12\xf7\x06\x78\x55\x97\x48\x77\x83\x06\xb3\x41\x30\x74\xe6\xb5\x4b\x54\x50\xa1\xd9\xc8\xdc\x11\x3a\xda\x4f\x62\xc6\x63\x4f\x51\x0c\xd1\xfd\xc3\xea\x60\x70\x4c\x64\x5f\x9c\x13\x44\xfb\xf4\xce\xa2\x14\xc5\xb1\xeb\x95\x9b\xb9\xbf\x44\xf5\x9d\x8c\x76\xe2\x72\x4e\x6f\xa6\x49\x9d\xb8\x8b\xac\xbd\x8b\x1f\xbb\xc4\x28\x2f\xe1\xf7\x8f\xeb\x9a\x55\x8a\x83\x99\xa9\x6a\x9b\x3c\x49\x2e\x8d\x6f\x6c\xf9\x4a\x4a\xaf\x16\x54\xc3\x98\x81\xa8\x54\x30\xeb\x82\xd9\x9b\x3d\x2c\xc0\x54\xf5\x50\xbf\xab\xb5\xbf\xb3\xfb\xae\xe8\x6f\x7d\x47\xee\x32\x26\xa6\x85\xd3\x99\x40\x05\x5c\x18\x54\x05\xcb\x30\xbd\x5c\x32\xe9\x46\x6e\x09\x0e\xea\x6d\x37\xaa\xb7\x61\x0a\x46\x97\x4f\x10\xcc\xf4\x13\x37\xd9\x06\x1a\xaa\xd5\xad\x80\x88\xf6\x8c\xed\x9a\xbd\x37\x9c\xe6\xbb\x60\xe6\xc0\x5a\x40\xe3\x05\x0e\xcc\x91\xc0\x83\xd8\xc4\x5e\x41\xf0\x92\xa4\x16\x85\x67\xb4\x3e\xa2\xe5\x86\x35\xf8\xdf\x61\x27\x80\xdd\xde\x9e\x20\x9c\x2b\xde\xa0\x72\xb2\xb3\x38\x4f\x61\xb6\x9a\x44\x74\x67\xe9\x5e\x03\x67\xe8\x7e\xe4\x79\x72\xa1\xf5\x45\xc9\xd6\x7d\xef\xf1\x19\xe7\x6f\x64\xc9\xc4\x1a\x48\xc9\xbf\x38\x86\xe4\x80\x72\x7b\x89\x09\x68\x88\x08\xc3\x92\x39\x32\xe0\x45\x88\x1b\x56\xc6\x1e\xc0\x26\x18\xe3\xea\xe0\xbb\x79\x39\xc7\x1b\x34\x66\x8c\xe0\xf7\x92\xbc\x41\xba\x2e\x46\x4c\x1d\x61\xf7\x66\xef\x63\x2e\xe9\xd5\x33\x09\xba\xe6\x66\xb3\x5b\xa5\x99\xac\xe6\xba\x2e\x7e\xf9\x75\x5e\xff\x4e\x40\x4e\x30\x7a\x21\x32\x39\x8d\xe2\x7e\x1d\x1d\xa3\x86\x93\xdb\xf4\xb4\x65\xfe\xc7\xc9\xd3\x76\x78\x3b\x0c\xcf\xdb\x33\x97\x3e\x2c\xc8\xb3\x0f\xc7\x8b\x83\x7b\x9f\x02\x05\xe8\x99\xe0\x76\x53\xd7\xd1\x84\x9e\x5b\x1b\x76\x86\x04\xab\x06\x6b\xff\x50\x39\xa7\xea\x8a\xa1\x5d\x5d\x72\xb7\x85\x6b\xa9\x35\x5f\x95\xfd\x34\xbb\x49\xd7\x24\x39\xb5\xf7\x90\x9d\x71\x1a\xc5\x70\xff\x70\xc4\xcb\x54\x35\x71\xa8\x62\x5b\x8c\xfa\xf3\x04\x4a\x3c\xbf\xf3\x68\xdb\x65\xb2\x3e\x44\x76\xc4\xcf\x6a\x0c\x2d\xa0\xc1\xb5\x8f\xfb\xe1\x3f\x89\x33\x90\x7c\x66\xb5\x05\x04\x2a\x56\x8f\xf1\xb4\x90\xdc\xf9\x47\xc6\xe4\x1e\xf5\x8d\xfa\x91\x01\xef\xc7\x76\x44\x10\x5e\xd0\xc7\x85\xe7\xc9\x67\x56\xdf\xef\x9f\x3d\x44\xb4\x71\xf7\x91\xff\xa4\x35\x7e\x57\x2b\x2e\x4c\x11\x4d\x78\x16\xbd\xce\xe3\x30\x81\x7d\x1c\x9c\x2f\xd7\x11\xdb\x16\x4c\x2b\x70\x54\x72\xda\xbf\xaf\xec\x03\x6f\xe0\xe8\xbf\x01\x00\x00\xff\xff\xfb\x28\xb0\xc2\xf7\x0d\x00\x00") func enumTmplBytes() ([]byte, error) { return bindataRead( @@ -85,7 +85,7 @@ func enumTmpl() (*asset, error) { } info := bindataFileInfo{name: "enum.tmpl", size: 0, mode: os.FileMode(0), modTime: time.Unix(0, 0)} - a := &asset{bytes: bytes, info: info, digest: [32]uint8{0xad, 0x1f, 0xae, 0xf9, 0x5b, 0xfe, 0x8a, 0x8, 0x9e, 0x42, 0xe6, 0xa8, 0xe3, 0xd7, 0xea, 0xf5, 0xb5, 0x31, 0xe1, 0xda, 0x3c, 0x17, 0xb5, 0x72, 0x95, 0x9e, 0x9c, 0xc2, 0xdb, 0x2f, 0x8f, 0xdd}} + a := &asset{bytes: bytes, info: info, digest: [32]uint8{0xa6, 0x47, 0x65, 0xcd, 0x71, 0x59, 0x7d, 0x63, 0x89, 0x4, 0xcb, 0x37, 0xff, 0xc3, 0xf, 0x89, 0x78, 0x9f, 0x37, 0x16, 0xcd, 0xb9, 0xc4, 0x34, 0x1d, 0x91, 0xc9, 0x68, 0x67, 0x61, 0x7d, 0xf2}} return a, nil } diff --git a/generator/enum.tmpl b/generator/enum.tmpl index 1278c8d9..9d4a6819 100644 --- a/generator/enum.tmpl +++ b/generator/enum.tmpl @@ -29,7 +29,12 @@ const ( func Parse{{.enum.Name}}(name string) ({{.enum.Name}}, error) { if x, ok := _{{.enum.Name}}Value[name]; ok { return x, nil - } + }{{if .nocase }} + // Case Invariant Parse enabled, do a separate lookup. + // This is a separate lookup to remove unnecessary cost of lowercasing a string if we don't need to. + if x, ok := _{{.enum.Name}}Value[strings.ToLower(name)]; ok { + return x, nil + }{{- end}} {{if .names -}} return {{.enum.Name}}(0), fmt.Errorf("%s is not a valid {{.enum.Name}}, try [%s]", name, strings.Join(_{{.enum.Name}}Names, ", ")) {{- else -}} diff --git a/generator/generator.go b/generator/generator.go index 58b9a575..ac6e94d5 100644 --- a/generator/generator.go +++ b/generator/generator.go @@ -33,6 +33,7 @@ type Generator struct { fileSet *token.FileSet noPrefix bool lowercaseLookup bool + caseInvariant bool marshal bool sql bool flag bool @@ -98,6 +99,13 @@ func (g *Generator) WithLowercaseVariant() *Generator { return g } +// WithLowercaseVariant is used to change the enum const values generated to not have the enum on them. +func (g *Generator) WithCaseInvariantParse() *Generator { + g.lowercaseLookup = true + g.caseInvariant = true + return g +} + // WithMarshal is used to add marshalling to the enum func (g *Generator) WithMarshal() *Generator { g.marshal = true @@ -180,6 +188,7 @@ func (g *Generator) Generate(f *ast.File) ([]byte, error) { "enum": enum, "name": name, "lowercase": g.lowercaseLookup, + "nocase": g.caseInvariant, "marshal": g.marshal, "sql": g.sql, "flag": g.flag, diff --git a/main.go b/main.go index 30e4a2d9..fa31a983 100644 --- a/main.go +++ b/main.go @@ -17,6 +17,7 @@ type rootT struct { FileNames cli.StringSlice NoPrefix bool Lowercase bool + NoCase bool Marshal bool SQL bool Flag bool @@ -35,7 +36,7 @@ func main() { app := &cli.App{ Name: "go-enum", - Usage: "An enum generator for go", + Usage: "An enum generator for go", HideHelpCommand: true, Flags: []cli.Flag{ &cli.StringSliceFlag{ @@ -55,6 +56,11 @@ func main() { Usage: "Adds lowercase variants of the enum strings for lookup.", Destination: &argv.Lowercase, }, + &cli.BoolFlag{ + Name: "nocase", + Usage: "Adds case insensitive parsing to the enumeration (forces lower flag).", + Destination: &argv.NoCase, + }, &cli.BoolFlag{ Name: "marshal", Usage: "Adds text (and inherently json) marshalling functions.", @@ -97,6 +103,9 @@ func main() { if argv.Lowercase { g.WithLowercaseVariant() } + if argv.NoCase { + g.WithCaseInvariantParse() + } if argv.Marshal { g.WithMarshal() } From 3f956dcc716c967d0fbae03c4fd160c1e3ed05ab Mon Sep 17 00:00:00 2001 From: Alex Bice Date: Tue, 16 Feb 2021 12:10:47 -0700 Subject: [PATCH 2/3] Change name to insensitive --- example/example_enum.go | 6 ++---- generator/assets/assets.go | 6 +++--- generator/enum.tmpl | 3 +-- generator/generator.go | 8 ++++---- main.go | 2 +- 5 files changed, 11 insertions(+), 14 deletions(-) diff --git a/example/example_enum.go b/example/example_enum.go index 8e5ef5b9..d4fd9374 100644 --- a/example/example_enum.go +++ b/example/example_enum.go @@ -128,8 +128,7 @@ func ParseMake(name string) (Make, error) { if x, ok := _MakeValue[name]; ok { return x, nil } - // Case Invariant Parse enabled, do a separate lookup. - // This is a separate lookup to remove unnecessary cost of lowercasing a string if we don't need to. + // Case insensitive parse, do a separate lookup to prevent unnecessary cost of lowercasing a string if we don't need to. if x, ok := _MakeValue[strings.ToLower(name)]; ok { return x, nil } @@ -239,8 +238,7 @@ func ParseNoZeros(name string) (NoZeros, error) { if x, ok := _NoZerosValue[name]; ok { return x, nil } - // Case Invariant Parse enabled, do a separate lookup. - // This is a separate lookup to remove unnecessary cost of lowercasing a string if we don't need to. + // Case insensitive parse, do a separate lookup to prevent unnecessary cost of lowercasing a string if we don't need to. if x, ok := _NoZerosValue[strings.ToLower(name)]; ok { return x, nil } diff --git a/generator/assets/assets.go b/generator/assets/assets.go index a8f45b49..24bd82d6 100644 --- a/generator/assets/assets.go +++ b/generator/assets/assets.go @@ -1,6 +1,6 @@ // Code generated by go-bindata. DO NOT EDIT. // sources: -// enum.tmpl (3.575kB) +// enum.tmpl (3.539kB) package assets @@ -69,7 +69,7 @@ func (fi bindataFileInfo) Sys() interface{} { return nil } -var _enumTmpl = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xb4\x57\x5f\x6f\xdb\x36\x10\x7f\xb6\x3e\xc5\x55\x48\x31\xa9\x53\xe5\x0c\x7b\xeb\xe0\xa7\xb6\x0b\x3a\xac\x4d\x81\x78\x7b\x09\x82\x80\x96\x4e\x36\x61\x89\x54\x49\x5a\xb1\xa1\xe9\xbb\x0f\x47\x52\xb2\xac\xd8\x69\x1f\xb6\x97\xc0\xe2\xfd\xff\xdd\xef\x78\x4c\xdb\xbe\x85\x1c\x0b\x2e\x10\xc2\x0d\xb2\x1c\x55\xd8\x75\xc1\x7c\x0e\xef\x65\x8e\xb0\x46\x81\x8a\x19\xcc\x61\x75\x80\xb5\x7c\x8b\x62\x57\x91\xf0\xc3\x2d\x7c\xb9\x5d\xc2\xc7\x0f\x9f\x96\xaf\x82\xa0\x66\xd9\x96\xad\x11\xda\x36\xf5\x3f\xbb\x2e\x08\x78\x55\x4b\x65\x20\x0a\x00\x00\xc2\xa2\x32\x61\x10\x07\x6d\x8b\x22\x87\xb7\x24\x1f\x47\x26\xbf\x14\x37\x93\x42\x93\x09\xc9\xae\xe8\xf0\x0b\xab\x10\xde\x2d\x20\xa5\x8f\xd4\x7e\x91\xb1\x95\x37\x4c\x69\x92\xe5\x3c\x33\x10\x96\x4c\x1b\x59\x14\x1a\x4d\x08\xd7\x5e\x09\x14\x13\x6b\x84\x2b\xf5\x49\xe4\xb8\x4f\xc8\xa4\xdc\x8d\xfc\xfd\x4d\x9f\x1a\xba\x2e\x98\x59\x8f\xe4\xe3\xd6\xfa\x20\x9d\xba\xdc\x65\xdb\x53\xc7\x2e\xe6\x3f\x50\x70\xa5\x0d\x74\x5d\xdb\xc2\x95\x1c\x0c\xf4\x6e\xe5\x43\x38\xcf\x7d\x60\x1f\x00\x78\x01\xf8\xad\xd7\xb0\xb5\x84\x8f\x61\xd7\xcd\xe7\x70\xb7\xe5\x75\x8d\x39\x58\x51\xdb\x62\xa9\xd1\x9e\xb7\xad\xd7\xfe\xaa\xb0\xe0\x7b\xcc\xc9\xaa\xeb\x80\x6b\x60\x24\xec\x21\xea\x3a\x90\x05\x98\x43\x8d\x47\x13\x77\x6e\x01\xef\x0b\xe4\x45\x1f\xfd\xbd\xac\x2a\x14\x86\x04\xe3\x30\xa3\x63\xd2\x77\xa6\xd4\xbf\x4b\x89\x1c\xab\xf2\xa5\x5e\x5b\x54\xc6\x89\x2d\x80\x4b\xc3\x9c\xa2\x40\xb8\x1e\x10\xeb\x3a\xf8\x19\x46\x08\x0e\xc9\x3a\x00\xbc\xfe\xb8\x29\x63\xcd\xe7\x21\x2e\x7a\xbb\x7a\xb4\xdd\x21\x07\xb6\x7f\xa7\x2d\x75\x3f\x3c\xa9\x5c\xc5\x31\xb1\x13\x0c\x56\x75\xc9\x0c\x42\xa8\x8d\xe2\x62\x8d\x2a\x84\x94\x7a\x49\x13\xf0\x95\x29\x8d\x6d\x7b\xe4\x65\xd7\x01\x33\x64\x62\x34\x18\x09\x99\x14\x0d\x2a\x03\x0c\x9c\x31\x9d\x51\xcb\xc6\x06\x41\xb1\x13\xd9\x39\x4f\x91\x20\x72\x38\xc3\x18\xa2\x53\x61\x02\xa8\x94\x54\x31\xb4\xc1\x8c\x17\xb0\x4f\x40\x6e\xa9\xbe\xc7\x53\x35\xcb\xc0\x7b\x72\xf4\xf0\x1b\x69\xb4\xc1\x6c\xa6\xd0\xec\x94\x20\x13\xc1\xcb\x60\xd6\xb5\x2d\x2f\x20\x15\x32\x63\x1a\xc1\x73\xe1\x3d\xfd\xfe\x24\x1a\xa6\x38\x13\xc6\x65\x07\x28\xd8\xaa\xc4\x3c\x81\x9c\xaa\xd0\x58\x33\xba\x15\xa0\x94\x72\xbb\xab\x53\x6b\xb7\xdc\x70\xed\x78\x39\x11\x53\xe5\x0a\x2b\xd9\x20\xec\x84\xc0\x0c\xb5\x66\xea\x00\x99\xd4\x86\x48\x5b\xca\x27\x54\x19\xd3\x84\xd1\x00\x16\x2f\xe0\x09\x21\x97\xe2\x27\x03\x02\x31\x07\x23\xd3\x1f\xa8\xd6\x59\xeb\x74\x29\xff\x24\xaf\x16\xc6\xf8\xa5\xf2\xfb\x86\xcf\x3c\x12\xac\x42\x6d\xef\x8e\x5e\x77\xd2\x97\xeb\x38\x81\xa2\x32\xe9\x47\xea\x40\x11\x85\xaf\x6d\xc9\x42\x52\x9f\x1b\x56\xf2\x1c\xa6\xbd\x32\xea\x00\xf7\xaf\xf5\x43\x98\x00\x79\x4f\xa0\xcf\xf1\x0f\xc9\x45\x34\xa9\x82\xfe\xea\x04\xc2\x04\xc2\x38\xf6\x23\x48\x93\xf0\x1f\x66\xe4\xf3\x88\xc7\x03\x6e\x2f\x63\xc2\x3c\xad\x98\xd2\x1b\x56\x82\x5b\x01\x9f\xdd\xd7\x12\xf7\x06\x78\x55\x97\x48\x77\x83\x06\xb3\x41\x30\x74\xe6\xb5\x4b\x54\x50\xa1\xd9\xc8\xdc\x11\x3a\xda\x4f\x62\xc6\x63\x4f\x51\x0c\xd1\xfd\xc3\xea\x60\x70\x4c\x64\x5f\x9c\x13\x44\xfb\xf4\xce\xa2\x14\xc5\xb1\xeb\x95\x9b\xb9\xbf\x44\xf5\x9d\x8c\x76\xe2\x72\x4e\x6f\xa6\x49\x9d\xb8\x8b\xac\xbd\x8b\x1f\xbb\xc4\x28\x2f\xe1\xf7\x8f\xeb\x9a\x55\x8a\x83\x99\xa9\x6a\x9b\x3c\x49\x2e\x8d\x6f\x6c\xf9\x4a\x4a\xaf\x16\x54\xc3\x98\x81\xa8\x54\x30\xeb\x82\xd9\x9b\x3d\x2c\xc0\x54\xf5\x50\xbf\xab\xb5\xbf\xb3\xfb\xae\xe8\x6f\x7d\x47\xee\x32\x26\xa6\x85\xd3\x99\x40\x05\x5c\x18\x54\x05\xcb\x30\xbd\x5c\x32\xe9\x46\x6e\x09\x0e\xea\x6d\x37\xaa\xb7\x61\x0a\x46\x97\x4f\x10\xcc\xf4\x13\x37\xd9\x06\x1a\xaa\xd5\xad\x80\x88\xf6\x8c\xed\x9a\xbd\x37\x9c\xe6\xbb\x60\xe6\xc0\x5a\x40\xe3\x05\x0e\xcc\x91\xc0\x83\xd8\xc4\x5e\x41\xf0\x92\xa4\x16\x85\x67\xb4\x3e\xa2\xe5\x86\x35\xf8\xdf\x61\x27\x80\xdd\xde\x9e\x20\x9c\x2b\xde\xa0\x72\xb2\xb3\x38\x4f\x61\xb6\x9a\x44\x74\x67\xe9\x5e\x03\x67\xe8\x7e\xe4\x79\x72\xa1\xf5\x45\xc9\xd6\x7d\xef\xf1\x19\xe7\x6f\x64\xc9\xc4\x1a\x48\xc9\xbf\x38\x86\xe4\x80\x72\x7b\x89\x09\x68\x88\x08\xc3\x92\x39\x32\xe0\x45\x88\x1b\x56\xc6\x1e\xc0\x26\x18\xe3\xea\xe0\xbb\x79\x39\xc7\x1b\x34\x66\x8c\xe0\xf7\x92\xbc\x41\xba\x2e\x46\x4c\x1d\x61\xf7\x66\xef\x63\x2e\xe9\xd5\x33\x09\xba\xe6\x66\xb3\x5b\xa5\x99\xac\xe6\xba\x2e\x7e\xf9\x75\x5e\xff\x4e\x40\x4e\x30\x7a\x21\x32\x39\x8d\xe2\x7e\x1d\x1d\xa3\x86\x93\xdb\xf4\xb4\x65\xfe\xc7\xc9\xd3\x76\x78\x3b\x0c\xcf\xdb\x33\x97\x3e\x2c\xc8\xb3\x0f\xc7\x8b\x83\x7b\x9f\x02\x05\xe8\x99\xe0\x76\x53\xd7\xd1\x84\x9e\x5b\x1b\x76\x86\x04\xab\x06\x6b\xff\x50\x39\xa7\xea\x8a\xa1\x5d\x5d\x72\xb7\x85\x6b\xa9\x35\x5f\x95\xfd\x34\xbb\x49\xd7\x24\x39\xb5\xf7\x90\x9d\x71\x1a\xc5\x70\xff\x70\xc4\xcb\x54\x35\x71\xa8\x62\x5b\x8c\xfa\xf3\x04\x4a\x3c\xbf\xf3\x68\xdb\x65\xb2\x3e\x44\x76\xc4\xcf\x6a\x0c\x2d\xa0\xc1\xb5\x8f\xfb\xe1\x3f\x89\x33\x90\x7c\x66\xb5\x05\x04\x2a\x56\x8f\xf1\xb4\x90\xdc\xf9\x47\xc6\xe4\x1e\xf5\x8d\xfa\x91\x01\xef\xc7\x76\x44\x10\x5e\xd0\xc7\x85\xe7\xc9\x67\x56\xdf\xef\x9f\x3d\x44\xb4\x71\xf7\x91\xff\xa4\x35\x7e\x57\x2b\x2e\x4c\x11\x4d\x78\x16\xbd\xce\xe3\x30\x81\x7d\x1c\x9c\x2f\xd7\x11\xdb\x16\x4c\x2b\x70\x54\x72\xda\xbf\xaf\xec\x03\x6f\xe0\xe8\xbf\x01\x00\x00\xff\xff\xfb\x28\xb0\xc2\xf7\x0d\x00\x00") +var _enumTmpl = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xb4\x57\x5f\x6f\xdb\x36\x10\x7f\xb6\x3e\xc5\x55\x48\x31\x29\x53\xe4\x0c\x7b\xeb\xe0\xa7\xb4\x0b\x3a\x2c\x4d\x81\x64\x7b\x09\x82\x80\x91\x4e\x36\x61\x89\x54\x49\x5a\xb1\xa1\xe9\xbb\x0f\x47\x52\xb2\xac\xd8\x69\x1f\xb6\x97\xc0\xe2\xfd\xff\xdd\xef\xc8\x4b\xdb\x5e\x40\x8e\x05\x17\x08\xe1\x0a\x59\x8e\x2a\xec\xba\x60\x3e\x87\x2b\x99\x23\x2c\x51\xa0\x62\x06\x73\x78\xde\xc1\x52\x5e\xa0\xd8\x54\x24\xfc\x78\x0b\x5f\x6e\xef\xe1\xd3\xc7\xcf\xf7\xef\x82\xa0\x66\xd9\x9a\x2d\x11\xda\x36\xf5\x3f\xbb\x2e\x08\x78\x55\x4b\x65\x20\x0a\x00\x00\xc2\xa2\x32\x61\x10\x07\x6d\x8b\x22\x87\x0b\x92\x8f\x23\x93\x5f\x8a\x9b\x49\xa1\xc9\x84\x64\x67\x74\xf8\x85\x55\x08\x1f\x16\x90\xd2\x47\x6a\xbf\xc8\xd8\xca\x1b\xa6\x34\xc9\x72\x9e\x19\x08\x4b\xa6\x8d\x2c\x0a\x8d\x26\x84\x4b\xaf\x04\x8a\x89\x25\xc2\x99\xfa\x2c\x72\xdc\x26\x64\x52\x6e\x46\xfe\xfe\xa6\x4f\x0d\x5d\x17\xcc\xac\x47\xf2\x71\x6b\x7d\x90\x4e\x5d\x6e\xb2\xf5\xa1\x63\x17\xf3\x1f\x28\xb8\xd2\x06\xba\xae\x6d\xe1\x4c\x0e\x06\x7a\xf3\xec\x43\x38\xcf\x7d\x60\x1f\x00\x78\x01\xf8\xad\xd7\xb0\xb5\x84\x4f\x61\xd7\xcd\xe7\x70\xb7\xe6\x75\x8d\x39\x58\x51\xdb\x62\xa9\xd1\x9e\xb7\xad\xd7\xfe\xaa\xb0\xe0\x5b\xcc\xc9\xaa\xeb\x80\x6b\x60\x24\xec\x21\xea\x3a\x90\x05\x98\x5d\x8d\x7b\x13\x77\x6e\x01\xef\x0b\xe4\x45\x1f\xfd\x4a\x56\x15\x0a\x43\x82\x71\x98\xd1\x31\xe9\x3b\x53\xea\xdf\xa9\x44\xf6\x55\xf9\x52\x2f\x2d\x2a\xe3\xc4\x16\xc0\xa5\x61\x4e\x51\x20\x5c\x0e\x88\x75\x1d\xfc\x0c\x23\x04\x87\x64\x1d\x00\x5e\x7f\xdc\x94\xb1\xe6\xeb\x10\x27\xbd\x9d\x3d\xd9\xee\x90\x03\xdb\xbf\xc3\x96\xba\x1f\x9e\x54\xae\xe2\x98\xd8\x09\x06\xab\xba\x64\x06\x21\xd4\x46\x71\xb1\x44\x15\x42\x4a\xbd\xa4\x09\xf8\xca\x94\xc6\xb6\xdd\xf3\xb2\xeb\x80\x19\x32\x31\x1a\x8c\x84\x4c\x8a\x06\x95\x01\x06\xce\x98\xce\xa8\x65\x63\x83\xa0\xd8\x88\xec\x98\xa7\x48\x10\x39\x9c\x61\x0c\xd1\xa1\x30\x01\x54\x4a\xaa\x18\xda\x60\xc6\x0b\xd8\x26\x20\xd7\x54\xdf\xd3\xa1\x9a\x65\xe0\x03\x39\x7a\xfc\x8d\x34\xda\x60\x36\x53\x68\x36\x4a\x90\x89\xe0\x65\x30\xeb\xda\x96\x17\x90\x0a\x99\x31\x8d\xe0\xb9\x70\x45\xbf\xb9\xd0\x28\x34\x37\xbc\x41\xa8\x29\xbf\x04\x72\xca\x5f\x63\xcd\xe8\x3e\x80\x52\xca\xf5\xa6\xa6\xa2\x6a\x85\x0d\x0a\x03\x1b\x21\x30\x43\xad\x99\xda\x41\x26\xb5\x21\x42\x96\xf2\x05\x55\xc6\x34\xd5\x3f\x00\xc1\x0b\x78\x41\xc8\xa5\xf8\xc9\x80\x40\xcc\xc1\xc8\xf4\x07\x2a\x71\xd6\x3a\xbd\x97\x7f\x92\x57\x0b\x51\xfc\x56\x69\x7d\x33\x67\xbe\x4a\x56\xa1\xb6\xf7\x42\xaf\x3b\xc1\xfc\x32\x4e\xa0\xa8\x4c\xfa\x89\xd0\x2d\xa2\xf0\xbd\xa6\x31\x13\x92\x7a\xd8\xb0\x92\xe7\x30\xed\x83\x51\x3b\x78\x78\xaf\x1f\xc3\x04\xc8\x7b\x02\x7d\x8e\x7f\x48\x2e\xa2\x49\x15\xf4\x57\x27\x10\x26\x10\xc6\xb1\x1f\x2f\x62\xf9\x7f\x98\x91\xcf\x23\x1e\x0f\xaf\xbd\x68\x09\xf3\xb4\x62\x4a\xaf\x58\x09\xee\x7a\xbf\x71\x5f\xf7\xb8\x35\xc0\xab\xba\x44\x9a\x7b\x0d\x66\x85\x60\xe8\xcc\x6b\x97\xa8\xa0\x42\xb3\x92\xb9\x23\x6b\xb4\x9d\xc4\x8c\xc7\x9e\xa2\x18\xa2\x87\xc7\xe7\x9d\xc1\x31\x49\x7d\x71\x4e\x10\x6d\xd3\x3b\x8b\x52\x14\xc7\xae\x57\x6e\x9e\xfe\x12\xd5\x77\x32\xda\x88\xd3\x39\x9d\x4f\x93\x3a\x70\x17\x59\x7b\x17\x3f\x76\x89\x51\x5e\xc2\xbf\x2d\xae\x6b\x56\x29\x0e\x66\xa6\xaa\x6d\xf2\x24\x39\x35\x9a\xb1\xe5\x2b\x29\xbd\x5b\x50\x0d\x63\x06\xa2\x52\xc1\xac\x0b\x66\xe7\x5b\x58\x80\xa9\xea\xa1\x7e\x57\x6b\x7f\x1f\xf7\x5d\xd1\xdf\xfa\x8e\xdc\x65\x4c\x4c\x0b\xa7\x33\x81\x0a\xb8\x30\xa8\x0a\x96\x61\x7a\xba\x64\xd2\x8d\xdc\x03\x37\xa8\xb7\xdd\xa8\xde\x86\x29\x18\x5d\x2c\x41\x30\xd3\x2f\xdc\x64\x2b\x68\xa8\x56\x77\xbd\x47\xf4\x86\xd8\xae\xd9\x3b\xc1\x69\x7e\x08\x66\x0e\xac\x05\x34\x5e\xe0\xc0\x1c\x09\x3c\x88\x4d\xec\x15\x04\x2f\x49\x6a\x51\x78\x45\xeb\x3d\x5a\x6e\x58\x83\xff\x1d\x76\x02\xd8\xbd\xc9\x13\x84\x73\xc5\x1b\x54\x4e\x76\x14\xe7\x29\xcc\x56\x93\x88\xee\x2c\xdd\x4b\x7f\x84\xee\x7b\x9e\x27\x27\x5a\x5f\x94\x6c\xd9\xf7\x1e\x5f\x71\xfe\x5a\x96\x4c\x2c\x81\x94\xfc\x36\x31\x24\x07\x94\xdb\x5b\x4c\x40\x43\x44\x18\x1e\x90\x3d\x03\xde\x84\xb8\x61\x65\xec\x01\x6c\x82\x31\xae\x0e\xbe\xeb\xb7\x73\xbc\x46\x63\xc6\x08\x7e\x2f\xc9\x6b\xa4\xeb\x62\xc4\xd4\x11\x76\xe7\x5b\x1f\xf3\x9e\x36\x9a\x49\xd0\x25\x37\xab\xcd\x73\x9a\xc9\x6a\xae\xeb\xe2\x97\x5f\xe7\xf5\xef\x04\xe4\x04\xa3\x37\x22\x93\xd3\x28\xee\x9f\xa3\x7d\xd4\x70\x72\x9b\x1e\xb6\xcc\xff\x38\x58\x5b\x87\xbd\x60\x58\x5d\x8f\x5c\xfa\xb0\x20\xcf\x3e\x1c\x2f\x76\x6e\xf7\x04\x0a\xd0\x33\xc1\xbd\x4d\x5d\x47\x13\x7a\xec\xd9\xb0\x33\x24\x58\x35\x58\xfb\x25\xe4\x98\xaa\x2b\x86\xf6\xc3\x92\xbb\x57\xb8\x96\x5a\xf3\xe7\xb2\x9f\x66\x37\xe9\x9a\x24\x87\xf6\x1e\xb2\x23\x4e\xa3\x18\x1e\x1e\xf7\x78\x99\xaa\x26\x0e\x55\x6c\x8d\x51\x7f\x9e\x40\x89\xc7\xdf\x3c\x7a\xed\x32\x59\xef\x22\x3b\xe2\x47\x35\x86\x16\xd0\xe0\xda\xc5\x7d\xf8\x2f\xe1\x08\x24\x37\xac\xb6\x80\x40\xc5\xea\x31\x9e\x16\x92\x3b\xbf\x64\x4c\xee\x51\xdf\xa8\x1f\x19\xf0\x7e\x6c\x47\x04\xe1\x05\x7d\x9c\x58\x4f\x6e\x58\xfd\xb0\x7d\xb5\x88\x68\xe3\xee\x23\xff\x49\xcf\xf8\x5d\xad\xb8\x30\x45\x34\xe1\x59\xf4\x3e\x8f\xc3\x04\xb6\x71\x70\xbc\x5c\x47\x6c\x5b\x30\x3d\x81\xa3\x92\xd3\x7e\xbf\xb2\xcb\xdb\xc0\xd1\x7f\x03\x00\x00\xff\xff\xdb\xbd\x05\x76\xd3\x0d\x00\x00") func enumTmplBytes() ([]byte, error) { return bindataRead( @@ -85,7 +85,7 @@ func enumTmpl() (*asset, error) { } info := bindataFileInfo{name: "enum.tmpl", size: 0, mode: os.FileMode(0), modTime: time.Unix(0, 0)} - a := &asset{bytes: bytes, info: info, digest: [32]uint8{0xa6, 0x47, 0x65, 0xcd, 0x71, 0x59, 0x7d, 0x63, 0x89, 0x4, 0xcb, 0x37, 0xff, 0xc3, 0xf, 0x89, 0x78, 0x9f, 0x37, 0x16, 0xcd, 0xb9, 0xc4, 0x34, 0x1d, 0x91, 0xc9, 0x68, 0x67, 0x61, 0x7d, 0xf2}} + a := &asset{bytes: bytes, info: info, digest: [32]uint8{0xbf, 0x4e, 0xba, 0x1b, 0x70, 0xaf, 0x58, 0x5f, 0x7f, 0x98, 0xb7, 0x5, 0xb3, 0x1f, 0xbf, 0xaa, 0xb8, 0xe4, 0xdf, 0xe, 0xe9, 0xcf, 0xea, 0x6f, 0x78, 0x61, 0x54, 0x6b, 0xbe, 0x82, 0xe6, 0x3d}} return a, nil } diff --git a/generator/enum.tmpl b/generator/enum.tmpl index 9d4a6819..95a6d888 100644 --- a/generator/enum.tmpl +++ b/generator/enum.tmpl @@ -30,8 +30,7 @@ func Parse{{.enum.Name}}(name string) ({{.enum.Name}}, error) { if x, ok := _{{.enum.Name}}Value[name]; ok { return x, nil }{{if .nocase }} - // Case Invariant Parse enabled, do a separate lookup. - // This is a separate lookup to remove unnecessary cost of lowercasing a string if we don't need to. + // Case insensitive parse, do a separate lookup to prevent unnecessary cost of lowercasing a string if we don't need to. if x, ok := _{{.enum.Name}}Value[strings.ToLower(name)]; ok { return x, nil }{{- end}} diff --git a/generator/generator.go b/generator/generator.go index ac6e94d5..3c7a00cf 100644 --- a/generator/generator.go +++ b/generator/generator.go @@ -33,7 +33,7 @@ type Generator struct { fileSet *token.FileSet noPrefix bool lowercaseLookup bool - caseInvariant bool + caseInsensitive bool marshal bool sql bool flag bool @@ -100,9 +100,9 @@ func (g *Generator) WithLowercaseVariant() *Generator { } // WithLowercaseVariant is used to change the enum const values generated to not have the enum on them. -func (g *Generator) WithCaseInvariantParse() *Generator { +func (g *Generator) WithCaseInsensitiveParse() *Generator { g.lowercaseLookup = true - g.caseInvariant = true + g.caseInsensitive = true return g } @@ -188,7 +188,7 @@ func (g *Generator) Generate(f *ast.File) ([]byte, error) { "enum": enum, "name": name, "lowercase": g.lowercaseLookup, - "nocase": g.caseInvariant, + "nocase": g.caseInsensitive, "marshal": g.marshal, "sql": g.sql, "flag": g.flag, diff --git a/main.go b/main.go index fa31a983..4f6f9504 100644 --- a/main.go +++ b/main.go @@ -104,7 +104,7 @@ func main() { g.WithLowercaseVariant() } if argv.NoCase { - g.WithCaseInvariantParse() + g.WithCaseInsensitiveParse() } if argv.Marshal { g.WithMarshal() From 7c89a9f7a64f7e2919b4969a7807269476369b51 Mon Sep 17 00:00:00 2001 From: Alex Bice Date: Tue, 16 Feb 2021 12:17:11 -0700 Subject: [PATCH 3/3] Add tests --- example/example_test.go | 3 ++ .../.snapshots/generator-TestExampleFile | 46 ++++++++++++++++++- generator/generator_test.go | 2 +- 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/example/example_test.go b/example/example_test.go index 80a1c12b..3df87452 100644 --- a/example/example_test.go +++ b/example/example_test.go @@ -187,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) diff --git a/generator/.snapshots/generator-TestExampleFile b/generator/.snapshots/generator-TestExampleFile index a021bdf0..94399733 100644 --- a/generator/.snapshots/generator-TestExampleFile +++ b/generator/.snapshots/generator-TestExampleFile @@ -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) "", @@ -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) "", @@ -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) "", @@ -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) "", @@ -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) "", @@ -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) "", @@ -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) "", @@ -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) "", @@ -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) "", @@ -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) "", @@ -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) "", @@ -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) "", diff --git a/generator/generator_test.go b/generator/generator_test.go index cdb6c188..f6eb9778 100644 --- a/generator/generator_test.go +++ b/generator/generator_test.go @@ -49,7 +49,7 @@ func TestExampleFile(t *testing.T) { g := NewGenerator(). WithMarshal(). WithSQLDriver(). - WithLowercaseVariant(). + WithCaseInsensitiveParse(). WithNames(). WithoutSnakeToCamel() // Parse the file given in arguments