Add method for getting a slice of strings

This commit is contained in:
Luca Osti 2019-03-22 12:56:38 +01:00
parent 188f5ebedc
commit 63cd80f057
2 changed files with 303 additions and 8 deletions

View File

@ -22,6 +22,16 @@ func %[1]sValues() []%[1]s {
} }
` `
// Arguments to format are:
// [1]: type name
const stringsMethod = `// %[1]sStrings returns a slice of all String values of the enum
func %[1]sStrings() []string {
strs := make([]string, len(_%[1]sNames))
copy(strs, _%[1]sNames)
return strs
}
`
// Arguments to format are: // Arguments to format are:
// [1]: type name // [1]: type name
const stringBelongsMethodLoop = `// IsA%[1]s returns "true" if the value is listed in the enum definition. "false" otherwise const stringBelongsMethodLoop = `// IsA%[1]s returns "true" if the value is listed in the enum definition. "false" otherwise
@ -57,8 +67,26 @@ func (g *Generator) buildBasicExtras(runs [][]Value, typeName string, runsThresh
g.Printf("}\n\n") g.Printf("}\n\n")
// Print the map between name and value // Print the map between name and value
g.Printf("\nvar _%sNameToValueMap = map[string]%s{\n", typeName, typeName) g.printValueMap(runs, typeName, runsThreshold)
// Print the slice of names
g.printNamesSlice(runs, typeName, runsThreshold)
// Print the basic extra methods
g.Printf(stringNameToValueMethod, typeName)
g.Printf(stringValuesMethod, typeName)
g.Printf(stringsMethod, 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)
}
}
func (g *Generator) printValueMap(runs [][]Value, typeName string, runsThreshold int) {
thereAreRuns := len(runs) > 1 && len(runs) <= runsThreshold thereAreRuns := len(runs) > 1 && len(runs) <= runsThreshold
g.Printf("\nvar _%sNameToValueMap = map[string]%s{\n", typeName, typeName)
var n int var n int
var runID string var runID string
for i, values := range runs { for i, values := range runs {
@ -75,15 +103,27 @@ func (g *Generator) buildBasicExtras(runs [][]Value, typeName string, runsThresh
} }
} }
g.Printf("}\n\n") g.Printf("}\n\n")
}
func (g *Generator) printNamesSlice(runs [][]Value, typeName string, runsThreshold int) {
thereAreRuns := len(runs) > 1 && len(runs) <= runsThreshold
g.Printf("\nvar _%sNames = []string{\n", typeName)
// Print the basic extra methods var n int
g.Printf(stringNameToValueMethod, typeName) var runID string
g.Printf(stringValuesMethod, typeName) for i, values := range runs {
if len(runs) < runsThreshold { if thereAreRuns {
g.Printf(stringBelongsMethodLoop, typeName) runID = "_" + fmt.Sprintf("%d", i)
} else { // There is a map of values, the code is simpler then n = 0
g.Printf(stringBelongsMethodSet, typeName) } else {
runID = ""
} }
for _, value := range values {
g.Printf("\t_%sName%s[%d:%d],\n", typeName, runID, n, n+len(value.name))
n += len(value.name)
}
}
g.Printf("}\n\n")
} }
// Arguments to format are: // Arguments to format are:

View File

