Skip to content

Commit

Permalink
ForceLowercase and MustParse (#80)
Browse files Browse the repository at this point in the history
* Add puncuation to comments

* ForceLower and MustParse

* Revert makefile changes

Co-authored-by: Alex Bice <alex.bice@users.noreply.github.com>
  • Loading branch information
abice and Alex Bice authored Jan 23, 2022
1 parent a703e9c commit 07a5b31
Show file tree
Hide file tree
Showing 25 changed files with 360 additions and 164 deletions.
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,48 @@ An enum generator for go

## How it works

go-enum will take a commented type declaration like this:

```go
// ENUM(jpeg, jpg, png, tiff, gif)
type ImageType int
```

and generate a file with the iota definition along various optional niceties that you may need:

```go
const (
// ImageTypeJpeg is a ImageType of type Jpeg.
ImageTypeJpeg ImageType = iota
// ImageTypeJpg is a ImageType of type Jpg.
ImageTypeJpg
// ImageTypePng is a ImageType of type Png.
ImageTypePng
// ImageTypeTiff is a ImageType of type Tiff.
ImageTypeTiff
// ImageTypeGif is a ImageType of type Gif.
ImageTypeGif
)

// String implements the Stringer interface.
func (x ImageType) String() string

// ParseImageType attempts to convert a string to a ImageType.
func ParseImageType(name string) (ImageType, error)

// MarshalText implements the text marshaller method.
func (x ImageType) MarshalText() ([]byte, error)

// UnmarshalText implements the text unmarshaller method.
func (x *ImageType) UnmarshalText(text []byte) error
```

**Fear not the fact that the `MarshalText` and `UnmarshalText` are generated rather than JSON methods... they will still be utilized by the default JSON encoding methods.**

If you find that the options given are not adequate for your use case, there is an option to add a custom template (`-t` flag) to the processing engine so that your custom code can be created!

## Goal

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.
It's not perfect, but I think it's useful.

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

package example

Expand Down
15 changes: 12 additions & 3 deletions example/color_enum.go

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

6 changes: 6 additions & 0 deletions example/color_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ func TestColorString(t *testing.T) {
assert.Equal(t, &x, Color(109).Ptr())
}

func TestColorMustParse(t *testing.T) {
x := `avocadogreen`

assert.PanicsWithError(t, x+" is not a valid Color", func() { MustParseColor(x) })
}

func TestColorUnmarshal(t *testing.T) {
tests := []struct {
name string
Expand Down
12 changes: 6 additions & 6 deletions example/commented_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/custom_prefix_enum.go

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

12 changes: 6 additions & 6 deletions example/example_enum.go

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

9 changes: 9 additions & 0 deletions example/force_lower.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//go:generate ../bin/go-enum -f=$GOFILE --forcelower

package example

// ENUM(
// DataSwap,
// BootNode,
// )
type ForceLowerType int
46 changes: 46 additions & 0 deletions example/force_lower_enum.go

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

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

import (
"testing"

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

func TestForceLowerString(t *testing.T) {

tests := map[string]struct {
input string
output ForceLowerType
}{
"dataswap": {
input: `dataswap`,
output: ForceLowerTypeDataSwap,
},
"bootnode": {
input: `bootnode`,
output: ForceLowerTypeBootNode,
},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
output, err := ParseForceLowerType(tc.input)
assert.NoError(t, err)
assert.Equal(t, tc.output, output)

assert.Equal(t, tc.input, output.String())
})
}
}
2 changes: 1 addition & 1 deletion example/globs/letter_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/globs/number_enum.go

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

6 changes: 3 additions & 3 deletions example/sql_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/sql_int_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/sql_str_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/user_template_enum.go

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

Loading

0 comments on commit 07a5b31

Please sign in to comment.