forked from mirror/enumer
Changed "BelongsTo" method by "IsA" and update Readme
This commit is contained in:
parent
d6adb342e2
commit
d213131f3e
36
README.md
36
README.md
|
@ -5,12 +5,16 @@ It started as a fork of [Rob Pike’s Stringer tool](https://godoc.org/golang.or
|
|||
## Generated functions and methods
|
||||
When Enumer is applied to a type, it will generate:
|
||||
|
||||
* A method `String()` that returns the string representation of the enum value. This makes the enum conform
|
||||
* The following basic methods/function:
|
||||
|
||||
* 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.
|
||||
* A function `<Type>String(s string)` to get the enum value from its string representation. This is useful
|
||||
* 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
|
||||
be almost meaningless or hard to trace or use by a human.
|
||||
* Function `<Type>Values()`: returns a slice with all the value of the enum
|
||||
* 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
|
||||
|
@ -34,14 +38,22 @@ const (
|
|||
Acetaminophen = Paracetamol
|
||||
)
|
||||
```
|
||||
executing `enumer -type=Pill -json` will generate a new file with four methods:
|
||||
executing `enumer -type=Pill -json` will generate a new file with four basic method and two extra for JSON:
|
||||
```go
|
||||
func (i Pill) String() string {
|
||||
//...
|
||||
func (i Pill) String() string {
|
||||
//...
|
||||
}
|
||||
|
||||
func PillString(s string) (Pill, error) {
|
||||
//...
|
||||
func PillString(s string) (Pill, error) {
|
||||
//...
|
||||
}
|
||||
|
||||
func PillValues() []Pill {
|
||||
//...
|
||||
}
|
||||
|
||||
func (i Pill) IsAPill() bool {
|
||||
//...
|
||||
}
|
||||
|
||||
func (i Pill) MarshalJSON() ([]byte, error) {
|
||||
|
@ -67,6 +79,16 @@ if err != nil {
|
|||
}
|
||||
// Now pill == Ibuprofen
|
||||
|
||||
// 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")
|
||||
}
|
||||
|
||||
// Marshal/unmarshal to/from json strings, either directly or automatically when
|
||||
// the enum is a field of a struct
|
||||
pillJSON := Aspirin.MarshalJSON()
|
||||
|
|
|
@ -24,8 +24,8 @@ func %[1]sValues() []%[1]s {
|
|||
|
||||
// Arguments to format are:
|
||||
// [1]: type name
|
||||
const stringBelongsMethodLoop = `// belongsTo%[1]s returns "true" if the value is listed in the enum definition. "false" otherwise
|
||||
func (i %[1]s) belongsTo%[1]s() bool {
|
||||
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 {
|
||||
for _, v := range _%[1]sValues {
|
||||
if i == v {
|
||||
return true
|
||||
|
@ -36,8 +36,8 @@ func (i %[1]s) belongsTo%[1]s() bool {
|
|||
`
|
||||
// Arguments to format are:
|
||||
// [1]: type name
|
||||
const stringBelongsMethodSet = `// belongsTo%[1]s returns "true" if the value is listed in the enum definition. "false" otherwise
|
||||
func (i %[1]s) belongsTo%[1]s() bool {
|
||||
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 {
|
||||
_, ok := _%[1]sMap[i]
|
||||
return ok
|
||||
}
|
||||
|
|
|
@ -106,8 +106,8 @@ func DayValues() []Day {
|
|||
return _DayValues
|
||||
}
|
||||
|
||||
// belongsToDay returns "true" if the value is listed in the enum definition. "false" otherwise
|
||||
func (i Day) belongsToDay() bool {
|
||||
// IsADay returns "true" if the value is listed in the enum definition. "false" otherwise
|
||||
func (i Day) IsADay() bool {
|
||||
for _, v := range _DayValues {
|
||||
if i == v {
|
||||
return true
|
||||
|
@ -164,8 +164,8 @@ func NumberValues() []Number {
|
|||
return _NumberValues
|
||||
}
|
||||
|
||||
// belongsToNumber returns "true" if the value is listed in the enum definition. "false" otherwise
|
||||
func (i Number) belongsToNumber() bool {
|
||||
// IsANumber returns "true" if the value is listed in the enum definition. "false" otherwise
|
||||
func (i Number) IsANumber() bool {
|
||||
for _, v := range _NumberValues {
|
||||
if i == v {
|
||||
return true
|
||||
|
@ -244,8 +244,8 @@ func GapValues() []Gap {
|
|||
return _GapValues
|
||||
}
|
||||
|
||||
// belongsToGap returns "true" if the value is listed in the enum definition. "false" otherwise
|
||||
func (i Gap) belongsToGap() bool {
|
||||
// IsAGap returns "true" if the value is listed in the enum definition. "false" otherwise
|
||||
func (i Gap) IsAGap() bool {
|
||||
for _, v := range _GapValues {
|
||||
if i == v {
|
||||
return true
|
||||
|
@ -303,8 +303,8 @@ func NumValues() []Num {
|
|||
return _NumValues
|
||||
}
|
||||
|
||||
// belongsToNum returns "true" if the value is listed in the enum definition. "false" otherwise
|
||||
func (i Num) belongsToNum() bool {
|
||||
// IsANum returns "true" if the value is listed in the enum definition. "false" otherwise
|
||||
func (i Num) IsANum() bool {
|
||||
for _, v := range _NumValues {
|
||||
if i == v {
|
||||
return true
|
||||
|
@ -375,8 +375,8 @@ func UnumValues() []Unum {
|
|||
return _UnumValues
|
||||
}
|
||||
|
||||
// belongsToUnum returns "true" if the value is listed in the enum definition. "false" otherwise
|
||||
func (i Unum) belongsToUnum() bool {
|
||||
// IsAUnum returns "true" if the value is listed in the enum definition. "false" otherwise
|
||||
func (i Unum) IsAUnum() bool {
|
||||
for _, v := range _UnumValues {
|
||||
if i == v {
|
||||
return true
|
||||
|
@ -465,8 +465,8 @@ func PrimeValues() []Prime {
|
|||
return _PrimeValues
|
||||
}
|
||||
|
||||
// belongsToPrime returns "true" if the value is listed in the enum definition. "false" otherwise
|
||||
func (i Prime) belongsToPrime() bool {
|
||||
// IsAPrime returns "true" if the value is listed in the enum definition. "false" otherwise
|
||||
func (i Prime) IsAPrime() bool {
|
||||
_, ok := _PrimeMap[i]
|
||||
return ok
|
||||
}
|
||||
|
@ -548,8 +548,8 @@ func PrimeValues() []Prime {
|
|||
return _PrimeValues
|
||||
}
|
||||
|
||||
// belongsToPrime returns "true" if the value is listed in the enum definition. "false" otherwise
|
||||
func (i Prime) belongsToPrime() bool {
|
||||
// IsAPrime returns "true" if the value is listed in the enum definition. "false" otherwise
|
||||
func (i Prime) IsAPrime() bool {
|
||||
_, ok := _PrimeMap[i]
|
||||
return ok
|
||||
}
|
||||
|
@ -649,8 +649,8 @@ func PrimeValues() []Prime {
|
|||
return _PrimeValues
|
||||
}
|
||||
|
||||
// belongsToPrime returns "true" if the value is listed in the enum definition. "false" otherwise
|
||||
func (i Prime) belongsToPrime() bool {
|
||||
// IsAPrime returns "true" if the value is listed in the enum definition. "false" otherwise
|
||||
func (i Prime) IsAPrime() bool {
|
||||
_, ok := _PrimeMap[i]
|
||||
return ok
|
||||
}
|
||||
|
@ -745,8 +745,8 @@ func PrimeValues() []Prime {
|
|||
return _PrimeValues
|
||||
}
|
||||
|
||||
// belongsToPrime returns "true" if the value is listed in the enum definition. "false" otherwise
|
||||
func (i Prime) belongsToPrime() bool {
|
||||
// IsAPrime returns "true" if the value is listed in the enum definition. "false" otherwise
|
||||
func (i Prime) IsAPrime() bool {
|
||||
_, ok := _PrimeMap[i]
|
||||
return ok
|
||||
}
|
||||
|
@ -846,8 +846,8 @@ func PrimeValues() []Prime {
|
|||
return _PrimeValues
|
||||
}
|
||||
|
||||
// belongsToPrime returns "true" if the value is listed in the enum definition. "false" otherwise
|
||||
func (i Prime) belongsToPrime() bool {
|
||||
// IsAPrime returns "true" if the value is listed in the enum definition. "false" otherwise
|
||||
func (i Prime) IsAPrime() bool {
|
||||
_, ok := _PrimeMap[i]
|
||||
return ok
|
||||
}
|
||||
|
@ -958,8 +958,8 @@ func PrimeValues() []Prime {
|
|||
return _PrimeValues
|
||||
}
|
||||
|
||||
// belongsToPrime returns "true" if the value is listed in the enum definition. "false" otherwise
|
||||
func (i Prime) belongsToPrime() bool {
|
||||
// IsAPrime returns "true" if the value is listed in the enum definition. "false" otherwise
|
||||
func (i Prime) IsAPrime() bool {
|
||||
_, ok := _PrimeMap[i]
|
||||
return ok
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue