| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
You could use stringer here or a related alterative here. But they both depend on your code compiling cleanly.
Sometimes, the code generation gets in the way because you can't compile without the generated code and you can't generate the code without compiling.
That's where this tool steps in. It has a basic Go source code parser that finds idiomatic enumerations, as shown below. It runs on any source code that's "clean enough", even if the source code is still incomplete. Provided your type is ok and your const is complete, you'll be fine.
It will not handle C-style comments though, so these must not be present. The normal double-slash is OK.
go get github.com/rickb777/enumeration/v4 go install github.com/rickb777/enumeration/v4
You should see that the enumeration binary is now in the bin folder on your GOPATH. Make sure this is on your PATH so it can be run.
For example,
type Day int
const (
Sunday Day = iota
Monday
Tuesday
Wednesday
Thursday
Friday
Partyday
numberOfDays // this constant is not exported but is included in the enumeration
)this example comes from the Go Language Reference.
Another example, which also includes the optional go:generate line,
//go:generate enumeration -type Month
type Month uint
const (
January, February, March Month = 1, 2, 3
April, May, June Month = 4, 5, 6
July, August, September Month = 7, 8, 9
October, November, December Month = 10, 11, 12
)Note that the full Go language specification is flexible, allowing several alternative equivalent syntaxes for declaring constants. The enumeration tool only handles the most common two cases, illustrated above.
Above, Month is based on uint; the base type does not have to be an int; any integer or float base type can be used, for example
type Base float32
const (
// Nucleotide Molecular Weights, g/mol
A Base = 331.2
C Base = 307.2
G Base = 347.2
T Base = 322.2
)There is one other restriction: the type declaration must be before the constants, as it does in all the examples above. White-space should be canonically formatted using gofmt before using the tool (the results are unpredictable otherwise). (This is because of the limited parser used, for the reasons given earlier.)
Although the default behaviour is to generate strings for each enumeration value which match the constants you declared, you can take full control and override this however you need.
Just occasionally, you might have several enumerations in the same package and you need to avoid their identifiers clashing. This is easy to control using the -prefix and -suffix options. Here's an example using a suffix; prefixes are similar and you can have both if you want.
//go:generate enumeration -lc -type SalesChannel -suffix Sales
type SalesChannel int
const (
_ SalesChannel = iota
OnlineSales // represented as "online"
InstoreSales // represented as "instore"
TelephoneSales // represented as "telephone"
)If you want more control of the strings used for JSON and for SQL marshaling, structured comments can be used. In this example, the String() method, the JSON string and the SQL value will all be different values.
//go:generate enumeration -ic -type SalesChannel
type SalesChannel int
const (
_ SalesChannel = iota
Online // json:"webshop" sql:"o"
Instore // json:"store" sql:"s"
Telephone // json:"phone" sql:"t"
)Structured comments are deliberately similar to Go struct tags. The supported tags are text (for MarshalText/UnmarshalText), json (for MarshalJSON/UnmarshalJSON) and sql (for Value/Scan); these can be used in any combination; also all sets a value for all of them.
//go:generate enumeration -ic -type SalesChannel
type SalesChannel int
const (
_ SalesChannel = iota
Online // all:"webshop"
Instore // all:"store"
Telephone // all:"phone"
)When present, these tags override the -marshaltext, -marshaljson and/or -store options described below. They also override the -uc, -ic and -lc case modifiers on the corresponding marshal methods.
For example:
enumeration -i example/sample1.go -type Base -package example
Options are:
-type <name>
-plural <name>
-prefix <prefix>
-suffix <suffix>
-i <name> or -input <name>
-o <name> or -output <name>
-package <name>
-s
-poly
-lc
-uc
-ic
-unsnake
-alias <map-name>
-marshaltext <as>
-marshaljson <as>
-store <as>
-lenient
-f
-v
-version
The option parser will also infer the source and output file names, so it is also permitted to use
enumeration -type Base
when this matches your needs. This example would try to read from base.go and write to base_enum.go.
The generated code complements your type and const definitions as follows. Let's assume that you wrote the Day type above. You will get these simple methods and fields:
func (d Day) String() string
func (d Day) Ordinal() int
func (d Day) IsValid() bool
func (d Day) Int() int
func (d Day) Float() float64
var AllDays = []Day{ ... }
var AllDayEnums = enum.IntEnums{ ... }
Unless you specify 'simple mode' (-s), you will also get these methods:
func DayOf(o int) Day
func (d *Day) Parse(s string) error
func AsDay(s string) (Day, error), func MustParseDay(s string) Day
If you used the -marshaltext option or text struct tags, you will also get:
If you used the -marshaljson option or json struct tags, you will also get:
If you used the -store option or sql struct tags, you will also get:
Other items generated:
var dayMarshalNumber
func (d Day) Text() and func (d Day) JSON()
This tool is compatible with go generate - more. However, go generate may not always work if the code is still incomplete and doesn't yet compile, in which case you can just run enumeration directly on the command line.
Thanks are due to others for their earlier work. Notable is the earlier stringer here by Rob Pike or a related alterative here by Matt Sherman.
| Back | FazBrowse Home | New Git URL |