Add transform to MACRO_CASE and phrase

This commit is contained in:
Adam Bouqdib 2019-08-21 14:35:35 +01:00
parent d27f80ae12
commit b59fe7c6e6
5 changed files with 20 additions and 11 deletions

View File

@ -128,7 +128,7 @@ For example, the command `enumer -type=MyType -json -transform=snake` would gene
```go ```go
name := MyTypeValue.String() // name => "my_type_value" 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.
## How to use ## 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) 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)
@ -139,7 +139,8 @@ There are four boolean flags: `json`, `text`, `yaml` and `sql`. You can use any
For enum string representation transformation the `transform` and `trimprefix` flags For enum string representation transformation the `transform` and `trimprefix` flags
were added (i.e. `enumer -type=MyType -json -transform=snake`). 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 `snake`, `kebab` `macro` and `phrase` for transformation
to snake_case, kebab-case, MACRO_CASE or a phrase (i.e. space-separated words) accordingly.
The default value for `transform` flag is `noop` which means no transformation will be performed. 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 If a prefix is provided via the `trimprefix` flag, it will be trimmed from the start of each name (before

View File

@ -316,6 +316,14 @@ func (g *Generator) transformValueNames(values []Value, transformMethod string)
sep = '_' sep = '_'
case "kebab": case "kebab":
sep = '-' sep = '-'
case "phrase":
sep = ' '
case "macro":
sep = '_'
for i := range values {
values[i].name = strings.ToUpper(name.Delimit(values[i].name, sep))
}
return
default: default:
return return
} }