mirror of https://github.com/dmarkham/enumer.git
Compare commits
4 Commits
4dc55cbc4c
...
5e27ec3867
Author | SHA1 | Date |
---|---|---|
Zhongpeng Lin | 5e27ec3867 | |
Sam Myres | bcbe6173c3 | |
samiam2013 | 0abe832021 | |
Zhongpeng Lin | a0342d7531 |
11
README.md
11
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).
|
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 [directory]
|
||||||
Enumer [flags] -type T files... # Must be a single package
|
Enumer [flags] -type T files... # Must be a single package
|
||||||
For more information, see:
|
For more information, see:
|
||||||
|
@ -34,11 +34,11 @@ Flags:
|
||||||
-transform string
|
-transform string
|
||||||
enum item name transformation method. Default: noop (default "noop")
|
enum item name transformation method. Default: noop (default "noop")
|
||||||
-trimprefix string
|
-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
|
-type string
|
||||||
comma-separated list of type names; must be set
|
comma-separated list of type names; must be set
|
||||||
-values
|
-values
|
||||||
if true, alternative string values method will be generated. Default: false
|
if true, alternative string values method will be generated. Default: false
|
||||||
-yaml
|
-yaml
|
||||||
if true, yaml marshaling methods will be generated. Default: false
|
if true, yaml marshaling methods will be generated. Default: false
|
||||||
```
|
```
|
||||||
|
@ -208,7 +208,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.
|
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
|
||||||
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).
|
If a prefix is provided via the `addprefix` flag, it will be added to the start of each name (after trimming and after transforming).
|
||||||
|
|
||||||
|
|
19
stringer.go
19
stringer.go
|
@ -18,7 +18,6 @@ import (
|
||||||
"go/importer"
|
"go/importer"
|
||||||
"go/token"
|
"go/token"
|
||||||
"go/types"
|
"go/types"
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
@ -53,7 +52,7 @@ var (
|
||||||
altValuesFunc = flag.Bool("values", false, "if true, alternative string values method will be generated. Default: false")
|
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")
|
output = flag.String("output", "", "output file name; default srcdir/<type>_string.go")
|
||||||
transformMethod = flag.String("transform", "noop", "enum item name transformation method. Default: noop")
|
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: \"\"")
|
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")
|
linecomment = flag.Bool("linecomment", false, "use line comment text as printed text when present")
|
||||||
)
|
)
|
||||||
|
@ -149,7 +148,7 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write to tmpfile first
|
// Write to tmpfile first
|
||||||
tmpFile, err := ioutil.TempFile(dir, fmt.Sprintf("%s_enumer_", typs[0]))
|
tmpFile, err := os.CreateTemp("", fmt.Sprintf("%s_enumer_", typs[0]))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("creating temporary file for output: %s", err)
|
log.Fatalf("creating temporary file for output: %s", err)
|
||||||
}
|
}
|
||||||
|
@ -774,9 +773,9 @@ func (g *Generator) buildOneRun(runs [][]Value, typeName string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Arguments to format are:
|
// Arguments to format are:
|
||||||
// [1]: type name
|
// [1]: type name
|
||||||
// [2]: size of index element (8 for uint8 etc.)
|
// [2]: size of index element (8 for uint8 etc.)
|
||||||
// [3]: less than zero check (for signed types)
|
// [3]: less than zero check (for signed types)
|
||||||
const stringOneRun = `func (i %[1]s) String() string {
|
const stringOneRun = `func (i %[1]s) String() string {
|
||||||
if %[3]si >= %[1]s(len(_%[1]sIndex)-1) {
|
if %[3]si >= %[1]s(len(_%[1]sIndex)-1) {
|
||||||
return fmt.Sprintf("%[1]s(%%d)", i)
|
return fmt.Sprintf("%[1]s(%%d)", i)
|
||||||
|
@ -786,10 +785,10 @@ const stringOneRun = `func (i %[1]s) String() string {
|
||||||
`
|
`
|
||||||
|
|
||||||
// Arguments to format are:
|
// Arguments to format are:
|
||||||
// [1]: type name
|
// [1]: type name
|
||||||
// [2]: lowest defined value for type, as a string
|
// [2]: lowest defined value for type, as a string
|
||||||
// [3]: size of index element (8 for uint8 etc.)
|
// [3]: size of index element (8 for uint8 etc.)
|
||||||
// [4]: less than zero check (for signed types)
|
// [4]: less than zero check (for signed types)
|
||||||
const stringOneRunWithOffset = `func (i %[1]s) String() string {
|
const stringOneRunWithOffset = `func (i %[1]s) String() string {
|
||||||
i -= %[2]s
|
i -= %[2]s
|
||||||
if %[4]si >= %[1]s(len(_%[1]sIndex)-1) {
|
if %[4]si >= %[1]s(len(_%[1]sIndex)-1) {
|
||||||
|
|
Loading…
Reference in New Issue