Compare commits

...

4 Commits

Author SHA1 Message Date
tom twinkle 731fee370f
Merge 20b6478a41 into bcbe6173c3 2024-11-21 13:32:17 +00:00
Sam Myres bcbe6173c3
Merge pull request #95 from dmarkham/samiam2013/readme-revision
Note in the readme and flag that trimprefix accepts lists
2024-10-03 19:12:26 -05:00
samiam2013 0abe832021 note in the readme and flag help description that trimprefix accepts lists 2024-10-03 19:06:09 -05:00
tom twinkle 20b6478a41 Added ability to change marshalling to json, text, yaml to int 2024-02-15 15:25:08 +09:00
3 changed files with 95 additions and 13 deletions

View File

@ -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,7 +34,7 @@ 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
@ -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.
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).

View File

@ -3,6 +3,7 @@ package main
import "fmt"
// Arguments to format are:
//
// [1]: type name
const stringNameToValueMethod = `// %[1]sString retrieves an enum value from the enum constants string name.
// Throws an error if the param is not part of the enum.
@ -19,6 +20,7 @@ func %[1]sString(s string) (%[1]s, error) {
`
// Arguments to format are:
//
// [1]: type name
const stringValuesMethod = `// %[1]sValues returns all values of the enum
func %[1]sValues() []%[1]s {
@ -27,6 +29,7 @@ func %[1]sValues() []%[1]s {
`
// Arguments to format are:
//
// [1]: type name
const stringsMethod = `// %[1]sStrings returns a slice of all String values of the enum
func %[1]sStrings() []string {
@ -37,6 +40,7 @@ func %[1]sStrings() []string {
`
// Arguments to format are:
//
// [1]: type name
const stringBelongsMethodLoop = `// IsA%[1]s returns "true" if the value is listed in the enum definition. "false" otherwise
func (i %[1]s) IsA%[1]s() bool {
@ -50,6 +54,7 @@ func (i %[1]s) IsA%[1]s() bool {
`
// Arguments to format are:
//
// [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 {
@ -59,6 +64,7 @@ func (i %[1]s) IsA%[1]s() bool {
`
// Arguments to format are:
//
// [1]: type name
const altStringValuesMethod = `func (%[1]s) Values() []string {
return %[1]sStrings()
@ -144,6 +150,7 @@ func (g *Generator) printNamesSlice(runs [][]Value, typeName string, runsThresho
}
// Arguments to format are:
//
// [1]: type name
const jsonMethods = `
// MarshalJSON implements the json.Marshaler interface for %[1]s
@ -169,6 +176,31 @@ func (g *Generator) buildJSONMethods(runs [][]Value, typeName string, runsThresh
}
// Arguments to format are:
//
// [1]: type name
const intJsonMethods = `
// MarshalJSON implements the json.Marshaler interface for %[1]s
func (i %[1]s) MarshalJSON() ([]byte, error) {
return json.Marshal(int(i))
}
// UnmarshalJSON implements the json.Unmarshaler interface for %[1]s
func (i *%[1]s) UnmarshalJSON(data []byte) error {
var v int
if err := json.Unmarshal(data, &v); err != nil {
return fmt.Errorf("%[1]s should be a int, got %%s", data)
}
*i = %[1]s(v)
return nil
}
`
func (g *Generator) buildIntJSONMethods(runs [][]Value, typeName string, runsThreshold int) {
g.Printf(intJsonMethods, typeName)
}
// Arguments to format are:
//
// [1]: type name
const textMethods = `
// MarshalText implements the encoding.TextMarshaler interface for %[1]s
@ -189,6 +221,31 @@ func (g *Generator) buildTextMethods(runs [][]Value, typeName string, runsThresh
}
// Arguments to format are:
//
// [1]: type name
const intTextMethods = `
// MarshalText implements the encoding.TextMarshaler interface for %[1]s
func (i %[1]s) MarshalText() ([]byte, error) {
return []byte(strconv.Itoa(int(i))), nil
}
// UnmarshalText implements the encoding.TextUnmarshaler interface for %[1]s
func (i *%[1]s) UnmarshalText(text []byte) error {
v, err := strconv.Atoi(string(text))
if err != nil {
return err
}
*i = %[1]s(v)
return nil
}
`
func (g *Generator) buildIntTextMethods(runs [][]Value, typeName string, runsThreshold int) {
g.Printf(intTextMethods, typeName)
}
// Arguments to format are:
//
// [1]: type name
const yamlMethods = `
// MarshalYAML implements a YAML Marshaler for %[1]s
@ -212,3 +269,27 @@ func (i *%[1]s) UnmarshalYAML(unmarshal func(interface{}) error) error {
func (g *Generator) buildYAMLMethods(runs [][]Value, typeName string, runsThreshold int) {
g.Printf(yamlMethods, typeName)
}
// Arguments to format are:
//
// [1]: type name
const intYamlMethods = `
// MarshalYAML implements a YAML Marshaler for %[1]s
func (i %[1]s) MarshalYAML() (interface{}, error) {
return strconv.Itoa(int(i)), nil
}
// UnmarshalYAML implements a YAML Unmarshaler for %[1]s
func (i *%[1]s) UnmarshalYAML(unmarshal func(interface{}) error) error {
var v int
if err := unmarshal(&v); err != nil {
return err
}
*i = %[1]s(v)
return nil
}
`
func (g *Generator) buildIntYAMLMethods(runs [][]Value, typeName string, runsThreshold int) {
g.Printf(intYamlMethods, typeName)
}

View File

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