mirror of https://github.com/dmarkham/enumer.git
Compare commits
4 Commits
0d6f3f8385
...
eee440fa28
Author | SHA1 | Date |
---|---|---|
Austin Nicholas | eee440fa28 | |
Sam Myres | bcbe6173c3 | |
samiam2013 | 0abe832021 | |
Austin Nicholas | 7e279f03c6 |
13
README.md
13
README.md
|
@ -7,9 +7,9 @@ This was again forked here as (https://github.com/dmarkham/enumer) picking up wh
|
|||
|
||||
|
||||
```
|
||||
$ ./enumer --help
|
||||
$ enumer --help
|
||||
Enumer is a tool to generate Go code that adds useful methods to Go enums (constants with a specific type).
|
||||
Usage of ./enumer:
|
||||
Usage of enumer:
|
||||
Enumer [flags] -type T [directory]
|
||||
Enumer [flags] -type T files... # Must be a single package
|
||||
For more information, see:
|
||||
|
@ -34,11 +34,11 @@ Flags:
|
|||
-transform string
|
||||
enum item name transformation method. Default: noop (default "noop")
|
||||
-trimprefix string
|
||||
transform each item name by removing a prefix. Default: ""
|
||||
transform each item name by removing a prefix or comma separated list of prefixes. Default: ""
|
||||
-type string
|
||||
comma-separated list of type names; must be set
|
||||
-values
|
||||
if true, alternative string values method will be generated. Default: false
|
||||
if true, alternative string values method will be generated. Default: false
|
||||
-yaml
|
||||
if true, yaml marshaling methods will be generated. Default: false
|
||||
```
|
||||
|
@ -183,6 +183,8 @@ name := MyTypeValue.String() // name => "my_type_value"
|
|||
- snake-upper
|
||||
- kebab
|
||||
- kebab-upper
|
||||
- dot (dot.case)
|
||||
- dot-upper (DOT.CASE)
|
||||
- lower (lowercase)
|
||||
- upper (UPPERCASE)
|
||||
- title (TitleCase)
|
||||
|
@ -208,7 +210,8 @@ Possible transform values are listed above in the [transformers](#transformers)
|
|||
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
|
||||
it is transformed). If a name doesn't have the prefix it will be passed unchanged.
|
||||
it is transformed). You can trim multiple prefixes by passing a comma separated list.
|
||||
If a name doesn't have the prefix it will be passed unchanged.
|
||||
|
||||
If a prefix is provided via the `addprefix` flag, it will be added to the start of each name (after trimming and after transforming).
|
||||
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
// go command is not available on android
|
||||
|
||||
//go:build !android
|
||||
// +build !android
|
||||
|
||||
package main
|
||||
|
@ -89,6 +90,12 @@ func TestEndToEnd(t *testing.T) {
|
|||
case "transform_kebab_upper.go":
|
||||
typeName = "KebabUpperCaseValue"
|
||||
transformNameMethod = "kebab-upper"
|
||||
case "transform_dot.go":
|
||||
typeName = "DotCaseValue"
|
||||
transformNameMethod = "dot"
|
||||
case "transform_dot_upper.go":
|
||||
typeName = "DotUpperCaseValue"
|
||||
transformNameMethod = "dot-upper"
|
||||
case "transform_upper.go":
|
||||
typeName = "UpperCaseValue"
|
||||
transformNameMethod = "upper"
|
||||
|
|
24
stringer.go
24
stringer.go
|
@ -53,7 +53,7 @@ var (
|
|||
altValuesFunc = flag.Bool("values", false, "if true, alternative string values method will be generated. Default: false")
|
||||
output = flag.String("output", "", "output file name; default srcdir/<type>_string.go")
|
||||
transformMethod = flag.String("transform", "noop", "enum item name transformation method. Default: noop")
|
||||
trimPrefix = flag.String("trimprefix", "", "transform each item name by removing a prefix. Default: \"\"")
|
||||
trimPrefix = flag.String("trimprefix", "", "transform each item name by removing a prefix or comma separated list of prefixes. Default: \"\"")
|
||||
addPrefix = flag.String("addprefix", "", "transform each item name by adding a prefix. Default: \"\"")
|
||||
linecomment = flag.Bool("linecomment", false, "use line comment text as printed text when present")
|
||||
)
|
||||
|
@ -343,6 +343,14 @@ func (g *Generator) transformValueNames(values []Value, transformMethod string)
|
|||
fn = func(s string) string {
|
||||
return strings.ToUpper(name.Delimit(s, '-'))
|
||||
}
|
||||
case "dot":
|
||||
fn = func(s string) string {
|
||||
return strings.ToLower(name.Delimit(s, '.'))
|
||||
}
|
||||
case "dot_upper", "dot-upper":
|
||||
fn = func(s string) string {
|
||||
return strings.ToUpper(name.Delimit(s, '.'))
|
||||
}
|
||||
case "upper":
|
||||
fn = func(s string) string {
|
||||
return strings.ToUpper(s)
|
||||
|
@ -774,9 +782,9 @@ func (g *Generator) buildOneRun(runs [][]Value, typeName string) {
|
|||
}
|
||||
|
||||
// Arguments to format are:
|
||||
// [1]: type name
|
||||
// [2]: size of index element (8 for uint8 etc.)
|
||||
// [3]: less than zero check (for signed types)
|
||||
// [1]: type name
|
||||
// [2]: size of index element (8 for uint8 etc.)
|
||||
// [3]: less than zero check (for signed types)
|
||||
const stringOneRun = `func (i %[1]s) String() string {
|
||||
if %[3]si >= %[1]s(len(_%[1]sIndex)-1) {
|
||||
return fmt.Sprintf("%[1]s(%%d)", i)
|
||||
|
@ -786,10 +794,10 @@ const stringOneRun = `func (i %[1]s) String() string {
|
|||
`
|
||||
|
||||
// Arguments to format are:
|
||||
// [1]: type name
|
||||
// [2]: lowest defined value for type, as a string
|
||||
// [3]: size of index element (8 for uint8 etc.)
|
||||
// [4]: less than zero check (for signed types)
|
||||
// [1]: type name
|
||||
// [2]: lowest defined value for type, as a string
|
||||
// [3]: size of index element (8 for uint8 etc.)
|
||||
// [4]: less than zero check (for signed types)
|
||||
const stringOneRunWithOffset = `func (i %[1]s) String() string {
|
||||
i -= %[2]s
|
||||
if %[4]si >= %[1]s(len(_%[1]sIndex)-1) {
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type DotCaseValue int
|
||||
|
||||
const (
|
||||
DotCaseValueOne DotCaseValue = iota
|
||||
DotCaseValueTwo
|
||||
DotCaseValueThree
|
||||
)
|
||||
|
||||
func main() {
|
||||
ck(DotCaseValueOne, "dot.case.value.one")
|
||||
ck(DotCaseValueTwo, "dot.case.value.two")
|
||||
ck(DotCaseValueThree, "dot.case.value.three")
|
||||
ck(-127, "DotCaseValue(-127)")
|
||||
ck(127, "DotCaseValue(127)")
|
||||
}
|
||||
|
||||
func ck(value DotCaseValue, str string) {
|
||||
if fmt.Sprint(value) != str {
|
||||
panic("transform_dot.go: " + str)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type DotUpperCaseValue int
|
||||
|
||||
const (
|
||||
DotUpperCaseValueOne DotUpperCaseValue = iota
|
||||
DotUpperCaseValueTwo
|
||||
DotUpperCaseValueThree
|
||||
)
|
||||
|
||||
func main() {
|
||||
ck(DotUpperCaseValueOne, "DOT.UPPER.CASE.VALUE.ONE")
|
||||
ck(DotUpperCaseValueTwo, "DOT.UPPER.CASE.VALUE.TWO")
|
||||
ck(DotUpperCaseValueThree, "DOT.UPPER.CASE.VALUE.THREE")
|
||||
ck(-127, "DotUpperCaseValue(-127)")
|
||||
ck(127, "DotUpperCaseValue(127)")
|
||||
}
|
||||
|
||||
func ck(value DotUpperCaseValue, str string) {
|
||||
if fmt.Sprint(value) != str {
|
||||
panic("transform_dot_upper.go: " + str)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue