diff --git a/README.md b/README.md index 75eb42d..521fc70 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,7 @@ When Enumer is applied to a type, it will generate: - 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 @@ -120,7 +121,7 @@ name := MyTypeValue.String() // name => "MyTypeValue" Sometimes you need to use some other string representation format than CamelCase (i.e. in JSON). -To transform it from CamelCase to snake_case or kebab-case, you can use the `transform` flag. +To transform it from CamelCase to another format, you can use the `transform` flag. For example, the command `enumer -type=MyType -json -transform=snake` would generate the following string representation: @@ -128,7 +129,7 @@ For example, the command `enumer -type=MyType -json -transform=snake` would gene 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. +**Note**: The transformation only works from CamelCase to snake_case or kebab-case, not the other way around. ### Transformers @@ -143,6 +144,7 @@ name := MyTypeValue.String() // name => "my_type_value" - first (Use first character of string) - first-lower (same as first only lower case) - first-upper (same as first only upper case) +- whitespace ## How to use @@ -153,7 +155,7 @@ There are four boolean flags: `json`, `text`, `yaml` and `sql`. You can use any 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. +Possible transform values are listed above in the [transformers](#transformers) section. The default value for `transform` flag is `noop` which means no transformation will be performed. If a prefix is provided via the `trimprefix` flag, it will be trimmed from the start of each name (before diff --git a/endtoend_test.go b/endtoend_test.go index 6c2aae2..da9ba60 100644 --- a/endtoend_test.go +++ b/endtoend_test.go @@ -108,6 +108,9 @@ func TestEndToEnd(t *testing.T) { case "transform_first_lower.go": typeName = "FirstLowerCaseValue" transformNameMethod = "first-lower" + case "transform_whitespace.go": + typeName = "WhitespaceSeparatedValue" + transformNameMethod = "whitespace" default: typeName = fmt.Sprintf("%c%s", name[0]+'A'-'a', name[1:len(name)-len(".go")]) transformNameMethod = "noop" diff --git a/stringer.go b/stringer.go index 71a6fa1..1527b7d 100644 --- a/stringer.go +++ b/stringer.go @@ -366,6 +366,10 @@ func (g *Generator) transformValueNames(values []Value, transformMethod string) r, _ := utf8.DecodeRuneInString(s) return strings.ToLower(string(r)) } + case "whitespace": + fn = func(s string) string { + return strings.ToLower(name.Delimit(s, ' ')) + } default: return } diff --git a/testdata/transform_whitespace.go b/testdata/transform_whitespace.go new file mode 100644 index 0000000..b3d552d --- /dev/null +++ b/testdata/transform_whitespace.go @@ -0,0 +1,25 @@ +package main + +import "fmt" + +type WhitespaceSeparatedValue int + +const ( + WhitespaceSeparatedValueOne WhitespaceSeparatedValue = iota + WhitespaceSeparatedValueTwo + WhitespaceSeparatedValueThree +) + +func main() { + ck(WhitespaceSeparatedValueOne, "whitespace separated value one") + ck(WhitespaceSeparatedValueTwo, "whitespace separated value two") + ck(WhitespaceSeparatedValueThree, "whitespace separated value three") + ck(-127, "WhitespaceSeparatedValue(-127)") + ck(127, "WhitespaceSeparatedValue(127)") +} + +func ck(value WhitespaceSeparatedValue, str string) { + if fmt.Sprint(value) != str { + panic("transform_whitespace.go: " + str) + } +}