Skip to content

Commit

Permalink
Stringer update (#5)
Browse files Browse the repository at this point in the history
Fixed String() method with skips.
  • Loading branch information
abice authored Aug 14, 2017
1 parent 5edf28f commit f308fc4
Show file tree
Hide file tree
Showing 13 changed files with 229 additions and 104 deletions.
11 changes: 1 addition & 10 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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)

Expand Down
56 changes: 31 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
48 changes: 28 additions & 20 deletions example/color_enum.go

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

2 changes: 1 addition & 1 deletion example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
117 changes: 91 additions & 26 deletions example/example_enum.go

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

11 changes: 11 additions & 0 deletions example/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package example

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestModel(t *testing.T) {
assert.Equal(t, "Ford", ModelFord.String())
}
4 changes: 2 additions & 2 deletions generator/assets.go

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

Loading

0 comments on commit f308fc4

Please sign in to comment.