From f308fc423bddcdfa120632464881ab18c74d175e Mon Sep 17 00:00:00 2001 From: Alex Bice Date: Mon, 14 Aug 2017 11:00:40 -0700 Subject: [PATCH] Stringer update (#5) Fixed String() method with skips. --- .circleci/config.yml | 11 +--- Makefile | 7 ++- README.md | 56 +++++++++-------- example/color_enum.go | 48 +++++++++------ example/example.go | 2 +- example/example_enum.go | 117 ++++++++++++++++++++++++++++-------- example/example_test.go | 11 ++++ generator/assets.go | 4 +- generator/enum.tmpl | 35 ++++++----- generator/example_test.go | 2 +- generator/generator.go | 2 + generator/generator_test.go | 2 + generator/template_funcs.go | 36 +++++++++++ 13 files changed, 229 insertions(+), 104 deletions(-) create mode 100644 example/example_test.go diff --git a/.circleci/config.yml b/.circleci/config.yml index cedf5bc6..f752ec58 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -23,13 +23,4 @@ jobs: name: Test command: | make cover - make coveralls - - persist_to_workspace: - root: . - paths: . - -workflows: - version: 2 - build_and_test: - jobs: - - build: + make coveralls \ No newline at end of file diff --git a/Makefile b/Makefile index b99f1c28..91c11c7b 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ COVERAGEDIR = coverage SERVICE=local -ifdef CIRCLE_ARTIFACTS - COVERAGEDIR = $(CIRCLE_ARTIFACTS) +ifdef CIRCLE_WORKING_DIRECTORY + COVERAGEDIR = $(CIRCLE_WORKING_DIRECTORY)/coverage SERVICE=circle-ci endif @@ -31,11 +31,12 @@ test: generate gen-test if [ ! -d coverage ]; then mkdir coverage; fi go test -v ./generator -race -cover -coverprofile=$(COVERAGEDIR)/generator.coverprofile -cover: gen-test +cover: gen-test test go tool cover -html=$(COVERAGEDIR)/generator.coverprofile -o $(COVERAGEDIR)/generator.html tc: test cover coveralls: + if [ ! -d $(COVERAGEDIR) ]; then mkdir $(COVERAGEDIR); fi gover $(COVERAGEDIR) $(COVERAGEDIR)/coveralls.coverprofile goveralls -coverprofile=$(COVERAGEDIR)/coveralls.coverprofile -service=$(SERVICE) -repotoken=$(COVERALLS_TOKEN) diff --git a/README.md b/README.md index 2d829824..aa26de3b 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ type Color int32 The generated code will look something like: ``` go + const ( // ColorBlack is a Color of type Black ColorBlack Color = iota @@ -54,31 +55,38 @@ const ( const _ColorName = "BlackWhiteRedGreenBlueGreyYellow" -var _ColorIndex = [...]uint8{0, 5, 10, 13, 18, 22, 26, 32} +var _ColorMap = map[Color]string{ + 0: _ColorName[0:5], + 1: _ColorName[5:10], + 2: _ColorName[10:13], + 3: _ColorName[13:18], + 4: _ColorName[18:22], + 5: _ColorName[22:26], + 6: _ColorName[26:32], +} func (i Color) String() string { - if i < 0 || i >= Color(len(_ColorIndex)-1) { - return fmt.Sprintf("Color(%d)", i) + if str, ok := _ColorMap[i]; ok { + return str } - return _ColorName[_ColorIndex[i]:_ColorIndex[i+1]] + return fmt.Sprintf("Color(%d)", i) } -var _ColorValue = map[string]int{ - - "Black": 0, - "White": 1, - "Red": 2, - "Green": 3, - "Blue": 4, - "Grey": 5, - "Yellow": 6, - "black": 0, - "white": 1, - "red": 2, - "green": 3, - "blue": 4, - "grey": 5, - "yellow": 6, +var _ColorValue = map[string]Color{ + _ColorName[0:5]: 0, + strings.ToLower(_ColorName[0:5]): 0, + _ColorName[5:10]: 1, + strings.ToLower(_ColorName[5:10]): 1, + _ColorName[10:13]: 2, + strings.ToLower(_ColorName[10:13]): 2, + _ColorName[13:18]: 3, + strings.ToLower(_ColorName[13:18]): 3, + _ColorName[18:22]: 4, + strings.ToLower(_ColorName[18:22]): 4, + _ColorName[22:26]: 5, + strings.ToLower(_ColorName[22:26]): 5, + _ColorName[26:32]: 6, + strings.ToLower(_ColorName[26:32]): 6, } // ParseColor attempts to convert a string to a Color @@ -106,12 +114,10 @@ func (x *Color) UnmarshalText(text []byte) error { ## Adding it to your project - +1. `go get github.com/abice/go-enum` 1. Add a go:generate line to your file like so... `//go:generate go-enum -f=thisfile.go` - -2. Run go generate like so `go generate ./...` - -3. Enjoy your newly created Enumeration +1. Run go generate like so `go generate ./...` +1. Enjoy your newly created Enumeration ## Options diff --git a/example/color_enum.go b/example/color_enum.go index 0fbe0891..d222c782 100755 --- a/example/color_enum.go +++ b/example/color_enum.go @@ -5,6 +5,7 @@ package example import ( "fmt" + "strings" ) const ( @@ -26,31 +27,38 @@ const ( const _ColorName = "BlackWhiteRedGreenBlueGreyYellow" -var _ColorIndex = [...]uint8{0, 5, 10, 13, 18, 22, 26, 32} +var _ColorMap = map[Color]string{ + 0: _ColorName[0:5], + 1: _ColorName[5:10], + 2: _ColorName[10:13], + 3: _ColorName[13:18], + 4: _ColorName[18:22], + 5: _ColorName[22:26], + 6: _ColorName[26:32], +} func (i Color) String() string { - if i < 0 || i >= Color(len(_ColorIndex)-1) { - return fmt.Sprintf("Color(%d)", i) + if str, ok := _ColorMap[i]; ok { + return str } - return _ColorName[_ColorIndex[i]:_ColorIndex[i+1]] + return fmt.Sprintf("Color(%d)", i) } -var _ColorValue = map[string]int{ - - "Black": 0, - "White": 1, - "Red": 2, - "Green": 3, - "Blue": 4, - "Grey": 5, - "Yellow": 6, - "black": 0, - "white": 1, - "red": 2, - "green": 3, - "blue": 4, - "grey": 5, - "yellow": 6, +var _ColorValue = map[string]Color{ + _ColorName[0:5]: 0, + strings.ToLower(_ColorName[0:5]): 0, + _ColorName[5:10]: 1, + strings.ToLower(_ColorName[5:10]): 1, + _ColorName[10:13]: 2, + strings.ToLower(_ColorName[10:13]): 2, + _ColorName[13:18]: 3, + strings.ToLower(_ColorName[13:18]): 3, + _ColorName[18:22]: 4, + strings.ToLower(_ColorName[18:22]): 4, + _ColorName[22:26]: 5, + strings.ToLower(_ColorName[22:26]): 5, + _ColorName[26:32]: 6, + strings.ToLower(_ColorName[26:32]): 6, } // ParseColor attempts to convert a string to a Color diff --git a/example/example.go b/example/example.go index 801f6aa3..a5395091 100644 --- a/example/example.go +++ b/example/example.go @@ -13,5 +13,5 @@ type X struct { // ) type Animal int32 -// Model x ENUM(Toyota,Chevy,_,Ford) +// Model x ENUM(Toyota,_,Chevy,_,Ford,_,Tesla,_,Hyundai,_,Nissan,_,Jaguar,_,Audi,_,BMW,_,Mercedes,_,Volkswagon) type Model int32 diff --git a/example/example_enum.go b/example/example_enum.go index 4f2df48a..fee2673b 100755 --- a/example/example_enum.go +++ b/example/example_enum.go @@ -5,6 +5,7 @@ package example import ( "fmt" + "strings" ) const ( @@ -18,23 +19,26 @@ const ( const _AnimalName = "CatDogFish" -var _AnimalIndex = [...]uint8{0, 3, 6, 10} +var _AnimalMap = map[Animal]string{ + 0: _AnimalName[0:3], + 1: _AnimalName[3:6], + 2: _AnimalName[6:10], +} func (i Animal) String() string { - if i < 0 || i >= Animal(len(_AnimalIndex)-1) { - return fmt.Sprintf("Animal(%d)", i) + if str, ok := _AnimalMap[i]; ok { + return str } - return _AnimalName[_AnimalIndex[i]:_AnimalIndex[i+1]] + return fmt.Sprintf("Animal(%d)", i) } -var _AnimalValue = map[string]int32{ - - "Cat": 0, - "Dog": 1, - "Fish": 2, - "cat": 0, - "dog": 1, - "fish": 2, +var _AnimalValue = map[string]Animal{ + _AnimalName[0:3]: 0, + strings.ToLower(_AnimalName[0:3]): 0, + _AnimalName[3:6]: 1, + strings.ToLower(_AnimalName[3:6]): 1, + _AnimalName[6:10]: 2, + strings.ToLower(_AnimalName[6:10]): 2, } // ParseAnimal attempts to convert a string to a Animal @@ -62,33 +66,94 @@ func (x *Animal) UnmarshalText(text []byte) error { const ( // ModelToyota is a Model of type Toyota ModelToyota Model = iota + // Skipped value + _ // ModelChevy is a Model of type Chevy ModelChevy // Skipped value _ // ModelFord is a Model of type Ford ModelFord + // Skipped value + _ + // ModelTesla is a Model of type Tesla + ModelTesla + // Skipped value + _ + // ModelHyundai is a Model of type Hyundai + ModelHyundai + // Skipped value + _ + // ModelNissan is a Model of type Nissan + ModelNissan + // Skipped value + _ + // ModelJaguar is a Model of type Jaguar + ModelJaguar + // Skipped value + _ + // ModelAudi is a Model of type Audi + ModelAudi + // Skipped value + _ + // ModelBMW is a Model of type BMW + ModelBMW + // Skipped value + _ + // ModelMercedes is a Model of type Mercedes + ModelMercedes + // Skipped value + _ + // ModelVolkswagon is a Model of type Volkswagon + ModelVolkswagon ) -const _ModelName = "ToyotaChevyFord" - -var _ModelIndex = [...]uint8{0, 6, 11, 15} +const _ModelName = "ToyotaChevyFordTeslaHyundaiNissanJaguarAudiBMWMercedesVolkswagon" + +var _ModelMap = map[Model]string{ + 0: _ModelName[0:6], + 2: _ModelName[6:11], + 4: _ModelName[11:15], + 6: _ModelName[15:20], + 8: _ModelName[20:27], + 10: _ModelName[27:33], + 12: _ModelName[33:39], + 14: _ModelName[39:43], + 16: _ModelName[43:46], + 18: _ModelName[46:54], + 20: _ModelName[54:64], +} func (i Model) String() string { - if i < 0 || i >= Model(len(_ModelIndex)-1) { - return fmt.Sprintf("Model(%d)", i) + if str, ok := _ModelMap[i]; ok { + return str } - return _ModelName[_ModelIndex[i]:_ModelIndex[i+1]] + return fmt.Sprintf("Model(%d)", i) } -var _ModelValue = map[string]int32{ - - "Toyota": 0, - "Chevy": 1, - "Ford": 3, - "toyota": 0, - "chevy": 1, - "ford": 3, +var _ModelValue = map[string]Model{ + _ModelName[0:6]: 0, + strings.ToLower(_ModelName[0:6]): 0, + _ModelName[6:11]: 2, + strings.ToLower(_ModelName[6:11]): 2, + _ModelName[11:15]: 4, + strings.ToLower(_ModelName[11:15]): 4, + _ModelName[15:20]: 6, + strings.ToLower(_ModelName[15:20]): 6, + _ModelName[20:27]: 8, + strings.ToLower(_ModelName[20:27]): 8, + _ModelName[27:33]: 10, + strings.ToLower(_ModelName[27:33]): 10, + _ModelName[33:39]: 12, + strings.ToLower(_ModelName[33:39]): 12, + _ModelName[39:43]: 14, + strings.ToLower(_ModelName[39:43]): 14, + _ModelName[43:46]: 16, + strings.ToLower(_ModelName[43:46]): 16, + _ModelName[46:54]: 18, + strings.ToLower(_ModelName[46:54]): 18, + _ModelName[54:64]: 20, + strings.ToLower(_ModelName[54:64]): 20, } // ParseModel attempts to convert a string to a Model diff --git a/example/example_test.go b/example/example_test.go new file mode 100644 index 00000000..e64f39f1 --- /dev/null +++ b/example/example_test.go @@ -0,0 +1,11 @@ +package example + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestModel(t *testing.T) { + assert.Equal(t, "Ford", ModelFord.String()) +} diff --git a/generator/assets.go b/generator/assets.go index 0cb34b72..6012ea87 100644 --- a/generator/assets.go +++ b/generator/assets.go @@ -68,7 +68,7 @@ func (fi bindataFileInfo) Sys() interface{} { return nil } -var _enumTmpl = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x94\x54\x4d\x6f\xdc\x36\x10\x3d\x2f\x7f\xc5\x8b\xe0\x00\x62\xaa\x5d\x3b\x57\xb7\xdb\x4b\x93\x43\x0e\x4d\x02\xd8\xed\x65\xb1\x08\x98\xd5\x68\x43\x78\x45\xa9\x14\xed\x6a\xc1\xf0\xbf\x17\x43\x4a\xb6\xac\xca\x40\x72\x12\xc5\x99\xf7\xe6\xeb\x71\xbc\x5f\xa3\xa4\x4a\x1b\x42\xf6\x8d\x54\x49\x36\x0b\x41\x5c\x5e\xe2\x8f\xa6\x24\x1c\xc9\x90\x55\x8e\x4a\x7c\x3d\xe3\xd8\xac\xc9\xdc\xd7\x6c\x7c\xf7\x09\x1f\x3f\xdd\xe2\xfd\xbb\x0f\xb7\xaf\x84\x68\xd5\xe1\x4e\x1d\x09\xde\x6f\x86\x63\x08\x42\xe8\xba\x6d\xac\x43\x2e\x00\x20\xab\x6a\x97\x09\x29\xbc\x27\x53\x62\xcd\xf6\x69\x64\xe6\xe5\xb8\x87\xc6\x74\x0c\x61\xdb\x05\x5f\x7e\x54\x35\xe1\x7a\x8b\x0d\xff\x6c\xe2\x1f\x83\xbd\x87\x55\xe6\x48\xb8\xb0\x1f\x4c\x49\x7d\x81\x8b\x07\x75\xba\x9f\xb8\xfe\xcd\xbf\x1d\x42\x10\x2b\xef\xa1\x2b\xd0\x3f\x83\x4f\x62\xc9\xbe\x64\x21\x5c\x5e\xe2\xe6\x4e\xb7\x2d\x95\x88\x26\xef\xe9\xd4\x51\xbc\xf7\x7e\xf0\xfe\x6c\xa9\xd2\x3d\x95\x8c\x0a\x01\xba\x83\x62\xe3\x98\x5c\x08\x68\x2a\xb8\x73\x4b\x4f\x90\x74\x1f\x4b\x0d\x21\x96\xff\x12\xdb\x53\x6a\xa9\x0e\x5c\x81\x81\x53\xf6\x2d\x74\xe3\xd4\x48\xc6\x8d\x49\x27\x29\x86\x6e\x7d\xf1\xfe\xa9\x3b\x21\xc4\xea\xb6\xc8\xbc\xef\x9c\xd5\xe6\xa8\xab\x73\x6a\x09\x42\xc8\x84\x78\x50\x76\x8e\x48\x91\xb7\xf0\x5e\xf3\x69\x0a\x10\xa2\xba\x37\x07\xe4\x1a\xcf\x21\x12\x37\x91\x3c\x97\x48\x51\xe0\xc5\x4a\x57\xd0\xf8\x0d\x57\xf8\xfe\x1d\x1a\xbf\x6f\x67\x98\xfc\x44\x26\x5f\x0a\x2d\xd7\x6f\x25\xe3\x57\x96\xdc\xbd\x35\xa8\x6a\xb7\xb9\x69\xad\x36\xae\xca\xb3\x19\xc7\xeb\x52\x66\x05\xb4\x14\xab\x20\x46\xff\x85\x06\xec\x96\xe2\xec\xf4\xfe\x7a\xf9\xfe\x97\xb7\xfb\xbd\x08\x8b\xcd\x89\x3a\xc2\x16\xb5\x6a\x77\xa9\xd4\xfd\xe8\x70\x7b\x6e\x79\xcc\xc3\x80\x7f\x50\x90\x6b\x9e\xaf\xae\x60\xe8\xff\x72\x14\xab\x6c\xa6\xa1\xec\xfa\x49\x3a\x91\x20\x84\x62\x90\xc2\xe3\x47\x57\xd8\x9c\x9a\x7f\xc9\x1e\x54\x47\x51\x3e\x3f\xfc\x36\x92\xfc\x5e\x4c\x05\x91\x16\x3f\x9d\x51\x12\x68\x10\xbc\x29\x3e\x2b\xdb\xd1\xf3\x96\x42\x39\x47\x75\xeb\x3a\xb8\x06\x87\xc6\x3c\x90\x75\x50\xa3\x90\x5c\x13\x1f\xd8\x14\x90\x44\xb8\xc0\x94\x1b\x4e\x38\x01\x25\xf2\xe7\xc6\x02\x64\x6d\x63\xe5\x20\xcd\xbe\x40\x73\xc7\x3d\x58\x1a\xf0\x8e\x89\xf6\xbf\xb2\xc7\x44\x88\xb3\x60\xbd\x2c\x60\xf4\x69\x2a\xbd\x99\xc7\x95\x2c\xa2\x7a\xdf\x73\xe0\x2a\xcf\x5e\x77\xbc\x2f\x4c\xc3\xe5\x3d\xa8\x93\x2e\x67\x80\xac\x00\x07\x96\x22\xae\x43\x9e\xc5\xa6\x56\xb6\xfb\xa6\x4e\x18\xab\xce\x7b\xbc\x99\xbf\xbd\x3f\x93\xcf\x2d\xf5\x2e\x97\xc8\x77\xfb\xaf\x67\x47\xd3\x72\x87\xec\x92\x21\xef\x37\xe3\x63\x1d\x0a\x78\x7c\xd6\x0b\xdc\x7f\x99\x7a\xc2\xee\xa8\x77\x03\x8d\x4c\xfc\x4c\x6f\x86\xa5\x9c\x1a\x1f\x9d\xa4\x58\xb9\xba\x8d\x39\xb0\xe5\xa5\x59\xc9\x38\x0a\x76\x7a\xb5\xe5\x54\xa6\xdd\x26\x6b\x63\x6b\xdf\xf0\x2e\x72\x75\xfb\x58\x46\x4a\x79\x94\xd5\xe3\xe1\xbf\x00\x00\x00\xff\xff\xae\xb6\xf8\xea\xba\x06\x00\x00") +var _enumTmpl = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x74\x53\xcf\x4f\xf3\x38\x10\x3d\xd7\x7f\xc5\x23\x02\x29\x46\x25\xe5\xbc\xab\x9e\x16\x0e\x1c\xf8\x21\xc1\xee\x05\x21\x64\x9a\x49\xb1\x68\x9c\xac\xe3\xf6\x4b\x65\xf9\x7f\xff\x34\x76\x4a\xd3\x7e\xf4\x66\x7b\xe6\x3d\xcf\x7b\x33\xe3\xfd\x15\x4a\xaa\xb4\x21\x64\x9f\xa4\x4a\xb2\x59\x08\x62\x36\xc3\x3f\x4d\x49\x58\x92\x21\xab\x1c\x95\xf8\xd8\x62\xd9\x5c\x91\x59\xd7\x1c\xbc\x79\xc4\xc3\xe3\x0b\x6e\x6f\xee\x5e\xce\x84\x68\xd5\xe2\x4b\x2d\x09\xde\x17\xc3\x31\x04\x21\x74\xdd\x36\xd6\x21\x17\x00\x90\x55\xb5\xcb\x84\x14\xde\x93\x29\x71\xc5\xf1\xf1\xcf\xcc\xcb\xff\x2e\x1a\xd3\x31\x84\x63\xe7\xfc\xf8\xa0\x6a\xc2\x5f\x73\x14\x7c\x29\xe2\x8d\xc1\xde\xc3\x2a\xb3\x24\x9c\xdb\x3b\x53\x52\x3f\xc5\xf9\x46\xad\xd6\xa3\xd4\xff\xf8\xda\x21\x04\x31\xf1\x1e\xba\x02\xfd\x3f\xe4\x24\x96\xec\x3d\x0b\x61\x36\xc3\xf3\x97\x6e\x5b\x2a\x11\x43\xde\xd3\xaa\xa3\xf8\xee\xfd\x90\xfd\x64\xa9\xd2\x3d\x95\x8c\x0a\x01\xba\x83\xe2\xe0\xae\xb8\x10\xd0\x54\x70\xdb\x96\xf6\x90\xf4\x1e\xa5\x86\x10\xe5\x9f\x62\xdb\x97\x96\x74\xe0\x1a\x0c\x1c\xb3\xcf\xa1\x1b\xa7\x76\x64\x6c\x4c\x3a\x49\x76\x10\x8e\xea\x76\xa5\x1c\x21\xeb\x9c\xd5\x66\x49\x36\x43\xc1\xaa\xb9\x4b\x4f\xca\x76\xe4\xfd\xde\xbb\x10\xa0\x1c\x43\x5c\x07\xd7\x60\xd1\x98\x0d\x59\x07\x85\x04\xe6\x37\x16\x37\x06\x88\x6a\x6d\x16\x3f\x31\xe5\x86\x6d\x4c\x40\x89\xfc\x30\x38\x05\x59\xdb\x58\x09\x2f\x26\xba\x42\x3f\x45\xf3\xc5\xbd\x79\x3f\x4c\x8b\x4d\x7a\x65\xa2\xb7\xbf\x39\xc3\x8b\xc9\xc4\x92\x5b\x5b\x73\x54\x45\xde\xcb\x29\x8c\x5e\x89\x49\x10\x27\x32\xae\xe5\x14\x55\xed\x8a\x5b\xfe\xb8\xca\xb3\x8b\x8e\x7b\x65\x1a\x96\xb7\x51\x2b\x5d\x1e\x01\xb2\x29\xf8\x63\x29\xe2\x28\x72\x1b\x8a\x5a\xd9\xee\x53\xad\xb0\x53\x9d\xf7\xb8\x3c\x04\x49\xdc\xa7\x9c\x17\xea\x5d\x2e\x91\xbf\xbe\x7d\x6c\x1d\x8d\xe5\x0e\xd5\xa5\x40\xde\x17\xcf\xd1\xa1\x5c\x0e\x02\x82\x38\xcd\xfd\xaf\xa9\x47\xec\x8e\x7a\x37\xd0\xc8\xc4\xcf\xf4\x66\x58\x88\x64\x7c\x4c\x92\x62\xe2\xea\x36\xd6\xc0\x91\x53\xbd\x92\xb1\x15\x9c\x74\x36\xe7\x52\xc6\x6e\x93\xb5\xd1\xda\xcb\x1e\x73\xb8\xba\xfd\x96\x91\x4a\xde\x4d\xdf\xfe\x70\xb0\xbd\xdf\xa3\xf7\xbd\xc1\x47\x7d\x8e\x1b\x37\x47\xe6\xfd\x50\xb7\xae\xb6\x69\x4f\x11\x42\x26\xc4\x46\xd9\x63\xc8\xbd\x6a\x31\xe7\xfd\xa8\x55\x3b\xce\x16\x3b\xff\x34\x8e\xed\xdb\x39\xbd\x9b\xe6\x34\x7b\x9d\xb3\x27\xa6\xef\x5e\xb5\xaf\xfa\x8f\xc1\xeb\x9c\x1d\x4f\x19\x8f\xd4\x73\x6b\xb5\x71\x55\x9e\x1d\xb9\x7a\x51\xca\x6c\x0a\x1d\x47\xe8\x07\x09\x71\xba\x93\x88\xb5\x39\x90\x51\xac\x9a\x5f\x64\x17\xaa\x23\x8c\x5c\xfd\x1d\x00\x00\xff\xff\x75\x9b\x60\x1b\x8b\x05\x00\x00") func enumTmplBytes() ([]byte, error) { return bindataRead( @@ -83,7 +83,7 @@ func enumTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "enum.tmpl", size: 1722, mode: os.FileMode(420), modTime: time.Unix(1502475635, 0)} + info := bindataFileInfo{name: "enum.tmpl", size: 1419, mode: os.FileMode(420), modTime: time.Unix(1502730755, 0)} a := &asset{bytes: bytes, info: info} return a, nil } diff --git a/generator/enum.tmpl b/generator/enum.tmpl index 46d2d00c..ec146218 100644 --- a/generator/enum.tmpl +++ b/generator/enum.tmpl @@ -18,22 +18,7 @@ const ( {{- end}} ) -const _{{.enum.Name}}Name = "{{stringify .enum }}" - -var _{{.enum.Name}}Index = {{indexify .enum }} - -func (i {{.enum.Name}}) String() string { - if i < 0 || i >= {{.enum.Name}}(len(_{{.enum.Name}}Index)-1) { - return fmt.Sprintf("{{.enum.Name}}(%d)", i) - } - return _{{.enum.Name}}Name[_{{.enum.Name}}Index[i]:_{{.enum.Name}}Index[i+1]] -} - -var _{{.enum.Name}}Value = map[string]{{.enum.Type}}{ - {{ range $rIndex, $value := .enum.Values -}}{{if ne $value.Name "_"}} - "{{$value.Name}}": {{$value.Value}},{{end}}{{end}}{{if .lowercase }}{{ range $rIndex, $value := .enum.Values }}{{ if ne $value.Name "_"}} - "{{ lower $value.Name}}": {{$value.Value}},{{end}}{{end}}{{end}} -} +{{ template "stringer" . }} // Parse{{.enum.Name}} attempts to convert a string to a {{.enum.Name}} func Parse{{.enum.Name}}(name string) ({{.enum.Name}}, error) { @@ -60,3 +45,21 @@ func (x *{{.enum.Name}}) UnmarshalText(text []byte) error { {{end}} {{end}} + + +{{- define "stringer"}} +const _{{.enum.Name}}Name = "{{ stringify .enum }}" + +var _{{.enum.Name}}Map = {{ mapify .enum }} + + +func (i {{.enum.Name}}) String() string { + if str, ok := _{{.enum.Name}}Map[i]; ok { + return str + } + return fmt.Sprintf("{{.enum.Name}}(%d)", i) +} + +var _{{.enum.Name}}Value = {{ unmapify .enum .lowercase }} + +{{end}} \ No newline at end of file diff --git a/generator/example_test.go b/generator/example_test.go index ca8561b6..c51bcce4 100644 --- a/generator/example_test.go +++ b/generator/example_test.go @@ -21,5 +21,5 @@ type Color int // ) type Animal int32 -// Model x ENUM(Toyota,Chevy,_,Ford) +// Model x ENUM(Toyota,_,Chevy,_,Ford) type Model int32 diff --git a/generator/generator.go b/generator/generator.go index 85fe907c..9715b50e 100644 --- a/generator/generator.go +++ b/generator/generator.go @@ -62,6 +62,8 @@ func NewGenerator() *Generator { funcs["stringify"] = Stringify funcs["indexify"] = Indexify + funcs["mapify"] = Mapify + funcs["unmapify"] = Unmapify g.t.Funcs(funcs) diff --git a/generator/generator_test.go b/generator/generator_test.go index f0e14255..9ff0198c 100644 --- a/generator/generator_test.go +++ b/generator/generator_test.go @@ -42,6 +42,8 @@ func TestNoFile(t *testing.T) { // TestExampleFile func TestExampleFile(t *testing.T) { g := NewGenerator() + g.WithMarshal() + g.WithLowercaseVariant() // Parse the file given in arguments imported, err := g.GenerateFromFile(testExample) assert.Nil(t, err, "Error generating formatted code") diff --git a/generator/template_funcs.go b/generator/template_funcs.go index 115013eb..4a693f5c 100644 --- a/generator/template_funcs.go +++ b/generator/template_funcs.go @@ -1,6 +1,7 @@ package generator import ( + "fmt" "strconv" ) @@ -28,3 +29,38 @@ func Indexify(e Enum) (ret string, err error) { ret = ret + strconv.Itoa(index) + `}` return } + +// Mapify returns a map that is all of the indexes for a string value lookup +func Mapify(e Enum) (ret string, err error) { + strName := fmt.Sprintf(`_%sName`, e.Name) + ret = fmt.Sprintf("map[%s]string{\n", e.Name) + index := 0 + for _, val := range e.Values { + if val.Name != skipHolder { + nextIndex := index + len(val.Name) + ret = fmt.Sprintf("%s%d: %s[%d:%d],\n", ret, val.Value, strName, index, nextIndex) + index = nextIndex + } + } + ret = ret + `}` + return +} + +// Unmapify returns a map that is all of the indexes for a string value lookup +func Unmapify(e Enum, lowercase bool) (ret string, err error) { + strName := fmt.Sprintf(`_%sName`, e.Name) + ret = fmt.Sprintf("map[string]%s{\n", e.Name) + index := 0 + for _, val := range e.Values { + if val.Name != skipHolder { + nextIndex := index + len(val.Name) + ret = fmt.Sprintf("%s%s[%d:%d]: %d,\n", ret, strName, index, nextIndex, val.Value) + if lowercase { + ret = fmt.Sprintf("%sstrings.ToLower(%s[%d:%d]): %d,\n", ret, strName, index, nextIndex, val.Value) + } + index = nextIndex + } + } + ret = ret + `}` + return +}