Skip to content

Commit

Permalink
Added option for snake_case to CamelCase. (#33)
Browse files Browse the repository at this point in the history
* Added option for snake_case to CamelCase.
The default will be to convert the names of the fields.
There is a flag to disable that conversion.
  • Loading branch information
abice authored Apr 9, 2019
1 parent c5b07cc commit 9b3947d
Show file tree
Hide file tree
Showing 10 changed files with 1,162 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ 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
Expand Down
22 changes: 16 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Options:
--flag Adds golang flag functions.
--prefix Replaces the prefix with a user one.
--names Generates a 'Names() []string' function, and adds the possible enum values in the error response during parsing
--nocamel Removes the snake_case to CamelCase name changing
```


Expand Down Expand Up @@ -82,6 +83,7 @@ Green = 33 // Green starts with 33
// yellow
// blue-green
// red-orange
// yellow_green
// )
type Color int32
```
Expand Down Expand Up @@ -110,9 +112,11 @@ const (
ColorBlueGreen
// ColorRedOrange is a Color of type Red-Orange
ColorRedOrange
// ColorYellowGreen is a Color of type Yellow_green
ColorYellowGreen
)

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

var _ColorMap = map[Color]string{
0: _ColorName[0:5],
Expand All @@ -124,13 +128,15 @@ var _ColorMap = map[Color]string{
36: _ColorName[26:32],
37: _ColorName[32:42],
38: _ColorName[42:52],
39: _ColorName[52:64],
}

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

var _ColorValue = map[string]Color{
Expand All @@ -152,20 +158,24 @@ var _ColorValue = map[string]Color{
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,
}

// ParseColor attempts to convert a string to a Color
func ParseColor(name string) (Color, error) {
if x, ok := _ColorValue[name]; ok {
return Color(x), nil
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
}

// UnmarshalText implements the text unmarshaller method
func (x *Color) UnmarshalText(text []byte) error {
name := string(text)
tmp, err := ParseColor(name)
Expand All @@ -181,6 +191,6 @@ 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=$GOFILE`
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 ./...`
1. Enjoy your newly created Enumeration
1 change: 1 addition & 0 deletions example/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ Green = 33 // Green starts with 33
// yellow
// blue-green
// red-orange
// yellow_green
// )
type Color int
7 changes: 6 additions & 1 deletion example/color_enum.go

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

7 changes: 7 additions & 0 deletions example/color_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ func TestColorUnmarshal(t *testing.T) {
errorExpected: false,
err: nil,
},
{
name: "yellow_green",
input: `{"color":"yellow_green"}`,
output: &testData{ColorX: ColorYellowGreen},
errorExpected: false,
err: nil,
},
{
name: "magenta",
input: `{"color":"Magenta"}`,
Expand Down
75 changes: 75 additions & 0 deletions example/commented_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,78 @@ func TestCommentedUnmarshal(t *testing.T) {
})
}
}

type complexCommentedData struct {
ComplexCommentedX ComplexCommented `json:"ComplexCommented,omitempty"`
}

func TestComplexCommentedEnumString(t *testing.T) {
x := ComplexCommented(109)
assert.Equal(t, "ComplexCommented(109)", x.String())
x = ComplexCommented(1)
assert.Equal(t, "value1", x.String())

y, err := ParseComplexCommented("value3")
require.NoError(t, err, "Failed parsing value3")
assert.Equal(t, ComplexCommentedValue3, y)

z, err := ParseComplexCommented("value4")
require.Error(t, err, "Shouldn't parse a value4")
assert.Equal(t, ComplexCommented(0), z)
}

func TestComplexCommentedUnmarshal(t *testing.T) {
tests := []struct {
name string
input string
output *complexCommentedData
errorExpected bool
err error
}{
{
name: "value1",
input: `{"ComplexCommented":"value1"}`,
output: &complexCommentedData{ComplexCommentedX: ComplexCommentedValue1},
errorExpected: false,
err: nil,
},
{
name: "value2",
input: `{"ComplexCommented":"value2"}`,
output: &complexCommentedData{ComplexCommentedX: ComplexCommentedValue2},
errorExpected: false,
err: nil,
},
{
name: "value3",
input: `{"ComplexCommented":"value3"}`,
output: &complexCommentedData{ComplexCommentedX: ComplexCommentedValue3},
errorExpected: false,
err: nil,
},
{
name: "notanCommented",
input: `{"ComplexCommented":"value4"}`,
output: nil,
errorExpected: true,
err: errors.New("value4 is not a valid ComplexCommented"),
},
}

for _, test := range tests {
t.Run(test.name, func(tt *testing.T) {
x := &complexCommentedData{}
err := json.Unmarshal([]byte(test.input), x)
if !test.errorExpected {
require.NoError(tt, err, "failed unmarshalling the json.")
assert.Equal(tt, test.output.ComplexCommentedX, x.ComplexCommentedX)
raw, err := json.Marshal(test.output)
require.NoError(tt, err, "failed marshalling back to json")
require.JSONEq(tt, test.input, string(raw), "json didn't match")
} else {
require.Error(tt, err)
assert.EqualError(tt, err, test.err.Error())
}
})
}
}
Loading

0 comments on commit 9b3947d

Please sign in to comment.