forked from mirror/enumer
Add a example for go mods use
This commit is contained in:
parent
a6b59c2799
commit
24de023e5c
|
@ -20,6 +20,7 @@ When Enumer is applied to a type, it will generate:
|
||||||
from a REST API request... In short, from those places where using the real enum value (an integer) would
|
from a REST API request... In short, from those places where using the real enum value (an integer) would
|
||||||
be almost meaningless or hard to trace or use by a human.
|
be almost meaningless or hard to trace or use by a human.
|
||||||
- Function `<Type>Values()`: returns a slice with all the values of the enum
|
- Function `<Type>Values()`: returns a slice with all the values of the enum
|
||||||
|
- Function `<Type>Strings()`: returns a slice with all the Strings of the enum
|
||||||
- Method `IsA<Type>()`: returns true only if the current value is among the values of the enum. Useful for validations.
|
- 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
|
- When the flag `json` is provided, two additional methods will be generated, `MarshalJSON()` and `UnmarshalJSON()`. These make
|
||||||
|
@ -30,7 +31,7 @@ When Enumer is applied to a type, it will generate:
|
||||||
convert the map keys to json (strings). If not, the numeric values will be used instead
|
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
|
- 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.
|
the enum conform to the `gopkg.in/yaml.v2.Marshaler` and `gopkg.in/yaml.v2.Unmarshaler` interfaces.
|
||||||
- When the flag `sql` is provided, the methods for implementing the Scanner and Valuer interfaces will be also generated.
|
- When the flag `sql` is provided, the methods for implementing the `Scanner` and `Valuer` interfaces.
|
||||||
Useful when storing the enum in a database.
|
Useful when storing the enum in a database.
|
||||||
|
|
||||||
|
|
||||||
|
@ -63,6 +64,10 @@ func PillValues() []Pill {
|
||||||
//...
|
//...
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func PillStrings() []string {
|
||||||
|
//...
|
||||||
|
}
|
||||||
|
|
||||||
func (i Pill) IsAPill() bool {
|
func (i Pill) IsAPill() bool {
|
||||||
//...
|
//...
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
# Go Modules Sample
|
||||||
|
|
||||||
|
## Steps
|
||||||
|
|
||||||
|
1. Go get enumer `go get -u github.com/dmarkham/enumer`
|
||||||
|
2. `go generate` This should create `pill_enumer.go`
|
||||||
|
3. `go run *.go` to see it in action
|
||||||
|
4. `go mod tidy` to remove the deps for `enumer` once your happy.
|
|
@ -0,0 +1,3 @@
|
||||||
|
module github.com/dmarkham/gomods
|
||||||
|
|
||||||
|
go 1.12
|
|
@ -0,0 +1,20 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
//go:generate go run github.com/dmarkham/enumer -type=Pill -json
|
||||||
|
type Pill int
|
||||||
|
|
||||||
|
const (
|
||||||
|
Placebo Pill = iota
|
||||||
|
Aspirin
|
||||||
|
Ibuprofen
|
||||||
|
Paracetamol
|
||||||
|
Acetaminophen = Paracetamol
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(PillStrings())
|
||||||
|
fmt.Println(Placebo.IsAPill())
|
||||||
|
fmt.Println(Placebo)
|
||||||
|
}
|
Loading…
Reference in New Issue