Skip to content

Commit

Permalink
Custom Prefix (#39)
Browse files Browse the repository at this point in the history
* Add Prefix
  • Loading branch information
abice authored Jan 17, 2020
1 parent 304de84 commit 18aeaef
Show file tree
Hide file tree
Showing 24 changed files with 1,465 additions and 167 deletions.
10 changes: 5 additions & 5 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ version: 2
common_parts: &common_parts
steps:
- checkout
- run:
name: Install Go Deps
command: |
go install -v github.com/kevinburke/go-bindata/go-bindata golang.org/x/tools/cmd/cover github.com/mattn/goveralls github.com/golang/mock/gomock
go install github.com/golang/mock/mockgen
- run:
name: Build
command: |
Expand All @@ -29,10 +24,15 @@ jobs:
docker:
- image: circleci/golang:1.12
<<: *common_parts
golang_1.13:
docker:
- image: circleci/golang:1.13
<<: *common_parts

workflows:
version: 2
build_and_test:
jobs:
- golang_1.11
- golang_1.12
- golang_1.13
46 changes: 25 additions & 21 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
.DEFAULT_GOAL:=all
ifdef VERBOSE
V = -v
else
.SILENT:
endif

GO ?= GO111MODULE=on go

include $(wildcard *.mk)

COVERAGEDIR = coverage
SERVICE=local
ifdef CIRCLE_WORKING_DIRECTORY
Expand All @@ -10,48 +21,41 @@ PACKAGES='./generator' './example'
.PHONY: all
all: build fmt test example cover install

.PHONY: install-deps
install-deps:
go install -v github.com/kevinburke/go-bindata/go-bindata
go install -v golang.org/x/tools/cmd/cover
go install -v github.com/mattn/goveralls
go mod vendor

build:
go generate ./generator
build: deps
$(GO) generate ./generator
if [ ! -d bin ]; then mkdir bin; fi
go build -v -o bin/go-enum .
$(GO) build -v -o bin/go-enum .

fmt:
gofmt -l -w -s $$(find . -type f -name '*.go' -not -path "./vendor/*")

test: gen-test generate
go test -v -race -coverprofile=coverage.out ./...
$(GO) test -v -race -coverprofile=coverage.out ./...

cover: gen-test test
go tool cover -html=coverage.out -o coverage.html
$(GO) tool cover -html=coverage.out -o coverage.html

tc: test cover
coveralls:
goveralls -coverprofile=coverage.out -service=$(SERVICE) -repotoken=$(COVERALLS_TOKEN)
coveralls: $(GOVERALLS)
$(GOVERALLS) -coverprofile=coverage.out -service=$(SERVICE) -repotoken=$(COVERALLS_TOKEN)

clean:
go clean
clean: cleandeps
$(GO) clean
rm -f bin/go-enum
rm -rf coverage/

.PHONY: generate
generate:
go generate $(PACKAGES)
$(GO) generate $(PACKAGES)

gen-test: build install
go generate $(PACKAGES)
gen-test: build
$(GO) generate $(PACKAGES)

install:
go install
$(GO) install

phony: clean tc build

.PHONY: example
example:
go generate ./example
$(GO) generate ./example
180 changes: 100 additions & 80 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# go-enum

[![CircleCI](https://circleci.com/gh/abice/go-enum.svg?style=svg&circle-token=b44c10ce16bcef76e86da801d67811a5ff71fc72)](https://circleci.com/gh/abice/go-enum)
[![Go Report Card](https://goreportcard.com/badge/github.com/abice/go-enum)](https://goreportcard.com/report/github.com/abice/go-enum)
[![Coverage Status](https://coveralls.io/repos/github/abice/go-enum/badge.svg)](https://coveralls.io/github/abice/go-enum)
Expand All @@ -7,7 +8,6 @@

An enum generator for go


## How it works

The goal of go-enum is to create an easy to use enum generator that will take a decorated type declaration like `type EnumName int` and create the associated constant values and funcs that will make life a little easier for adding new values.
Expand All @@ -33,15 +33,17 @@ Options:
--nocamel Removes the snake_case to CamelCase name changing
```


### Syntax

The parser looks for comments on your type defs and parse the enum declarations from it.
The parser will look for `ENUM(` and continue to look for comma separated values until it finds a `)`. You can put values on the same line, or on multiple lines.\
If you need to have a specific value jump in the enum, you can now specify that by adding `=numericValue` to the enum declaration. Keep in mind, this resets the data for all following values. So if you specify `50` in the middle of an enum, each value after that will be `51, 52, 53...`

#### Comments

You can use comments inside enum that start with `//`\
The comment must be at the end of the same line as the comment value, only then it will be added as a comment to the generated constant.

```go
// Commented is an enumeration of commented values
/*
Expand All @@ -53,23 +55,26 @@ value3 // Commented value 3
*/
type Commented int
```

The generated comments in code will look something like:

```go
...
const (
// CommentedValue1 is a Commented of type Value1
// Commented value 1
CommentedValue1 Commented = iota
// CommentedValue2 is a Commented of type Value2
CommentedValue2
// CommentedValue3 is a Commented of type Value3
// Commented value 3
CommentedValue3
// CommentedValue1 is a Commented of type Value1
// Commented value 1
CommentedValue1 Commented = iota
// CommentedValue2 is a Commented of type Value2
CommentedValue2
// CommentedValue3 is a Commented of type Value3
// Commented value 3
CommentedValue3
)
...
```

#### Example

There are a few examples in the `example` [directory](example).
I've included one here for easy access, but can't guarantee it's up to date.

Expand All @@ -85,112 +90,127 @@ Green = 33 // Green starts with 33
// blue-green
// red-orange
// yellow_green
// red-orange-blue
// )
type Color int32
```

The generated code will look something like:

``` go
// Code generated by go-enum
// DO NOT EDIT!

package example

import (
"fmt"
"strings"
)

const (
// ColorBlack is a Color of type Black
ColorBlack Color = iota
// ColorWhite is a Color of type White
ColorWhite
// ColorRed is a Color of type Red
ColorRed
// ColorGreen is a Color of type Green
// Green starts with 33
ColorGreen Color = iota + 30
// ColorBlue is a Color of type Blue
ColorBlue
// ColorGrey is a Color of type Grey
ColorGrey
// ColorYellow is a Color of type Yellow
ColorYellow
// ColorBlueGreen is a Color of type Blue-Green
ColorBlueGreen
// ColorRedOrange is a Color of type Red-Orange
ColorRedOrange
// ColorYellowGreen is a Color of type Yellow_green
ColorYellowGreen
// ColorBlack is a Color of type Black
ColorBlack Color = iota
// ColorWhite is a Color of type White
ColorWhite
// ColorRed is a Color of type Red
ColorRed
// ColorGreen is a Color of type Green
// Green starts with 33
ColorGreen Color = iota + 30
// ColorBlue is a Color of type Blue
ColorBlue
// ColorGrey is a Color of type Grey
ColorGrey
// ColorYellow is a Color of type Yellow
ColorYellow
// ColorBlueGreen is a Color of type Blue-Green
ColorBlueGreen
// ColorRedOrange is a Color of type Red-Orange
ColorRedOrange
// ColorYellowGreen is a Color of type Yellow_green
ColorYellowGreen
// ColorRedOrangeBlue is a Color of type Red-Orange-Blue
ColorRedOrangeBlue
)

const _ColorName = "BlackWhiteRedGreenBluegreyyellowblue-greenred-orangeyellow_green"
const _ColorName = "BlackWhiteRedGreenBluegreyyellowblue-greenred-orangeyellow_greenred-orange-blue"

var _ColorMap = map[Color]string{
0: _ColorName[0:5],
1: _ColorName[5:10],
2: _ColorName[10:13],
33: _ColorName[13:18],
34: _ColorName[18:22],
35: _ColorName[22:26],
36: _ColorName[26:32],
37: _ColorName[32:42],
38: _ColorName[42:52],
39: _ColorName[52:64],
0: _ColorName[0:5],
1: _ColorName[5:10],
2: _ColorName[10:13],
33: _ColorName[13:18],
34: _ColorName[18:22],
35: _ColorName[22:26],
36: _ColorName[26:32],
37: _ColorName[32:42],
38: _ColorName[42:52],
39: _ColorName[52:64],
40: _ColorName[64:79],
}

// String implements the Stringer interface.
func (x Color) String() string {
if str, ok := _ColorMap[x]; ok {
return str
}
return fmt.Sprintf("Color(%d)", x)
if str, ok := _ColorMap[x]; ok {
return str
}
return fmt.Sprintf("Color(%d)", x)
}

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]: 33,
strings.ToLower(_ColorName[13:18]): 33,
_ColorName[18:22]: 34,
strings.ToLower(_ColorName[18:22]): 34,
_ColorName[22:26]: 35,
strings.ToLower(_ColorName[22:26]): 35,
_ColorName[26:32]: 36,
strings.ToLower(_ColorName[26:32]): 36,
_ColorName[32:42]: 37,
strings.ToLower(_ColorName[32:42]): 37,
_ColorName[42:52]: 38,
strings.ToLower(_ColorName[42:52]): 38,
_ColorName[52:64]: 39,
strings.ToLower(_ColorName[52:64]): 39,
_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]: 33,
strings.ToLower(_ColorName[13:18]): 33,
_ColorName[18:22]: 34,
strings.ToLower(_ColorName[18:22]): 34,
_ColorName[22:26]: 35,
strings.ToLower(_ColorName[22:26]): 35,
_ColorName[26:32]: 36,
strings.ToLower(_ColorName[26:32]): 36,
_ColorName[32:42]: 37,
strings.ToLower(_ColorName[32:42]): 37,
_ColorName[42:52]: 38,
strings.ToLower(_ColorName[42:52]): 38,
_ColorName[52:64]: 39,
strings.ToLower(_ColorName[52:64]): 39,
_ColorName[64:79]: 40,
strings.ToLower(_ColorName[64:79]): 40,
}

// ParseColor attempts to convert a string to a Color
func ParseColor(name string) (Color, error) {
if x, ok := _ColorValue[name]; ok {
return x, nil
}
return Color(0), fmt.Errorf("%s is not a valid Color", name)
if x, ok := _ColorValue[name]; ok {
return x, nil
}
return Color(0), fmt.Errorf("%s is not a valid Color", name)
}

// MarshalText implements the text marshaller method
func (x *Color) MarshalText() ([]byte, error) {
return []byte(x.String()), nil
func (x Color) MarshalText() ([]byte, error) {
return []byte(x.String()), nil
}

// UnmarshalText implements the text unmarshaller method
func (x *Color) UnmarshalText(text []byte) error {
name := string(text)
tmp, err := ParseColor(name)
if err != nil {
return err
}
*x = tmp
return nil
name := string(text)
tmp, err := ParseColor(name)
if err != nil {
return err
}
*x = tmp
return nil
}

```


## 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=$GOFILE --marshal`
1. Run go generate like so `go generate ./...`
Expand Down
18 changes: 18 additions & 0 deletions deps.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
GOBINDATA=bin/go/github.com/kevinburke/go-bindata/go-bindata
GOIMPORTS=bin/go/golang.org/x/tools/cmd/goimports
GOVERALLS=bin/go/github.com/mattn/goveralls
MOCKGEN=bin/go/github.com/golang/mock/mockgen
TOOLS = $(GOBINDATA) \
$(GOIMPORTS) \
$(MOCKGEN) \
$(GOVERALLS)

cleandeps:
if [ -d "./bin" ]; then rm -rf "./bin"; fi

freshdeps: cleandeps deps

deps: $(TOOLS)
bin/go/%:
@echo "installing $*"
$(GO) build -o bin/go/$* $*
2 changes: 1 addition & 1 deletion example/animal.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:generate go-enum -f=$GOFILE
//go:generate ../bin/go-enum -f=$GOFILE

package example

Expand Down
Empty file modified example/animal_enum.go
100755 → 100644
Empty file.
Loading

0 comments on commit 18aeaef

Please sign in to comment.