forked from mirror/enumer
Optimized belongsToEnum method and updated tests
This commit is contained in:
parent
1dea5e9823
commit
d6adb342e2
16
enumer.go
16
enumer.go
|
@ -24,7 +24,7 @@ func %[1]sValues() []%[1]s {
|
||||||
|
|
||||||
// Arguments to format are:
|
// Arguments to format are:
|
||||||
// [1]: type name
|
// [1]: type name
|
||||||
const stringBelongsMethod = `// belongsTo%[1]s returns "true" if the value is listed in the enum definition. "false" otherwise
|
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 {
|
func (i %[1]s) belongsTo%[1]s() bool {
|
||||||
for _, v := range _%[1]sValues {
|
for _, v := range _%[1]sValues {
|
||||||
if i == v {
|
if i == v {
|
||||||
|
@ -34,6 +34,14 @@ func (i %[1]s) belongsTo%[1]s() bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
// 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 {
|
||||||
|
_, ok := _%[1]sMap[i]
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
`
|
||||||
|
|
||||||
func (g *Generator) buildBasicExtras(runs [][]Value, typeName string, runsThreshold int) {
|
func (g *Generator) buildBasicExtras(runs [][]Value, typeName string, runsThreshold int) {
|
||||||
// At this moment, either "g.declareIndexAndNameVars()" or "g.declareNameVars()" has been called
|
// At this moment, either "g.declareIndexAndNameVars()" or "g.declareNameVars()" has been called
|
||||||
|
@ -70,7 +78,11 @@ func (g *Generator) buildBasicExtras(runs [][]Value, typeName string, runsThresh
|
||||||
// Print the basic extra methods
|
// Print the basic extra methods
|
||||||
g.Printf(stringNameToValueMethod, typeName)
|
g.Printf(stringNameToValueMethod, typeName)
|
||||||
g.Printf(stringValuesMethod, typeName)
|
g.Printf(stringValuesMethod, typeName)
|
||||||
g.Printf(stringBelongsMethod, typeName)
|
if len(runs) < runsThreshold {
|
||||||
|
g.Printf(stringBelongsMethodLoop, typeName)
|
||||||
|
} else { // There is a map of values, the code is simpler then
|
||||||
|
g.Printf(stringBelongsMethodSet, typeName)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Arguments to format are:
|
// Arguments to format are:
|
||||||
|
|
|
@ -433,6 +433,8 @@ func (i Prime) String() string {
|
||||||
return fmt.Sprintf("Prime(%d)", i)
|
return fmt.Sprintf("Prime(%d)", i)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var _PrimeValues = []Prime{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 41, 43}
|
||||||
|
|
||||||
var _PrimeNameToValueMap = map[string]Prime{
|
var _PrimeNameToValueMap = map[string]Prime{
|
||||||
_PrimeName[0:2]: 2,
|
_PrimeName[0:2]: 2,
|
||||||
_PrimeName[2:4]: 3,
|
_PrimeName[2:4]: 3,
|
||||||
|
@ -457,6 +459,17 @@ func PrimeString(s string) (Prime, error) {
|
||||||
}
|
}
|
||||||
return 0, fmt.Errorf("%s does not belong to Prime values", s)
|
return 0, fmt.Errorf("%s does not belong to Prime values", s)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PrimeValues returns all values of the enum
|
||||||
|
func PrimeValues() []Prime {
|
||||||
|
return _PrimeValues
|
||||||
|
}
|
||||||
|
|
||||||
|
// belongsToPrime returns "true" if the value is listed in the enum definition. "false" otherwise
|
||||||
|
func (i Prime) belongsToPrime() bool {
|
||||||
|
_, ok := _PrimeMap[i]
|
||||||
|
return ok
|
||||||
|
}
|
||||||
`
|
`
|
||||||
const prime_json_in = `type Prime int
|
const prime_json_in = `type Prime int
|
||||||
const (
|
const (
|
||||||
|
@ -503,6 +516,8 @@ func (i Prime) String() string {
|
||||||
return fmt.Sprintf("Prime(%d)", i)
|
return fmt.Sprintf("Prime(%d)", i)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var _PrimeValues = []Prime{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 41, 43}
|
||||||
|
|
||||||
var _PrimeNameToValueMap = map[string]Prime{
|
var _PrimeNameToValueMap = map[string]Prime{
|
||||||
_PrimeName[0:2]: 2,
|
_PrimeName[0:2]: 2,
|
||||||
_PrimeName[2:4]: 3,
|
_PrimeName[2:4]: 3,
|
||||||
|
@ -528,6 +543,17 @@ func PrimeString(s string) (Prime, error) {
|
||||||
return 0, fmt.Errorf("%s does not belong to Prime values", s)
|
return 0, fmt.Errorf("%s does not belong to Prime values", s)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PrimeValues returns all values of the enum
|
||||||
|
func PrimeValues() []Prime {
|
||||||
|
return _PrimeValues
|
||||||
|
}
|
||||||
|
|
||||||
|
// belongsToPrime returns "true" if the value is listed in the enum definition. "false" otherwise
|
||||||
|
func (i Prime) belongsToPrime() bool {
|
||||||
|
_, ok := _PrimeMap[i]
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
// MarshalJSON implements the json.Marshaler interface for Prime
|
// MarshalJSON implements the json.Marshaler interface for Prime
|
||||||
func (i Prime) MarshalJSON() ([]byte, error) {
|
func (i Prime) MarshalJSON() ([]byte, error) {
|
||||||
return json.Marshal(i.String())
|
return json.Marshal(i.String())
|
||||||
|
@ -591,6 +617,8 @@ func (i Prime) String() string {
|
||||||
return fmt.Sprintf("Prime(%d)", i)
|
return fmt.Sprintf("Prime(%d)", i)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var _PrimeValues = []Prime{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 41, 43}
|
||||||
|
|
||||||
var _PrimeNameToValueMap = map[string]Prime{
|
var _PrimeNameToValueMap = map[string]Prime{
|
||||||
_PrimeName[0:2]: 2,
|
_PrimeName[0:2]: 2,
|
||||||
_PrimeName[2:4]: 3,
|
_PrimeName[2:4]: 3,
|
||||||
|
@ -616,6 +644,17 @@ func PrimeString(s string) (Prime, error) {
|
||||||
return 0, fmt.Errorf("%s does not belong to Prime values", s)
|
return 0, fmt.Errorf("%s does not belong to Prime values", s)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PrimeValues returns all values of the enum
|
||||||
|
func PrimeValues() []Prime {
|
||||||
|
return _PrimeValues
|
||||||
|
}
|
||||||
|
|
||||||
|
// belongsToPrime returns "true" if the value is listed in the enum definition. "false" otherwise
|
||||||
|
func (i Prime) belongsToPrime() bool {
|
||||||
|
_, ok := _PrimeMap[i]
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
// MarshalText implements the encoding.TextMarshaler interface for Prime
|
// MarshalText implements the encoding.TextMarshaler interface for Prime
|
||||||
func (i Prime) MarshalText() ([]byte, error) {
|
func (i Prime) MarshalText() ([]byte, error) {
|
||||||
return []byte(i.String()), nil
|
return []byte(i.String()), nil
|
||||||
|
@ -674,6 +713,8 @@ func (i Prime) String() string {
|
||||||
return fmt.Sprintf("Prime(%d)", i)
|
return fmt.Sprintf("Prime(%d)", i)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var _PrimeValues = []Prime{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 41, 43}
|
||||||
|
|
||||||
var _PrimeNameToValueMap = map[string]Prime{
|
var _PrimeNameToValueMap = map[string]Prime{
|
||||||
_PrimeName[0:2]: 2,
|
_PrimeName[0:2]: 2,
|
||||||
_PrimeName[2:4]: 3,
|
_PrimeName[2:4]: 3,
|
||||||
|
@ -699,6 +740,17 @@ func PrimeString(s string) (Prime, error) {
|
||||||
return 0, fmt.Errorf("%s does not belong to Prime values", s)
|
return 0, fmt.Errorf("%s does not belong to Prime values", s)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PrimeValues returns all values of the enum
|
||||||
|
func PrimeValues() []Prime {
|
||||||
|
return _PrimeValues
|
||||||
|
}
|
||||||
|
|
||||||
|
// belongsToPrime returns "true" if the value is listed in the enum definition. "false" otherwise
|
||||||
|
func (i Prime) belongsToPrime() bool {
|
||||||
|
_, ok := _PrimeMap[i]
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
// MarshalYAML implements a YAML Marshaler for Prime
|
// MarshalYAML implements a YAML Marshaler for Prime
|
||||||
func (i Prime) MarshalYAML() (interface{}, error) {
|
func (i Prime) MarshalYAML() (interface{}, error) {
|
||||||
return i.String(), nil
|
return i.String(), nil
|
||||||
|
@ -762,6 +814,8 @@ func (i Prime) String() string {
|
||||||
return fmt.Sprintf("Prime(%d)", i)
|
return fmt.Sprintf("Prime(%d)", i)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var _PrimeValues = []Prime{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 41, 43}
|
||||||
|
|
||||||
var _PrimeNameToValueMap = map[string]Prime{
|
var _PrimeNameToValueMap = map[string]Prime{
|
||||||
_PrimeName[0:2]: 2,
|
_PrimeName[0:2]: 2,
|
||||||
_PrimeName[2:4]: 3,
|
_PrimeName[2:4]: 3,
|
||||||
|
@ -787,6 +841,17 @@ func PrimeString(s string) (Prime, error) {
|
||||||
return 0, fmt.Errorf("%s does not belong to Prime values", s)
|
return 0, fmt.Errorf("%s does not belong to Prime values", s)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PrimeValues returns all values of the enum
|
||||||
|
func PrimeValues() []Prime {
|
||||||
|
return _PrimeValues
|
||||||
|
}
|
||||||
|
|
||||||
|
// belongsToPrime returns "true" if the value is listed in the enum definition. "false" otherwise
|
||||||
|
func (i Prime) belongsToPrime() bool {
|
||||||
|
_, ok := _PrimeMap[i]
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
func (i Prime) Value() (driver.Value, error) {
|
func (i Prime) Value() (driver.Value, error) {
|
||||||
return i.String(), nil
|
return i.String(), nil
|
||||||
}
|
}
|
||||||
|
@ -861,6 +926,8 @@ func (i Prime) String() string {
|
||||||
return fmt.Sprintf("Prime(%d)", i)
|
return fmt.Sprintf("Prime(%d)", i)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var _PrimeValues = []Prime{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 41, 43}
|
||||||
|
|
||||||
var _PrimeNameToValueMap = map[string]Prime{
|
var _PrimeNameToValueMap = map[string]Prime{
|
||||||
_PrimeName[0:2]: 2,
|
_PrimeName[0:2]: 2,
|
||||||
_PrimeName[2:4]: 3,
|
_PrimeName[2:4]: 3,
|
||||||
|
@ -886,6 +953,17 @@ func PrimeString(s string) (Prime, error) {
|
||||||
return 0, fmt.Errorf("%s does not belong to Prime values", s)
|
return 0, fmt.Errorf("%s does not belong to Prime values", s)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PrimeValues returns all values of the enum
|
||||||
|
func PrimeValues() []Prime {
|
||||||
|
return _PrimeValues
|
||||||
|
}
|
||||||
|
|
||||||
|
// belongsToPrime returns "true" if the value is listed in the enum definition. "false" otherwise
|
||||||
|
func (i Prime) belongsToPrime() bool {
|
||||||
|
_, ok := _PrimeMap[i]
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
// MarshalJSON implements the json.Marshaler interface for Prime
|
// MarshalJSON implements the json.Marshaler interface for Prime
|
||||||
func (i Prime) MarshalJSON() ([]byte, error) {
|
func (i Prime) MarshalJSON() ([]byte, error) {
|
||||||
return json.Marshal(i.String())
|
return json.Marshal(i.String())
|
||||||
|
|
Loading…
Reference in New Issue