diff --git a/README.md b/README.md index f4ac14d..2cebb9f 100644 --- a/README.md +++ b/README.md @@ -1,32 +1,36 @@ # Enumer [![GoDoc](https://godoc.org/github.com/alvaroloes/enumer?status.svg)](https://godoc.org/github.com/alvaroloes/enumer) [![Go Report Card](https://goreportcard.com/badge/github.com/alvaroloes/enumer)](https://goreportcard.com/report/github.com/alvaroloes/enumer) [![cover.run go](https://cover.run/go/github.com/alvaroloes/enumer.svg?tag=golang-1.10)](https://cover.run/go/github.com/alvaroloes/enumer?tag=golang-1.10) + Enumer is a tool to generate Go code that adds useful methods to Go enums (constants with a specific type). It started as a fork of [Rob Pike’s Stringer tool](https://godoc.org/golang.org/x/tools/cmd/stringer). ## Generated functions and methods + When Enumer is applied to a type, it will generate: -* The following basic methods/functions: +- The following basic methods/functions: - * Method `String()`: returns the string representation of the enum value. This makes the enum conform -the `Stringer` interface, so whenever you print an enum value, you'll get the string name instead of a number. - * Function `String(s string)`: returns the enum value from its string representation. This is useful -when you need to read enum values from command line arguments, from a configuration file, or -from a REST API request... In short, from those places where using the real enum value (an integer) would -be almost meaningless or hard to trace or use by a human. - * Function `Values()`: returns a slice with all the values of the enum - * Method `IsA()`: returns true only if the current value is among the values of the enum. Useful for validations. -* When the flag `json` is provided, two additional methods will be generated, `MarshalJSON()` and `UnmarshalJSON()`. These make -the enum conform to the `json.Marshaler` and `json.Unmarshaler` interfaces. Very useful to use it in JSON APIs. -* When the flag `text` is provided, two additional methods will be generated, `MarshalText()` and `UnmarshalText()`. These make -the enum conform to the `encoding.TextMarshaler` and `encoding.TextUnmarshaler` interfaces. -**Note:** If you use your enum values as keys in a map and you encode the map as _JSON_, you need this flag set to true to properly -convert the map keys to json (strings). If not, the numeric values will be used instead -* When the flag `yaml` is provided, two additional methods will be generated, `MarshalYAML()` and `UnmarshalYAML()`. These make -the enum conform to the `gopkg.in/yaml.v2.Marshaler` and `gopkg.in/yaml.v2.Unmarshaler` interfaces. -* When the flag `sql` is provided, the methods for implementing the Scanner and Valuer interfaces will be also generated. -Useful when storing the enum in a database. + - Method `String()`: returns the string representation of the enum value. This makes the enum conform + the `Stringer` interface, so whenever you print an enum value, you'll get the string name instead of a number. + - Function `String(s string)`: returns the enum value from its string representation. This is useful + when you need to read enum values from command line arguments, from a configuration file, or + from a REST API request... In short, from those places where using the real enum value (an integer) would + be almost meaningless or hard to trace or use by a human. + - Function `Values()`: returns a slice with all the values of the enum + - Method `IsA()`: returns true only if the current value is among the values of the enum. Useful for validations. + +- When the flag `json` is provided, two additional methods will be generated, `MarshalJSON()` and `UnmarshalJSON()`. These make + the enum conform to the `json.Marshaler` and `json.Unmarshaler` interfaces. Very useful to use it in JSON APIs. +- When the flag `text` is provided, two additional methods will be generated, `MarshalText()` and `UnmarshalText()`. These make + the enum conform to the `encoding.TextMarshaler` and `encoding.TextUnmarshaler` interfaces. + **Note:** If you use your enum values as keys in a map and you encode the map as _JSON_, you need this flag set to true to properly + convert the map keys to json (strings). If not, the numeric values will be used instead +- When the flag `yaml` is provided, two additional methods will be generated, `MarshalYAML()` and `UnmarshalYAML()`. These make + the enum conform to the `gopkg.in/yaml.v2.Marshaler` and `gopkg.in/yaml.v2.Unmarshaler` interfaces. +- When the flag `sql` is provided, the methods for implementing the Scanner and Valuer interfaces will be also generated. + Useful when storing the enum in a database. For example, if we have an enum type called `Pill`, + ```go type Pill int @@ -38,21 +42,23 @@ const ( Acetaminophen = Paracetamol ) ``` + executing `enumer -type=Pill -json` will generate a new file with four basic methods and two extra for JSON: + ```go -func (i Pill) String() string { +func (i Pill) String() string { //... } -func PillString(s string) (Pill, error) { +func PillString(s string) (Pill, error) { //... } -func PillValues() []Pill { +func PillValues() []Pill { //... } -func (i Pill) IsAPill() bool { +func (i Pill) IsAPill() bool { //... } @@ -64,7 +70,9 @@ func (i *Pill) UnmarshalJSON(data []byte) error { //... } ``` + From now on, we can: + ```go // Convert any Pill value to string var aspirinString string = Aspirin.String() @@ -119,15 +127,29 @@ For example, the command `enumer -type=MyType -json -transform=snake` would gene ```go name := MyTypeValue.String() // name => "my_type_value" ``` + **Note**: The transformation only works form CamelCase to snake_case or kebab-case, not the other way around. +### Transformers + +- snake +- snake-upper +- kebab +- kebab-upper +- lower (Lowercase) +- upper (Uppercase) +- title (TitleCase) +- first (Use first character of string) +- first-lower (same as first only lower case) +- first-upper (same as first only upper case) + ## How to use + The usage of Enumer is the same as Stringer, so you can refer to the [Stringer docs](https://godoc.org/golang.org/x/tools/cmd/stringer) for more information. There are four boolean flags: `json`, `text`, `yaml` and `sql`. You can use any combination of them (i.e. `enumer -type=Pill -json -text`), - For enum string representation transformation the `transform` and `trimprefix` flags were added (i.e. `enumer -type=MyType -json -transform=snake`). Possible transform values are `snake` and `kebab` for transformation to snake_case and kebab-case accordingly. @@ -137,5 +159,6 @@ If a prefix is provided via the `trimprefix` flag, it will be trimmed from the s it is transformed). If a name doesn't have the prefix it will be passed unchanged. ## Inspiring projects -* [Stringer](https://godoc.org/golang.org/x/tools/cmd/stringer) -* [jsonenums](https://github.com/campoy/jsonenums) + +- [Stringer](https://godoc.org/golang.org/x/tools/cmd/stringer) +- [jsonenums](https://github.com/campoy/jsonenums) diff --git a/endtoend_test.go b/endtoend_test.go index 3397c06..10e0e39 100644 --- a/endtoend_test.go +++ b/endtoend_test.go @@ -57,13 +57,45 @@ func TestEndToEnd(t *testing.T) { t.Logf("cgo is no enabled for %s", name) continue } - // Names are known to be ASCII and long enough. - typeName := fmt.Sprintf("%c%s", name[0]+'A'-'a', name[1:len(name)-len(".go")]) - transformNameMethod := "noop" - if name == "transform.go" { - typeName = "CamelCaseValue" + // Names are known to be ASCII and long enough. + var typeName string + var transformNameMethod string + + switch name { + case "transform_snake.go": + typeName = "SnakeCaseValue" transformNameMethod = "snake" + case "transform_snake_upper.go": + typeName = "SnakeUpperCaseValue" + transformNameMethod = "snake-upper" + case "transform_kebab.go": + typeName = "KebabCaseValue" + transformNameMethod = "kebab" + case "transform_kebab_upper.go": + typeName = "KebabUpperCaseValue" + transformNameMethod = "kebab-upper" + case "transform_upper.go": + typeName = "UpperCaseValue" + transformNameMethod = "upper" + case "transform_lower.go": + typeName = "LowerCaseValue" + transformNameMethod = "lower" + case "transform_title.go": + typeName = "TitleCaseValue" + transformNameMethod = "title" + case "transform_first.go": + typeName = "FirstCaseValue" + transformNameMethod = "first" + case "transform_first_upper.go": + typeName = "FirstUpperCaseValue" + transformNameMethod = "first-upper" + case "transform_first_lower.go": + typeName = "FirstLowerCaseValue" + transformNameMethod = "first-lower" + default: + typeName = fmt.Sprintf("%c%s", name[0]+'A'-'a', name[1:len(name)-len(".go")]) + transformNameMethod = "noop" } stringerCompileAndRun(t, dir, stringer, typeName, name, transformNameMethod) diff --git a/stringer.go b/stringer.go index 6c267e5..e3ac91c 100644 --- a/stringer.go +++ b/stringer.go @@ -27,6 +27,7 @@ import ( "path/filepath" "sort" "strings" + "unicode/utf8" "github.com/pascaldekloe/name" ) @@ -240,18 +241,57 @@ func (pkg *Package) check(fs *token.FileSet, astFiles []*ast.File) { } func (g *Generator) transformValueNames(values []Value, transformMethod string) { - var sep rune + var fn func(src string) string switch transformMethod { case "snake": - sep = '_' + fn = func(s string) string { + return strings.ToLower(name.Delimit(s, '_')) + } + case "snake_upper", "snake-upper": + fn = func(s string) string { + return strings.ToUpper(name.Delimit(s, '_')) + } case "kebab": - sep = '-' + fn = func(s string) string { + return strings.ToLower(name.Delimit(s, '-')) + } + case "kebab_upper", "kebab-upper": + fn = func(s string) string { + return strings.ToUpper(name.Delimit(s, '-')) + } + case "upper": + fn = func(s string) string { + return strings.ToUpper(s) + } + case "lower": + fn = func(s string) string { + return strings.ToLower(s) + } + case "title": + fn = func(s string) string { + return strings.Title(s) + } + case "first": + fn = func(s string) string { + r, _ := utf8.DecodeRuneInString(s) + return string(r) + } + case "first_upper", "first-upper": + fn = func(s string) string { + r, _ := utf8.DecodeRuneInString(s) + return strings.ToUpper(string(r)) + } + case "first_lower", "first-lower": + fn = func(s string) string { + r, _ := utf8.DecodeRuneInString(s) + return strings.ToLower(string(r)) + } default: return } for i := range values { - values[i].name = strings.ToLower(name.Delimit(values[i].name, sep)) + values[i].name = fn(values[i].name) } } diff --git a/testdata/transform.go b/testdata/transform.go deleted file mode 100644 index 70d4b1f..0000000 --- a/testdata/transform.go +++ /dev/null @@ -1,25 +0,0 @@ -package main - -import "fmt" - -type CamelCaseValue int - -const ( - CamelCaseValueOne CamelCaseValue = iota - CamelCaseValueTwo - CamelCaseValueThree -) - -func main() { - ck(CamelCaseValueOne, "camel_case_value_one") - ck(CamelCaseValueTwo, "camel_case_value_two") - ck(CamelCaseValueThree, "camel_case_value_three") - ck(-127, "CamelCaseValue(-127)") - ck(127, "CamelCaseValue(127)") -} - -func ck(value CamelCaseValue, str string) { - if fmt.Sprint(value) != str { - panic("transform.go: " + str) - } -} diff --git a/testdata/transform_first.go b/testdata/transform_first.go new file mode 100644 index 0000000..f1f56c8 --- /dev/null +++ b/testdata/transform_first.go @@ -0,0 +1,25 @@ +package main + +import "fmt" + +type FirstCaseValue int + +const ( + Male FirstCaseValue = iota + Female + unknown +) + +func main() { + ck(Male, "M") + ck(Female, "F") + ck(unknown, "u") + ck(-127, "FirstCaseValue(-127)") + ck(127, "FirstCaseValue(127)") +} + +func ck(value FirstCaseValue, str string) { + if fmt.Sprint(value) != str { + panic("transform_first.go: " + str) + } +} diff --git a/testdata/transform_first_lower.go b/testdata/transform_first_lower.go new file mode 100644 index 0000000..865a265 --- /dev/null +++ b/testdata/transform_first_lower.go @@ -0,0 +1,25 @@ +package main + +import "fmt" + +type FirstLowerCaseValue int + +const ( + Male FirstLowerCaseValue = iota + Female + Unknown +) + +func main() { + ck(Male, "m") + ck(Female, "f") + ck(Unknown, "u") + ck(-127, "FirstLowerCaseValue(-127)") + ck(127, "FirstLowerCaseValue(127)") +} + +func ck(value FirstLowerCaseValue, str string) { + if fmt.Sprint(value) != str { + panic("transform_first_lower.go: " + str) + } +} diff --git a/testdata/transform_first_upper.go b/testdata/transform_first_upper.go new file mode 100644 index 0000000..f7282a5 --- /dev/null +++ b/testdata/transform_first_upper.go @@ -0,0 +1,25 @@ +package main + +import "fmt" + +type FirstUpperCaseValue int + +const ( + male FirstUpperCaseValue = iota + female + unknown +) + +func main() { + ck(male, "M") + ck(female, "F") + ck(unknown, "U") + ck(-127, "FirstUpperCaseValue(-127)") + ck(127, "FirstUpperCaseValue(127)") +} + +func ck(value FirstUpperCaseValue, str string) { + if fmt.Sprint(value) != str { + panic("transform_first_upper.go: " + str) + } +} diff --git a/testdata/transform_kebab.go b/testdata/transform_kebab.go new file mode 100644 index 0000000..8af3704 --- /dev/null +++ b/testdata/transform_kebab.go @@ -0,0 +1,25 @@ +package main + +import "fmt" + +type KebabCaseValue int + +const ( + KebabCaseValueOne KebabCaseValue = iota + KebabCaseValueTwo + KebabCaseValueThree +) + +func main() { + ck(KebabCaseValueOne, "kebab-case-value-one") + ck(KebabCaseValueTwo, "kebab-case-value-two") + ck(KebabCaseValueThree, "kebab-case-value-three") + ck(-127, "KebabCaseValue(-127)") + ck(127, "KebabCaseValue(127)") +} + +func ck(value KebabCaseValue, str string) { + if fmt.Sprint(value) != str { + panic("transform_kebab.go: " + str) + } +} diff --git a/testdata/transform_kebab_upper.go b/testdata/transform_kebab_upper.go new file mode 100644 index 0000000..1d15f9b --- /dev/null +++ b/testdata/transform_kebab_upper.go @@ -0,0 +1,25 @@ +package main + +import "fmt" + +type KebabUpperCaseValue int + +const ( + KebabUpperCaseValueOne KebabUpperCaseValue = iota + KebabUpperCaseValueTwo + KebabUpperCaseValueThree +) + +func main() { + ck(KebabUpperCaseValueOne, "KEBAB-UPPER-CASE-VALUE-ONE") + ck(KebabUpperCaseValueTwo, "KEBAB-UPPER-CASE-VALUE-TWO") + ck(KebabUpperCaseValueThree, "KEBAB-UPPER-CASE-VALUE-THREE") + ck(-127, "KebabUpperCaseValue(-127)") + ck(127, "KebabUpperCaseValue(127)") +} + +func ck(value KebabUpperCaseValue, str string) { + if fmt.Sprint(value) != str { + panic("transform_kebab_upper.go: " + str) + } +} diff --git a/testdata/transform_lower.go b/testdata/transform_lower.go new file mode 100644 index 0000000..e9ebe29 --- /dev/null +++ b/testdata/transform_lower.go @@ -0,0 +1,25 @@ +package main + +import "fmt" + +type LowerCaseValue int + +const ( + LowerCaseValueOne LowerCaseValue = iota + LowerCaseValueTwo + LowerCaseValueThree +) + +func main() { + ck(LowerCaseValueOne, "lowercasevalueone") + ck(LowerCaseValueTwo, "lowercasevaluetwo") + ck(LowerCaseValueThree, "lowercasevaluethree") + ck(-127, "LowerCaseValue(-127)") + ck(127, "LowerCaseValue(127)") +} + +func ck(value LowerCaseValue, str string) { + if fmt.Sprint(value) != str { + panic("transform_lower.go: " + str) + } +} diff --git a/testdata/transform_snake.go b/testdata/transform_snake.go new file mode 100644 index 0000000..c8cce88 --- /dev/null +++ b/testdata/transform_snake.go @@ -0,0 +1,25 @@ +package main + +import "fmt" + +type SnakeCaseValue int + +const ( + SnakeCaseValueOne SnakeCaseValue = iota + SnakeCaseValueTwo + SnakeCaseValueThree +) + +func main() { + ck(SnakeCaseValueOne, "snake_case_value_one") + ck(SnakeCaseValueTwo, "snake_case_value_two") + ck(SnakeCaseValueThree, "snake_case_value_three") + ck(-127, "SnakeCaseValue(-127)") + ck(127, "SnakeCaseValue(127)") +} + +func ck(value SnakeCaseValue, str string) { + if fmt.Sprint(value) != str { + panic("transform_snake.go: " + str) + } +} diff --git a/testdata/transform_snake_upper.go b/testdata/transform_snake_upper.go new file mode 100644 index 0000000..46e41e1 --- /dev/null +++ b/testdata/transform_snake_upper.go @@ -0,0 +1,25 @@ +package main + +import "fmt" + +type SnakeUpperCaseValue int + +const ( + SnakeUpperCaseValueOne SnakeUpperCaseValue = iota + SnakeUpperCaseValueTwo + SnakeUpperCaseValueThree +) + +func main() { + ck(SnakeUpperCaseValueOne, "SNAKE_UPPER_CASE_VALUE_ONE") + ck(SnakeUpperCaseValueTwo, "SNAKE_UPPER_CASE_VALUE_TWO") + ck(SnakeUpperCaseValueThree, "SNAKE_UPPER_CASE_VALUE_THREE") + ck(-127, "SnakeUpperCaseValue(-127)") + ck(127, "SnakeUpperCaseValue(127)") +} + +func ck(value SnakeUpperCaseValue, str string) { + if fmt.Sprint(value) != str { + panic("transform_snake_upper.go: " + str) + } +} diff --git a/testdata/transform_title.go b/testdata/transform_title.go new file mode 100644 index 0000000..a36a745 --- /dev/null +++ b/testdata/transform_title.go @@ -0,0 +1,25 @@ +package main + +import "fmt" + +type TitleCaseValue int + +const ( + titlecasevalueone TitleCaseValue = iota + titlecasevaluetwo + titlecasevaluethree +) + +func main() { + ck(titlecasevalueone, "Titlecasevalueone") + ck(titlecasevaluetwo, "Titlecasevaluetwo") + ck(titlecasevaluethree, "Titlecasevaluethree") + ck(-127, "TitleCaseValue(-127)") + ck(127, "TitleCaseValue(127)") +} + +func ck(value TitleCaseValue, str string) { + if fmt.Sprint(value) != str { + panic("transform_title.go: " + str) + } +} diff --git a/testdata/transform_upper.go b/testdata/transform_upper.go new file mode 100644 index 0000000..a18a4bf --- /dev/null +++ b/testdata/transform_upper.go @@ -0,0 +1,25 @@ +package main + +import "fmt" + +type UpperCaseValue int + +const ( + UpperCaseValueOne UpperCaseValue = iota + UpperCaseValueTwo + UpperCaseValueThree +) + +func main() { + ck(UpperCaseValueOne, "UPPERCASEVALUEONE") + ck(UpperCaseValueTwo, "UPPERCASEVALUETWO") + ck(UpperCaseValueThree, "UPPERCASEVALUETHREE") + ck(-127, "UpperCaseValue(-127)") + ck(127, "UpperCaseValue(127)") +} + +func ck(value UpperCaseValue, str string) { + if fmt.Sprint(value) != str { + panic("transform_upper.go: " + str) + } +}