Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add transform to MACRO_CASE and phrase #51

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ After that, the `enumer` executable will be in "$GOPATH/bin" folder and you can
## 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.
Expand All @@ -27,7 +27,7 @@ be almost meaningless or hard to trace or use by a human.
* 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.
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
Expand All @@ -49,19 +49,19 @@ const (
```
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 {
//...
}

Expand Down Expand Up @@ -128,7 +128,7 @@ 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.
**Note**: The transformation only works from CamelCase to snake_case or kebab-case, not the other way around.

## 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)
Expand All @@ -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
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.

If a prefix is provided via the `trimprefix` flag, it will be trimmed from the start of each name (before
Expand Down
2 changes: 1 addition & 1 deletion endtoend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,4 @@ func runInDir(dir, name string, arg ...string) error {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
}
2 changes: 1 addition & 1 deletion enumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (i %[1]s) IsA%[1]s() bool {
// [1]: type name
const stringBelongsMethodSet = `// IsA%[1]s returns "true" if the value is listed in the enum definition. "false" otherwise
func (i %[1]s) IsA%[1]s() bool {
_, ok := _%[1]sMap[i]
_, ok := _%[1]sMap[i]
return ok
}
`
Expand Down
2 changes: 1 addition & 1 deletion sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const scanMethod = `func (i *%[1]s) Scan(value interface{}) error {
if err != nil {
return err
}

*i = val
return nil
}
Expand Down
8 changes: 8 additions & 0 deletions stringer.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,14 @@ func (g *Generator) transformValueNames(values []Value, transformMethod string)
sep = '_'
case "kebab":
sep = '-'
case "phrase":
sep = ' '
case "macro":
sep = '_'
for i := range values {
values[i].name = strings.ToUpper(name.Delimit(values[i].name, sep))
}
return
default:
return
}
Expand Down