2021-06-23 07:20:35 +03:00
# Enumer [![GoDoc](https://godoc.org/github.com/dmarkham/enumer?status.svg)](https://godoc.org/github.com/dmarkham/enumer) [![Go Report Card](https://goreportcard.com/badge/github.com/dmarkham/enumer)](https://goreportcard.com/report/github.com/dmarkham/enumer) [![GitHub Release](https://img.shields.io/github/release/dmarkham/enumer.svg)](https://github.com/dmarkham/enumer/releases)[![Build Status](https://travis-ci.com/dmarkham/enumer.svg?branch=master)](https://travis-ci.com/dmarkham/enumer)
2019-04-05 11:15:09 +03:00
2019-01-17 15:07:52 +03:00
2016-01-21 13:49:12 +03:00
Enumer is a tool to generate Go code that adds useful methods to Go enums (constants with a specific type).
2019-04-05 08:53:44 +03:00
It started as a fork of [Rob Pike’ s Stringer tool ](https://godoc.org/golang.org/x/tools/cmd/stringer )
2019-04-05 09:54:40 +03:00
maintained by [Álvaro López Espinosa ](https://github.com/alvaroloes/enumer ).
This was again forked here as (https://github.com/dmarkham/enumer) picking up where Álvaro left off.
2019-04-05 08:53:44 +03:00
2015-12-29 16:27:33 +03:00
2019-04-24 07:21:16 +03:00
```
$ ./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:
Enumer [flags] -type T [directory]
Enumer [flags] -type T files... # Must be a single package
For more information, see:
http://godoc.org/github.com/dmarkham/enumer
Flags:
-addprefix string
transform each item name by adding a prefix. Default: ""
-comment value
comments to include in generated code, can repeat. Default: ""
2021-01-31 10:37:57 +03:00
-gqlgen
if true, GraphQL marshaling methods for gqlgen will be generated. Default: false
2019-04-24 07:21:16 +03:00
-json
if true, json marshaling methods will be generated. Default: false
-linecomment
use line comment text as printed text when present
2024-02-15 10:19:41 +03:00
-tostring
if true, ToString function that returns an number-based string will be generated. Default: false
2019-04-24 07:21:16 +03:00
-output string
output file name; default srcdir/< type > _string.go
-sql
if true, the Scanner and Valuer interface will be implemented.
-text
if true, text marshaling methods will be generated. Default: false
-transform string
enum item name transformation method. Default: noop (default "noop")
-trimprefix string
transform each item name by removing a prefix. Default: ""
-type string
comma-separated list of type names; must be set
2022-11-13 17:41:23 +03:00
-values
if true, alternative string values method will be generated. Default: false
2019-04-24 07:21:16 +03:00
-yaml
if true, yaml marshaling methods will be generated. Default: false
```
2017-02-01 11:49:25 +03:00
## Generated functions and methods
2019-01-17 15:07:52 +03:00
2016-05-21 15:34:12 +03:00
When Enumer is applied to a type, it will generate:
2016-01-21 02:09:18 +03:00
2019-01-17 15:07:52 +03:00
- The following basic methods/functions:
- Method `String()` : returns the string representation of the enum value. This makes the enum conform
the `Stringer` interface, so whenever you print an enum value, you'll get the string name instead of a number.
- Function `<Type>String(s string)` : returns the enum value from its string representation. This is useful
when you need to read enum values from command line arguments, from a configuration file, or
from a REST API request... In short, from those places where using the real enum value (an integer) would
2019-05-06 01:07:38 +03:00
be almost meaningless or hard to trace or use by a human. `s` string is Case Insensitive.
2019-01-17 15:07:52 +03:00
- Function `<Type>Values()` : returns a slice with all the values of the enum
2019-04-08 09:56:38 +03:00
- Function `<Type>Strings()` : returns a slice with all the Strings of the enum
2019-01-17 15:07:52 +03:00
- Method `IsA<Type>()` : returns true only if the current value is among the values of the enum. Useful for validations.
- When the flag `json` is provided, two additional methods will be generated, `MarshalJSON()` and `UnmarshalJSON()` . These make
the enum conform to the `json.Marshaler` and `json.Unmarshaler` interfaces. Very useful to use it in JSON APIs.
- When the flag `text` is provided, two additional methods will be generated, `MarshalText()` and `UnmarshalText()` . These make
the enum conform to the `encoding.TextMarshaler` and `encoding.TextUnmarshaler` interfaces.
**Note:** If you use your enum values as keys in a map and you encode the map as _JSON_ , you need this flag set to true to properly
convert the map keys to json (strings). If not, the numeric values will be used instead
- When the flag `yaml` is provided, two additional methods will be generated, `MarshalYAML()` and `UnmarshalYAML()` . These make
the enum conform to the `gopkg.in/yaml.v2.Marshaler` and `gopkg.in/yaml.v2.Unmarshaler` interfaces.
2019-04-08 09:56:38 +03:00
- When the flag `sql` is provided, the methods for implementing the `Scanner` and `Valuer` interfaces.
2019-01-17 15:07:52 +03:00
Useful when storing the enum in a database.
2024-02-15 10:19:41 +03:00
- When the flag `tostring` is provided, the methods for implementing the `ToString` interfaces.
the `ToString` interface, Instead of an enumerated string, get a defined number-based string.
2015-12-31 17:36:39 +03:00
2019-02-12 18:28:13 +03:00
2015-12-31 17:36:39 +03:00
For example, if we have an enum type called `Pill` ,
2019-01-17 15:07:52 +03:00
2015-12-31 17:41:41 +03:00
```go
2015-12-31 17:36:39 +03:00
type Pill int
2015-12-29 16:27:33 +03:00
2015-12-31 17:36:39 +03:00
const (
Placebo Pill = iota
Aspirin
Ibuprofen
Paracetamol
Acetaminophen = Paracetamol
)
```
2019-01-17 15:07:52 +03:00
2018-04-12 01:50:53 +03:00
executing `enumer -type=Pill -json` will generate a new file with four basic methods and two extra for JSON:
2019-01-17 15:07:52 +03:00
2015-12-31 17:41:41 +03:00
```go
2019-01-17 15:07:52 +03:00
func (i Pill) String() string {
2018-03-09 17:44:41 +03:00
//...
}
2019-01-17 15:07:52 +03:00
func PillString(s string) (Pill, error) {
2018-03-09 17:44:41 +03:00
//...
}
2019-01-17 15:07:52 +03:00
func PillValues() []Pill {
2018-03-09 17:44:41 +03:00
//...
2015-12-29 16:27:33 +03:00
}
2019-04-08 09:56:38 +03:00
func PillStrings() []string {
//...
}
2019-01-17 15:07:52 +03:00
func (i Pill) IsAPill() bool {
2018-03-09 17:44:41 +03:00
//...
2015-12-29 16:27:33 +03:00
}
2016-01-21 02:09:18 +03:00
func (i Pill) MarshalJSON() ([]byte, error) {
//...
}
func (i *Pill) UnmarshalJSON(data []byte) error {
//...
}
2015-12-29 16:27:33 +03:00
```
2019-01-17 15:07:52 +03:00
2015-12-31 17:36:39 +03:00
From now on, we can:
2019-01-17 15:07:52 +03:00
2015-12-31 17:41:41 +03:00
```go
2015-12-31 17:36:39 +03:00
// Convert any Pill value to string
var aspirinString string = Aspirin.String()
// (or use it in any place where a Stringer is accepted)
fmt.Println("I need ", Paracetamol) // Will print "I need Paracetamol"
// Convert a string with the enum name to the corresponding enum value
2019-05-06 01:07:38 +03:00
pill, err := PillString("Ibuprofen") // "ibuprofen" will also work.
2015-12-31 17:36:39 +03:00
if err != nil {
fmt.Println("Unrecognized pill: ", err)
return
}
// Now pill == Ibuprofen
2016-01-21 02:09:18 +03:00
2018-03-09 17:44:41 +03:00
// Get all the values of the string
allPills := PillValues()
fmt.Println(allPills) // Will print [Placebo Aspirin Ibuprofen Paracetamol]
// Check if a value belongs to the Pill enum values
var notAPill Pill = 42
if (notAPill.IsAPill()) {
fmt.Println(notAPill, "is not a value of the Pill enum")
}
2016-01-21 02:15:57 +03:00
// Marshal/unmarshal to/from json strings, either directly or automatically when
// the enum is a field of a struct
pillJSON := Aspirin.MarshalJSON()
// Now pillJSON == `"Aspirin"`
2015-12-31 17:36:39 +03:00
```
2016-01-21 02:09:18 +03:00
The generated code is exactly the same as the Stringer tool plus the mentioned additions, so you can use
2015-12-31 17:36:39 +03:00
**Enumer** where you are already using **Stringer** without any code change.
2015-12-29 16:27:33 +03:00
2017-02-09 14:02:18 +03:00
## Transforming the string representation of the enum value
2017-02-01 11:49:25 +03:00
2017-02-09 14:02:18 +03:00
By default, Enumer uses the same name of the enum value for generating the string representation (usually CamelCase in Go).
2017-02-01 11:49:25 +03:00
```go
type MyType int
...
name := MyTypeValue.String() // name => "MyTypeValue"
```
2017-02-09 14:02:18 +03:00
Sometimes you need to use some other string representation format than CamelCase (i.e. in JSON).
2017-11-03 00:02:53 +03:00
2019-02-12 18:28:13 +03:00
To transform it from CamelCase to another format, you can use the `transform` flag.
2017-02-01 11:49:25 +03:00
2017-02-09 14:02:18 +03:00
For example, the command `enumer -type=MyType -json -transform=snake` would generate the following string representation:
2017-02-01 11:49:25 +03:00
```go
name := MyTypeValue.String() // name => "my_type_value"
```
2019-01-17 15:07:52 +03:00
2019-02-12 18:28:13 +03:00
**Note**: The transformation only works from CamelCase to snake_case or kebab-case, not the other way around.
2017-02-01 11:49:25 +03:00
2019-01-17 15:07:52 +03:00
### Transformers
- snake
- snake-upper
- kebab
- kebab-upper
2019-03-22 13:37:02 +03:00
- lower (lowercase)
- upper (UPPERCASE)
2019-01-17 15:07:52 +03:00
- title (TitleCase)
2019-03-22 13:37:02 +03:00
- title-lower (titleCase)
2019-01-17 15:07:52 +03:00
- first (Use first character of string)
- first-lower (same as first only lower case)
- first-upper (same as first only upper case)
2019-02-12 18:28:13 +03:00
- whitespace
2019-01-17 15:07:52 +03:00
2015-12-31 17:36:39 +03:00
## How to use
2019-01-17 15:07:52 +03:00
2022-02-04 19:25:17 +03:00
For a module-aware repo with `enumer` in the `go.mod` file, generation can be called by adding the following to a `.go` source file:
```golang
//go:generate go run github.com/dmarkham/enumer -type=YOURTYPE
```
2018-03-08 23:08:41 +03:00
There are four boolean flags: `json` , `text` , `yaml` and `sql` . You can use any combination of them (i.e. `enumer -type=Pill -json -text` ),
2018-02-06 03:48:43 +03:00
For enum string representation transformation the `transform` and `trimprefix` flags
2017-04-10 21:37:23 +03:00
were added (i.e. `enumer -type=MyType -json -transform=snake` ).
2019-02-12 18:28:13 +03:00
Possible transform values are listed above in the [transformers ](#transformers ) section.
2017-02-01 11:49:25 +03:00
The default value for `transform` flag is `noop` which means no transformation will be performed.
2018-02-06 15:17:13 +03:00
If a prefix is provided via the `trimprefix` flag, it will be trimmed from the start of each name (before
2017-04-10 21:37:23 +03:00
it is transformed). If a name doesn't have the prefix it will be passed unchanged.
2019-03-22 13:36:16 +03:00
If a prefix is provided via the `addprefix` flag, it will be added to the start of each name (after trimming and after transforming).
2019-03-22 12:54:07 +03:00
2021-10-17 15:32:13 +03:00
The boolean flag `values` will additionally create an alternative string values method `Values() []string` to fullfill the `EnumValues` interface of [ent ](https://entgo.io/docs/schema-fields/#enum-fields ).
2016-01-21 02:18:14 +03:00
## Inspiring projects
2019-01-17 15:07:52 +03:00
2019-04-05 08:53:44 +03:00
- [Álvaro López Espinosa ](https://github.com/alvaroloes/enumer )
2019-01-17 15:07:52 +03:00
- [Stringer ](https://godoc.org/golang.org/x/tools/cmd/stringer )
- [jsonenums ](https://github.com/campoy/jsonenums )