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 support for -transform=space #58

Open
wants to merge 3 commits 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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,14 @@ 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 snake_case, kebab-case, or space case you can use the `transform` flag.

For example, the command `enumer -type=MyType -json -transform=snake` would generate the following string representation:

```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 form CamelCase to snake_case, kebab-case, or space 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,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 `snake`, `kebab`, and `space` for transformation to snake_case, kebab-case, and space case 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
5 changes: 4 additions & 1 deletion endtoend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ func TestEndToEnd(t *testing.T) {
if name == "transform.go" {
typeName = "CamelCaseValue"
transformNameMethod = "snake"
} else if name == "transformspace.go" {
typeName = "CamelCaseValue"
transformNameMethod = "space"
}

stringerCompileAndRun(t, dir, stringer, typeName, name, transformNameMethod)
Expand Down Expand Up @@ -122,4 +125,4 @@ func runInDir(dir, name string, arg ...string) error {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
}
2 changes: 2 additions & 0 deletions stringer.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ func (g *Generator) transformValueNames(values []Value, transformMethod string)
sep = '_'
case "kebab":
sep = '-'
case "space":
sep = ' '
default:
return
}
Expand Down
25 changes: 25 additions & 0 deletions testdata/transformspace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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("transformspace.go: " + str)
}
}