@ -100,6 +100,16 @@ var _DayNameToValueMap = map[string]Day{
_DayName[44:50]: 6, _DayName[44:50]: 6,
} }
var _DayNames = []string{
_DayName[0:6],
_DayName[6:13],
_DayName[13:22],
_DayName[22:30],
_DayName[30:36],
_DayName[36:44],
_DayName[44:50],
}
// DayString retrieves an enum value from the enum constants string name. // DayString retrieves an enum value from the enum constants string name.
// Throws an error if the param is not part of the enum. // Throws an error if the param is not part of the enum.
func DayString(s string) (Day, error) { func DayString(s string) (Day, error) {
@ -114,6 +124,13 @@ func DayValues() []Day {
return _DayValues return _DayValues
} }
// DayStrings returns a slice of all String values of the enum
func DayStrings() []string {
strs := make([]string, len(_DayNames))
copy(strs, _DayNames)
return strs
}
// IsADay returns "true" if the value is listed in the enum definition. "false" otherwise // IsADay returns "true" if the value is listed in the enum definition. "false" otherwise
func (i Day) IsADay() bool { func (i Day) IsADay() bool {
for _, v := range _DayValues { for _, v := range _DayValues {
@ -149,6 +166,16 @@ var _DayNameToValueMap = map[string]Day{
_DayName[62:71]: 6, _DayName[62:71]: 6,
} }
var _DayNames = []string{
_DayName[0:9],
_DayName[9:19],
_DayName[19:31],
_DayName[31:42],
_DayName[42:51],
_DayName[51:62],
_DayName[62:71],
}
// DayString retrieves an enum value from the enum constants string name. // DayString retrieves an enum value from the enum constants string name.
// Throws an error if the param is not part of the enum. // Throws an error if the param is not part of the enum.
func DayString(s string) (Day, error) { func DayString(s string) (Day, error) {
@ -163,6 +190,13 @@ func DayValues() []Day {
return _DayValues return _DayValues
} }
// DayStrings returns a slice of all String values of the enum
func DayStrings() []string {
strs := make([]string, len(_DayNames))
copy(strs, _DayNames)
return strs
}
// IsADay returns "true" if the value is listed in the enum definition. "false" otherwise // IsADay returns "true" if the value is listed in the enum definition. "false" otherwise
func (i Day) IsADay() bool { func (i Day) IsADay() bool {
for _, v := range _DayValues { for _, v := range _DayValues {
@ -198,6 +232,16 @@ var _DayNameToValueMap = map[string]Day{
_DayName[74:85]: 6, _DayName[74:85]: 6,
} }
var _DayNames = []string{
_DayName[0:11],
_DayName[11:23],
_DayName[23:37],
_DayName[37:50],
_DayName[50:61],
_DayName[61:74],
_DayName[74:85],
}
// DayString retrieves an enum value from the enum constants string name. // DayString retrieves an enum value from the enum constants string name.
// Throws an error if the param is not part of the enum. // Throws an error if the param is not part of the enum.
func DayString(s string) (Day, error) { func DayString(s string) (Day, error) {
@ -212,6 +256,13 @@ func DayValues() []Day {
return _DayValues return _DayValues
} }
// DayStrings returns a slice of all String values of the enum
func DayStrings() []string {
strs := make([]string, len(_DayNames))
copy(strs, _DayNames)
return strs
}
// IsADay returns "true" if the value is listed in the enum definition. "false" otherwise // IsADay returns "true" if the value is listed in the enum definition. "false" otherwise
func (i Day) IsADay() bool { func (i Day) IsADay() bool {
for _, v := range _DayValues { for _, v := range _DayValues {
@ -256,6 +307,12 @@ var _NumberNameToValueMap = map[string]Number{
_NumberName[6:11]: 3, _NumberName[6:11]: 3,
} }
var _NumberNames = []string{
_NumberName[0:3],
_NumberName[3:6],
_NumberName[6:11],
}
// NumberString retrieves an enum value from the enum constants string name. // NumberString retrieves an enum value from the enum constants string name.
// Throws an error if the param is not part of the enum. // Throws an error if the param is not part of the enum.
func NumberString(s string) (Number, error) { func NumberString(s string) (Number, error) {
@ -270,6 +327,13 @@ func NumberValues() []Number {
return _NumberValues return _NumberValues
} }
// NumberStrings returns a slice of all String values of the enum
func NumberStrings() []string {
strs := make([]string, len(_NumberNames))
copy(strs, _NumberNames)
return strs
}
// IsANumber returns "true" if the value is listed in the enum definition. "false" otherwise // IsANumber returns "true" if the value is listed in the enum definition. "false" otherwise
func (i Number) IsANumber() bool { func (i Number) IsANumber() bool {
for _, v := range _NumberValues { for _, v := range _NumberValues {
@ -336,6 +400,17 @@ var _GapNameToValueMap = map[string]Gap{
_GapName_2[0:6]: 11, _GapName_2[0:6]: 11,
} }
var _GapNames = []string{
_GapName_0[0:3],
_GapName_0[3:8],
_GapName_1[0:4],
_GapName_1[4:7],
_GapName_1[7:12],
_GapName_1[12:17],
_GapName_1[17:21],
_GapName_2[0:6],
}
// GapString retrieves an enum value from the enum constants string name. // GapString retrieves an enum value from the enum constants string name.
// Throws an error if the param is not part of the enum. // Throws an error if the param is not part of the enum.
func GapString(s string) (Gap, error) { func GapString(s string) (Gap, error) {
@ -350,6 +425,13 @@ func GapValues() []Gap {
return _GapValues return _GapValues
} }
// GapStrings returns a slice of all String values of the enum
func GapStrings() []string {
strs := make([]string, len(_GapNames))
copy(strs, _GapNames)
return strs
}
// IsAGap returns "true" if the value is listed in the enum definition. "false" otherwise // IsAGap returns "true" if the value is listed in the enum definition. "false" otherwise
func (i Gap) IsAGap() bool { func (i Gap) IsAGap() bool {
for _, v := range _GapValues { for _, v := range _GapValues {
@ -395,6 +477,14 @@ var _NumNameToValueMap = map[string]Num{
_NumName[10:12]: 2, _NumName[10:12]: 2,
} }
var _NumNames = []string{
_NumName[0:3],
_NumName[3:6],
_NumName[6:8],
_NumName[8:10],
_NumName[10:12],
}
// NumString retrieves an enum value from the enum constants string name. // NumString retrieves an enum value from the enum constants string name.
// Throws an error if the param is not part of the enum. // Throws an error if the param is not part of the enum.
func NumString(s string) (Num, error) { func NumString(s string) (Num, error) {
@ -409,6 +499,13 @@ func NumValues() []Num {
return _NumValues return _NumValues
} }
// NumStrings returns a slice of all String values of the enum
func NumStrings() []string {
strs := make([]string, len(_NumNames))
copy(strs, _NumNames)
return strs
}
// IsANum returns "true" if the value is listed in the enum definition. "false" otherwise // IsANum returns "true" if the value is listed in the enum definition. "false" otherwise
func (i Num) IsANum() bool { func (i Num) IsANum() bool {
for _, v := range _NumValues { for _, v := range _NumValues {
@ -467,6 +564,14 @@ var _UnumNameToValueMap = map[string]Unum{
_UnumName_1[3:6]: 254, _UnumName_1[3:6]: 254,
} }
var _UnumNames = []string{
_UnumName_0[0:2],
_UnumName_0[2:4],
_UnumName_0[4:6],
_UnumName_1[0:3],
_UnumName_1[3:6],
}
// UnumString retrieves an enum value from the enum constants string name. // UnumString retrieves an enum value from the enum constants string name.
// Throws an error if the param is not part of the enum. // Throws an error if the param is not part of the enum.
func UnumString(s string) (Unum, error) { func UnumString(s string) (Unum, error) {
@ -481,6 +586,13 @@ func UnumValues() []Unum {
return _UnumValues return _UnumValues
} }
// UnumStrings returns a slice of all String values of the enum
func UnumStrings() []string {
strs := make([]string, len(_UnumNames))
copy(strs, _UnumNames)
return strs
}
// IsAUnum returns "true" if the value is listed in the enum definition. "false" otherwise // IsAUnum returns "true" if the value is listed in the enum definition. "false" otherwise
func (i Unum) IsAUnum() bool { func (i Unum) IsAUnum() bool {
for _, v := range _UnumValues { for _, v := range _UnumValues {
@ -557,6 +669,22 @@ var _PrimeNameToValueMap = map[string]Prime{
_PrimeName[32:35]: 43, _PrimeName[32:35]: 43,
} }
var _PrimeNames = []string{
_PrimeName[0:2],
_PrimeName[2:4],
_PrimeName[4:6],
_PrimeName[6:8],
_PrimeName[8:11],
_PrimeName[11:14],
_PrimeName[14:17],
_PrimeName[17:20],
_PrimeName[20:23],
_PrimeName[23:26],
_PrimeName[26:29],
_PrimeName[29:32],
_PrimeName[32:35],
}
// PrimeString retrieves an enum value from the enum constants string name. // PrimeString retrieves an enum value from the enum constants string name.
// Throws an error if the param is not part of the enum. // Throws an error if the param is not part of the enum.
func PrimeString(s string) (Prime, error) { func PrimeString(s string) (Prime, error) {
@ -571,6 +699,13 @@ func PrimeValues() []Prime {
return _PrimeValues return _PrimeValues
} }
// PrimeStrings returns a slice of all String values of the enum
func PrimeStrings() []string {
strs := make([]string, len(_PrimeNames))
copy(strs, _PrimeNames)
return strs
}
// IsAPrime returns "true" if the value is listed in the enum definition. "false" otherwise // IsAPrime returns "true" if the value is listed in the enum definition. "false" otherwise
func (i Prime) IsAPrime() bool { func (i Prime) IsAPrime() bool {
_, ok := _PrimeMap[i] _, ok := _PrimeMap[i]
@ -640,6 +775,22 @@ var _PrimeNameToValueMap = map[string]Prime{
_PrimeName[32:35]: 43, _PrimeName[32:35]: 43,
} }
var _PrimeNames = []string{
_PrimeName[0:2],
_PrimeName[2:4],
_PrimeName[4:6],
_PrimeName[6:8],
_PrimeName[8:11],
_PrimeName[11:14],
_PrimeName[14:17],
_PrimeName[17:20],
_PrimeName[20:23],
_PrimeName[23:26],
_PrimeName[26:29],
_PrimeName[29:32],
_PrimeName[32:35],
}
// PrimeString retrieves an enum value from the enum constants string name. // PrimeString retrieves an enum value from the enum constants string name.
// Throws an error if the param is not part of the enum. // Throws an error if the param is not part of the enum.
func PrimeString(s string) (Prime, error) { func PrimeString(s string) (Prime, error) {
@ -654,6 +805,13 @@ func PrimeValues() []Prime {
return _PrimeValues return _PrimeValues
} }
// PrimeStrings returns a slice of all String values of the enum
func PrimeStrings() []string {
strs := make([]string, len(_PrimeNames))
copy(strs, _PrimeNames)
return strs
}
// IsAPrime returns "true" if the value is listed in the enum definition. "false" otherwise // IsAPrime returns "true" if the value is listed in the enum definition. "false" otherwise
func (i Prime) IsAPrime() bool { func (i Prime) IsAPrime() bool {
_, ok := _PrimeMap[i] _, ok := _PrimeMap[i]
@ -741,6 +899,22 @@ var _PrimeNameToValueMap = map[string]Prime{
_PrimeName[32:35]: 43, _PrimeName[32:35]: 43,
} }
var _PrimeNames = []string{
_PrimeName[0:2],
_PrimeName[2:4],
_PrimeName[4:6],
_PrimeName[6:8],
_PrimeName[8:11],
_PrimeName[11:14],
_PrimeName[14:17],
_PrimeName[17:20],
_PrimeName[20:23],
_PrimeName[23:26],
_PrimeName[26:29],
_PrimeName[29:32],
_PrimeName[32:35],
}
// PrimeString retrieves an enum value from the enum constants string name. // PrimeString retrieves an enum value from the enum constants string name.
// Throws an error if the param is not part of the enum. // Throws an error if the param is not part of the enum.
func PrimeString(s string) (Prime, error) { func PrimeString(s string) (Prime, error) {
@ -755,6 +929,13 @@ func PrimeValues() []Prime {
return _PrimeValues return _PrimeValues
} }
// PrimeStrings returns a slice of all String values of the enum
func PrimeStrings() []string {
strs := make([]string, len(_PrimeNames))
copy(strs, _PrimeNames)
return strs
}
// IsAPrime returns "true" if the value is listed in the enum definition. "false" otherwise // IsAPrime returns "true" if the value is listed in the enum definition. "false" otherwise
func (i Prime) IsAPrime() bool { func (i Prime) IsAPrime() bool {
_, ok := _PrimeMap[i] _, ok := _PrimeMap[i]
@ -837,6 +1018,22 @@ var _PrimeNameToValueMap = map[string]Prime{
_PrimeName[32:35]: 43, _PrimeName[32:35]: 43,
} }
var _PrimeNames = []string{
_PrimeName[0:2],
_PrimeName[2:4],
_PrimeName[4:6],
_PrimeName[6:8],
_PrimeName[8:11],
_PrimeName[11:14],
_PrimeName[14:17],
_PrimeName[17:20],
_PrimeName[20:23],
_PrimeName[23:26],
_PrimeName[26:29],
_PrimeName[29:32],
_PrimeName[32:35],
}
// PrimeString retrieves an enum value from the enum constants string name. // PrimeString retrieves an enum value from the enum constants string name.
// Throws an error if the param is not part of the enum. // Throws an error if the param is not part of the enum.
func PrimeString(s string) (Prime, error) { func PrimeString(s string) (Prime, error) {
@ -851,6 +1048,13 @@ func PrimeValues() []Prime {
return _PrimeValues return _PrimeValues
} }
// PrimeStrings returns a slice of all String values of the enum
func PrimeStrings() []string {
strs := make([]string, len(_PrimeNames))
copy(strs, _PrimeNames)
return strs
}
// IsAPrime returns "true" if the value is listed in the enum definition. "false" otherwise // IsAPrime returns "true" if the value is listed in the enum definition. "false" otherwise
func (i Prime) IsAPrime() bool { func (i Prime) IsAPrime() bool {
_, ok := _PrimeMap[i] _, ok := _PrimeMap[i]
@ -938,6 +1142,22 @@ var _PrimeNameToValueMap = map[string]Prime{
_PrimeName[32:35]: 43, _PrimeName[32:35]: 43,
} }
var _PrimeNames = []string{
_PrimeName[0:2],
_PrimeName[2:4],
_PrimeName[4:6],
_PrimeName[6:8],
_PrimeName[8:11],
_PrimeName[11:14],
_PrimeName[14:17],
_PrimeName[17:20],
_PrimeName[20:23],
_PrimeName[23:26],
_PrimeName[26:29],
_PrimeName[29:32],
_PrimeName[32:35],
}
// PrimeString retrieves an enum value from the enum constants string name. // PrimeString retrieves an enum value from the enum constants string name.
// Throws an error if the param is not part of the enum. // Throws an error if the param is not part of the enum.
func PrimeString(s string) (Prime, error) { func PrimeString(s string) (Prime, error) {
@ -952,6 +1172,13 @@ func PrimeValues() []Prime {
return _PrimeValues return _PrimeValues
} }
// PrimeStrings returns a slice of all String values of the enum
func PrimeStrings() []string {
strs := make([]string, len(_PrimeNames))
copy(strs, _PrimeNames)
return strs
}
// IsAPrime returns "true" if the value is listed in the enum definition. "false" otherwise // IsAPrime returns "true" if the value is listed in the enum definition. "false" otherwise
func (i Prime) IsAPrime() bool { func (i Prime) IsAPrime() bool {
_, ok := _PrimeMap[i] _, ok := _PrimeMap[i]
@ -1050,6 +1277,22 @@ var _PrimeNameToValueMap = map[string]Prime{
_PrimeName[32:35]: 43, _PrimeName[32:35]: 43,
} }
var _PrimeNames = []string{
_PrimeName[0:2],
_PrimeName[2:4],
_PrimeName[4:6],
_PrimeName[6:8],
_PrimeName[8:11],
_PrimeName[11:14],
_PrimeName[14:17],
_PrimeName[17:20],
_PrimeName[20:23],
_PrimeName[23:26],
_PrimeName[26:29],
_PrimeName[29:32],
_PrimeName[32:35],
}
// PrimeString retrieves an enum value from the enum constants string name. // PrimeString retrieves an enum value from the enum constants string name.
// Throws an error if the param is not part of the enum. // Throws an error if the param is not part of the enum.
func PrimeString(s string) (Prime, error) { func PrimeString(s string) (Prime, error) {
@ -1064,6 +1307,13 @@ func PrimeValues() []Prime {
return _PrimeValues return _PrimeValues
} }
// PrimeStrings returns a slice of all String values of the enum
func PrimeStrings() []string {
strs := make([]string, len(_PrimeNames))
copy(strs, _PrimeNames)
return strs
}
// IsAPrime returns "true" if the value is listed in the enum definition. "false" otherwise // IsAPrime returns "true" if the value is listed in the enum definition. "false" otherwise
func (i Prime) IsAPrime() bool { func (i Prime) IsAPrime() bool {
_, ok := _PrimeMap[i] _, ok := _PrimeMap[i]
@ -1171,6 +1421,11 @@ func runGoldenTest(t *testing.T, test Golden, generateJSON, generateYAML, genera
g.generate(tokens[1], generateJSON, generateYAML, generateSQL, generateText, "noop", trimPrefix, prefix) g.generate(tokens[1], generateJSON, generateYAML, generateSQL, generateText, "noop", trimPrefix, prefix)
got := string(g.format()) got := string(g.format())
if got != test.output { if got != test.output {
// dmp := diffmatchpatch.New()
// diffs := dmp.DiffMain(got, test.output, false)
// t.Errorf(dmp.DiffPrettyText(diffs))
t.Errorf("%s: got\n====\n%s====\nexpected\n====%s", test.name, got, test.output) t.Errorf("%s: got\n====\n%s====\nexpected\n====%s", test.name, got, test.output)
} }
} }