diff --git a/README.md b/README.md index ad71301..2533dfe 100644 --- a/README.md +++ b/README.md @@ -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 be almost meaningless or hard to trace or use by a human. - Function `Values()`: returns a slice with all the values of the enum + - Function `Strings()`: returns a slice with all the Strings of the enum - Method `IsA()`: 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 @@ -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 - 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. -- 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. @@ -63,6 +64,10 @@ func PillValues() []Pill { //... } +func PillStrings() []string { + //... +} + func (i Pill) IsAPill() bool { //... } diff --git a/examples/gomods/README.md b/examples/gomods/README.md new file mode 100644 index 0000000..8d96aa0 --- /dev/null +++ b/examples/gomods/README.md @@ -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. diff --git a/examples/gomods/go.mod b/examples/gomods/go.mod new file mode 100644 index 0000000..ae7bd39 --- /dev/null +++ b/examples/gomods/go.mod @@ -0,0 +1,3 @@ +module github.com/dmarkham/gomods + +go 1.12 diff --git a/examples/gomods/go.sum b/examples/gomods/go.sum new file mode 100644 index 0000000..e69de29 diff --git a/examples/gomods/main.go b/examples/gomods/main.go new file mode 100644 index 0000000..7d38027 --- /dev/null +++ b/examples/gomods/main.go @@ -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) +}