Skip to content

Commit

Permalink
Add Prefix
Browse files Browse the repository at this point in the history
General cleanup
  • Loading branch information
abice committed Jan 17, 2020
1 parent 304de84 commit 7556fd6
Show file tree
Hide file tree
Showing 22 changed files with 1,348 additions and 72 deletions.
24 changes: 13 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
.DEFAULT_GOAL:=all
ifdef VERBOSE
V = -v
else
.SILENT:
endif

include $(wildcard *.mk)

COVERAGEDIR = coverage
SERVICE=local
ifdef CIRCLE_WORKING_DIRECTORY
Expand All @@ -10,14 +19,7 @@ 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:
build: deps
go generate ./generator
if [ ! -d bin ]; then mkdir bin; fi
go build -v -o bin/go-enum .
Expand All @@ -35,7 +37,7 @@ tc: test cover
coveralls:
goveralls -coverprofile=coverage.out -service=$(SERVICE) -repotoken=$(COVERALLS_TOKEN)

clean:
clean: cleandeps
go clean
rm -f bin/go-enum
rm -rf coverage/
Expand All @@ -44,8 +46,8 @@ clean:
generate:
go generate $(PACKAGES)

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

install:
go install
Expand Down
18 changes: 18 additions & 0 deletions deps.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
GO ?= go

GOBINDATA=bin/go/github.com/kevinburke/go-bindata/go-bindata
GOIMPORTS=bin/go/golang.org/x/tools/cmd/goimports
MOCKGEN=bin/go/github.com/golang/mock/mockgen
TOOLS = $(GOBINDATA) \
$(GOIMPORTS) \
$(MOCKGEN)

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.
2 changes: 1 addition & 1 deletion example/color.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:generate go-enum -f=$GOFILE --marshal --lower
//go:generate ../bin/go-enum -f=$GOFILE --marshal --lower

package example

Expand Down
Empty file modified example/color_enum.go
100755 → 100644
Empty file.
2 changes: 1 addition & 1 deletion example/commented.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:generate go-enum -f=$GOFILE --marshal --lower
//go:generate ../bin/go-enum -f=$GOFILE --marshal --lower

package example

Expand Down
10 changes: 10 additions & 0 deletions example/custom_prefix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//go:generate ../bin/go-enum -f=$GOFILE --prefix=AcmeInc

package example

// Products of AcmeInc ENUM(
// Anvil,
// Dynamite,
// Glue
// )
type Product int32
47 changes: 47 additions & 0 deletions example/custom_prefix_enum.go

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

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

import (
"encoding/json"
"testing"

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

type productData struct {
ProductX Product `json:"product"`
}

func TestProductString(t *testing.T) {
x := Product(109)
assert.Equal(t, "Product(109)", x.String())
x = Product(1)
assert.Equal(t, "Dynamite", x.String())

y, err := ParseProduct("Anvil")
require.NoError(t, err, "Failed parsing anvil")
assert.Equal(t, AcmeIncProductAnvil, y)

z, err := ParseProduct("Snake")
require.Error(t, err, "Shouldn't parse a snake")
assert.Equal(t, Product(0), z)
}

func TestProductUnmarshal(t *testing.T) {
tests := []struct {
name string
input string
output *productData
errorExpected bool
err error
}{
{
name: "anvil",
input: `{"product":0}`,
output: &productData{ProductX: AcmeIncProductAnvil},
errorExpected: false,
err: nil,
},
{
name: "dynamite",
input: `{"product":1}`,
output: &productData{ProductX: AcmeIncProductDynamite},
errorExpected: false,
err: nil,
},
{
name: "glue",
input: `{"product":2}`,
output: &productData{ProductX: AcmeIncProductGlue},
errorExpected: false,
err: nil,
},
{
name: "notanproduct",
input: `{"product":22}`,
output: &productData{ProductX: Product(22)},
errorExpected: false,
err: nil,
},
}

for _, test := range tests {
t.Run(test.name, func(tt *testing.T) {
x := &productData{}
err := json.Unmarshal([]byte(test.input), x)
if !test.errorExpected {
require.NoError(tt, err, "failed unmarshalling the json.")
assert.Equal(tt, test.output.ProductX, x.ProductX)
} else {
require.Error(tt, err)
assert.EqualError(tt, err, test.err.Error())
}
})
}
}

func TestProductMarshal(t *testing.T) {
tests := []struct {
name string
input *productData
output string
errorExpected bool
err error
}{
{
name: "anvil",
output: `{"product":0}`,
input: &productData{ProductX: AcmeIncProductAnvil},
errorExpected: false,
err: nil,
},
{
name: "dynamite",
output: `{"product":1}`,
input: &productData{ProductX: AcmeIncProductDynamite},
errorExpected: false,
err: nil,
},
{
name: "glue",
output: `{"product":2}`,
input: &productData{ProductX: AcmeIncProductGlue},
errorExpected: false,
err: nil,
},
}

for _, test := range tests {
t.Run(test.name, func(tt *testing.T) {
raw, err := json.Marshal(test.input)
require.NoError(tt, err, "failed marshalling to json")
assert.JSONEq(tt, test.output, string(raw))
})
}
}
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 go-enum -f=$GOFILE --marshal --lower --flag --names
//go:generate ../bin/go-enum -f=$GOFILE --marshal --lower --flag --names

package example

Expand Down
Empty file modified example/example_enum.go
100755 → 100644
Empty file.
2 changes: 1 addition & 1 deletion example/sql.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:generate go-enum -f=$GOFILE --sql
//go:generate ../bin/go-enum -f=$GOFILE --sql

package example

Expand Down
2 changes: 1 addition & 1 deletion example/sql_1_11_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// +build go1.11
//go:generate mockgen -destination sql_mock_test.go -package example database/sql/driver Conn,Driver,Stmt,Result,Rows
//go:generate ../bin/go/github.com/golang/mock/mockgen -destination sql_mock_test.go -package example database/sql/driver Conn,Driver,Stmt,Result,Rows

/*
This example shows the conversion of enumerations between GO and SQL database.
Expand Down
Loading

0 comments on commit 7556fd6

Please sign in to comment.