resolve some conflicts

This commit is contained in:
Dan Markham 2019-04-04 17:57:54 -07:00
commit 107292ddbc
No known key found for this signature in database
GPG Key ID: 7994037517927D77
4 changed files with 37 additions and 3 deletions

View File

@ -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. - 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. Useful when storing the enum in a database.
For example, if we have an enum type called `Pill`, For example, if we have an enum type called `Pill`,
```go ```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). 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: 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" 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 ### Transformers
@ -143,6 +144,7 @@ name := MyTypeValue.String() // name => "my_type_value"
- first (Use first character of string) - first (Use first character of string)
- first-lower (same as first only lower case) - first-lower (same as first only lower case)
- first-upper (same as first only upper case) - first-upper (same as first only upper case)
- whitespace
## How to use ## 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 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 listed above in the [transformers](#transformers) section.
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

@ -108,6 +108,9 @@ func TestEndToEnd(t *testing.T) {
case "transform_first_lower.go": case "transform_first_lower.go":
typeName = "FirstLowerCaseValue" typeName = "FirstLowerCaseValue"
transformNameMethod = "first-lower" transformNameMethod = "first-lower"
case "transform_whitespace.go":
typeName = "WhitespaceSeparatedValue"
transformNameMethod = "whitespace"
default: default:
typeName = fmt.Sprintf("%c%s", name[0]+'A'-'a', name[1:len(name)-len(".go")]) typeName = fmt.Sprintf("%c%s", name[0]+'A'-'a', name[1:len(name)-len(".go")])
transformNameMethod = "noop" transformNameMethod = "noop"

View File

@ -366,6 +366,10 @@ func (g *Generator) transformValueNames(values []Value, transformMethod string)
r, _ := utf8.DecodeRuneInString(s) r, _ := utf8.DecodeRuneInString(s)
return strings.ToLower(string(r)) return strings.ToLower(string(r))
} }
case "whitespace":
fn = func(s string) string {
return strings.ToLower(name.Delimit(s, ' '))
}
default: default:
return return
} }

25
testdata/transform_whitespace.go vendored Normal file
View File

@ -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)
}
}