From aa50e29ee8e385e47d0bbd09051296deee18ee3c Mon Sep 17 00:00:00 2001 From: Michael Wolber Date: Wed, 3 Jan 2018 15:35:09 +0100 Subject: [PATCH 1/3] Added comment to generated ...String function. Removed usage of _ in identifiers. --- enumer.go | 10 +- golden_test.go | 440 ++++++++++++++++++++++++------------------------- stringer.go | 29 ++-- 3 files changed, 243 insertions(+), 236 deletions(-) diff --git a/enumer.go b/enumer.go index 834fc30..a43a469 100644 --- a/enumer.go +++ b/enumer.go @@ -4,8 +4,10 @@ import "fmt" // Arguments to format are: // [1]: type name -const stringValueToNameMap = `func %[1]sString(s string) (%[1]s, error) { - if val, ok := _%[1]sNameToValue_map[s]; ok { +const stringValueToNameMap = `// %[1]sString retrieves an enum value from the enum constants string name. +// Throws an error if the param is not part of the enum. +func %[1]sString(s string) (%[1]s, error) { + if val, ok := _%[1]sNameToValueMap[s]; ok { return val, nil } return 0, fmt.Errorf("%%s does not belong to %[1]s values", s) @@ -14,7 +16,7 @@ const stringValueToNameMap = `func %[1]sString(s string) (%[1]s, error) { func (g *Generator) buildValueToNameMap(runs [][]Value, typeName string, runsThreshold int) { // At this moment, either "g.declareIndexAndNameVars()" or "g.declareNameVars()" has been called - g.Printf("\nvar _%sNameToValue_map = map[string]%s{\n", typeName, typeName) + g.Printf("\nvar _%sNameToValueMap = map[string]%s{\n", typeName, typeName) thereAreRuns := len(runs) > 1 && len(runs) <= runsThreshold var n int var runID string @@ -27,7 +29,7 @@ func (g *Generator) buildValueToNameMap(runs [][]Value, typeName string, runsThr } for _, value := range values { - g.Printf("\t_%s_name%s[%d:%d]: %s,\n", typeName, runID, n, n+len(value.name), &value) + g.Printf("\t_%sName%s[%d:%d]: %s,\n", typeName, runID, n, n+len(value.name), &value) n += len(value.name) } } diff --git a/golden_test.go b/golden_test.go index 72f7bc0..ce5651f 100644 --- a/golden_test.go +++ b/golden_test.go @@ -62,29 +62,29 @@ const ( ` const day_out = ` -const _Day_name = "MondayTuesdayWednesdayThursdayFridaySaturdaySunday" +const _DayName = "MondayTuesdayWednesdayThursdayFridaySaturdaySunday" -var _Day_index = [...]uint8{0, 6, 13, 22, 30, 36, 44, 50} +var _DayIndex = [...]uint8{0, 6, 13, 22, 30, 36, 44, 50} func (i Day) String() string { - if i < 0 || i >= Day(len(_Day_index)-1) { + if i < 0 || i >= Day(len(_DayIndex)-1) { return fmt.Sprintf("Day(%d)", i) } - return _Day_name[_Day_index[i]:_Day_index[i+1]] + return _DayName[_DayIndex[i]:_DayIndex[i+1]] } -var _DayNameToValue_map = map[string]Day{ - _Day_name[0:6]: 0, - _Day_name[6:13]: 1, - _Day_name[13:22]: 2, - _Day_name[22:30]: 3, - _Day_name[30:36]: 4, - _Day_name[36:44]: 5, - _Day_name[44:50]: 6, +var _DayNameToValueMap = map[string]Day{ + _DayName[0:6]: 0, + _DayName[6:13]: 1, + _DayName[13:22]: 2, + _DayName[22:30]: 3, + _DayName[30:36]: 4, + _DayName[36:44]: 5, + _DayName[44:50]: 6, } func DayString(s string) (Day, error) { - if val, ok := _DayNameToValue_map[s]; ok { + if val, ok := _DayNameToValueMap[s]; ok { return val, nil } return 0, fmt.Errorf("%s does not belong to Day values", s) @@ -104,26 +104,26 @@ const ( ` const offset_out = ` -const _Number_name = "OneTwoThree" +const _NumberName = "OneTwoThree" -var _Number_index = [...]uint8{0, 3, 6, 11} +var _NumberIndex = [...]uint8{0, 3, 6, 11} func (i Number) String() string { i -= 1 - if i < 0 || i >= Number(len(_Number_index)-1) { + if i < 0 || i >= Number(len(_NumberIndex)-1) { return fmt.Sprintf("Number(%d)", i+1) } - return _Number_name[_Number_index[i]:_Number_index[i+1]] + return _NumberName[_NumberIndex[i]:_NumberIndex[i+1]] } -var _NumberNameToValue_map = map[string]Number{ - _Number_name[0:3]: 1, - _Number_name[3:6]: 2, - _Number_name[6:11]: 3, +var _NumberNameToValueMap = map[string]Number{ + _NumberName[0:3]: 1, + _NumberName[3:6]: 2, + _NumberName[6:11]: 3, } func NumberString(s string) (Number, error) { - if val, ok := _NumberNameToValue_map[s]; ok { + if val, ok := _NumberNameToValueMap[s]; ok { return val, nil } return 0, fmt.Errorf("%s does not belong to Number values", s) @@ -146,45 +146,45 @@ const ( const gap_out = ` const ( - _Gap_name_0 = "TwoThree" - _Gap_name_1 = "FiveSixSevenEightNine" - _Gap_name_2 = "Eleven" + _GapName_0 = "TwoThree" + _GapName_1 = "FiveSixSevenEightNine" + _GapName_2 = "Eleven" ) var ( - _Gap_index_0 = [...]uint8{0, 3, 8} - _Gap_index_1 = [...]uint8{0, 4, 7, 12, 17, 21} - _Gap_index_2 = [...]uint8{0, 6} + _GapIndex_0 = [...]uint8{0, 3, 8} + _GapIndex_1 = [...]uint8{0, 4, 7, 12, 17, 21} + _GapIndex_2 = [...]uint8{0, 6} ) func (i Gap) String() string { switch { case 2 <= i && i <= 3: i -= 2 - return _Gap_name_0[_Gap_index_0[i]:_Gap_index_0[i+1]] + return _GapName_0[_GapIndex_0[i]:_GapIndex_0[i+1]] case 5 <= i && i <= 9: i -= 5 - return _Gap_name_1[_Gap_index_1[i]:_Gap_index_1[i+1]] + return _GapName_1[_GapIndex_1[i]:_GapIndex_1[i+1]] case i == 11: - return _Gap_name_2 + return _GapName_2 default: return fmt.Sprintf("Gap(%d)", i) } } -var _GapNameToValue_map = map[string]Gap{ - _Gap_name_0[0:3]: 2, - _Gap_name_0[3:8]: 3, - _Gap_name_1[0:4]: 5, - _Gap_name_1[4:7]: 6, - _Gap_name_1[7:12]: 7, - _Gap_name_1[12:17]: 8, - _Gap_name_1[17:21]: 9, - _Gap_name_2[0:6]: 11, +var _GapNameToValueMap = map[string]Gap{ + _GapName_0[0:3]: 2, + _GapName_0[3:8]: 3, + _GapName_1[0:4]: 5, + _GapName_1[4:7]: 6, + _GapName_1[7:12]: 7, + _GapName_1[12:17]: 8, + _GapName_1[17:21]: 9, + _GapName_2[0:6]: 11, } func GapString(s string) (Gap, error) { - if val, ok := _GapNameToValue_map[s]; ok { + if val, ok := _GapNameToValueMap[s]; ok { return val, nil } return 0, fmt.Errorf("%s does not belong to Gap values", s) @@ -203,28 +203,28 @@ const ( ` const num_out = ` -const _Num_name = "m_2m_1m0m1m2" +const _NumName = "m_2m_1m0m1m2" -var _Num_index = [...]uint8{0, 3, 6, 8, 10, 12} +var _NumIndex = [...]uint8{0, 3, 6, 8, 10, 12} func (i Num) String() string { i -= -2 - if i < 0 || i >= Num(len(_Num_index)-1) { + if i < 0 || i >= Num(len(_NumIndex)-1) { return fmt.Sprintf("Num(%d)", i+-2) } - return _Num_name[_Num_index[i]:_Num_index[i+1]] + return _NumName[_NumIndex[i]:_NumIndex[i+1]] } -var _NumNameToValue_map = map[string]Num{ - _Num_name[0:3]: -2, - _Num_name[3:6]: -1, - _Num_name[6:8]: 0, - _Num_name[8:10]: 1, - _Num_name[10:12]: 2, +var _NumNameToValueMap = map[string]Num{ + _NumName[0:3]: -2, + _NumName[3:6]: -1, + _NumName[6:8]: 0, + _NumName[8:10]: 1, + _NumName[10:12]: 2, } func NumString(s string) (Num, error) { - if val, ok := _NumNameToValue_map[s]; ok { + if val, ok := _NumNameToValueMap[s]; ok { return val, nil } return 0, fmt.Errorf("%s does not belong to Num values", s) @@ -247,37 +247,37 @@ const ( const unum_out = ` const ( - _Unum_name_0 = "m0m1m2" - _Unum_name_1 = "m_2m_1" + _UnumName_0 = "m0m1m2" + _UnumName_1 = "m_2m_1" ) var ( - _Unum_index_0 = [...]uint8{0, 2, 4, 6} - _Unum_index_1 = [...]uint8{0, 3, 6} + _UnumIndex_0 = [...]uint8{0, 2, 4, 6} + _UnumIndex_1 = [...]uint8{0, 3, 6} ) func (i Unum) String() string { switch { case 0 <= i && i <= 2: - return _Unum_name_0[_Unum_index_0[i]:_Unum_index_0[i+1]] + return _UnumName_0[_UnumIndex_0[i]:_UnumIndex_0[i+1]] case 253 <= i && i <= 254: i -= 253 - return _Unum_name_1[_Unum_index_1[i]:_Unum_index_1[i+1]] + return _UnumName_1[_UnumIndex_1[i]:_UnumIndex_1[i+1]] default: return fmt.Sprintf("Unum(%d)", i) } } -var _UnumNameToValue_map = map[string]Unum{ - _Unum_name_0[0:2]: 0, - _Unum_name_0[2:4]: 1, - _Unum_name_0[4:6]: 2, - _Unum_name_1[0:3]: 253, - _Unum_name_1[3:6]: 254, +var _UnumNameToValueMap = map[string]Unum{ + _UnumName_0[0:2]: 0, + _UnumName_0[2:4]: 1, + _UnumName_0[4:6]: 2, + _UnumName_1[0:3]: 253, + _UnumName_1[3:6]: 254, } func UnumString(s string) (Unum, error) { - if val, ok := _UnumNameToValue_map[s]; ok { + if val, ok := _UnumNameToValueMap[s]; ok { return val, nil } return 0, fmt.Errorf("%s does not belong to Unum values", s) @@ -306,49 +306,49 @@ const ( ` const prime_out = ` -const _Prime_name = "p2p3p5p7p11p13p17p19p23p29p37p41p43" +const _PrimeName = "p2p3p5p7p11p13p17p19p23p29p37p41p43" -var _Prime_map = map[Prime]string{ - 2: _Prime_name[0:2], - 3: _Prime_name[2:4], - 5: _Prime_name[4:6], - 7: _Prime_name[6:8], - 11: _Prime_name[8:11], - 13: _Prime_name[11:14], - 17: _Prime_name[14:17], - 19: _Prime_name[17:20], - 23: _Prime_name[20:23], - 29: _Prime_name[23:26], - 31: _Prime_name[26:29], - 41: _Prime_name[29:32], - 43: _Prime_name[32:35], +var _PrimeMap = map[Prime]string{ + 2: _PrimeName[0:2], + 3: _PrimeName[2:4], + 5: _PrimeName[4:6], + 7: _PrimeName[6:8], + 11: _PrimeName[8:11], + 13: _PrimeName[11:14], + 17: _PrimeName[14:17], + 19: _PrimeName[17:20], + 23: _PrimeName[20:23], + 29: _PrimeName[23:26], + 31: _PrimeName[26:29], + 41: _PrimeName[29:32], + 43: _PrimeName[32:35], } func (i Prime) String() string { - if str, ok := _Prime_map[i]; ok { + if str, ok := _PrimeMap[i]; ok { return str } return fmt.Sprintf("Prime(%d)", i) } -var _PrimeNameToValue_map = map[string]Prime{ - _Prime_name[0:2]: 2, - _Prime_name[2:4]: 3, - _Prime_name[4:6]: 5, - _Prime_name[6:8]: 7, - _Prime_name[8:11]: 11, - _Prime_name[11:14]: 13, - _Prime_name[14:17]: 17, - _Prime_name[17:20]: 19, - _Prime_name[20:23]: 23, - _Prime_name[23:26]: 29, - _Prime_name[26:29]: 31, - _Prime_name[29:32]: 41, - _Prime_name[32:35]: 43, +var _PrimeNameToValueMap = map[string]Prime{ + _PrimeName[0:2]: 2, + _PrimeName[2:4]: 3, + _PrimeName[4:6]: 5, + _PrimeName[6:8]: 7, + _PrimeName[8:11]: 11, + _PrimeName[11:14]: 13, + _PrimeName[14:17]: 17, + _PrimeName[17:20]: 19, + _PrimeName[20:23]: 23, + _PrimeName[23:26]: 29, + _PrimeName[26:29]: 31, + _PrimeName[29:32]: 41, + _PrimeName[32:35]: 43, } func PrimeString(s string) (Prime, error) { - if val, ok := _PrimeNameToValue_map[s]; ok { + if val, ok := _PrimeNameToValueMap[s]; ok { return val, nil } return 0, fmt.Errorf("%s does not belong to Prime values", s) @@ -374,49 +374,49 @@ const ( ` const prime_json_out = ` -const _Prime_name = "p2p3p5p7p11p13p17p19p23p29p37p41p43" +const _PrimeName = "p2p3p5p7p11p13p17p19p23p29p37p41p43" -var _Prime_map = map[Prime]string{ - 2: _Prime_name[0:2], - 3: _Prime_name[2:4], - 5: _Prime_name[4:6], - 7: _Prime_name[6:8], - 11: _Prime_name[8:11], - 13: _Prime_name[11:14], - 17: _Prime_name[14:17], - 19: _Prime_name[17:20], - 23: _Prime_name[20:23], - 29: _Prime_name[23:26], - 31: _Prime_name[26:29], - 41: _Prime_name[29:32], - 43: _Prime_name[32:35], +var _PrimeMap = map[Prime]string{ + 2: _PrimeName[0:2], + 3: _PrimeName[2:4], + 5: _PrimeName[4:6], + 7: _PrimeName[6:8], + 11: _PrimeName[8:11], + 13: _PrimeName[11:14], + 17: _PrimeName[14:17], + 19: _PrimeName[17:20], + 23: _PrimeName[20:23], + 29: _PrimeName[23:26], + 31: _PrimeName[26:29], + 41: _PrimeName[29:32], + 43: _PrimeName[32:35], } func (i Prime) String() string { - if str, ok := _Prime_map[i]; ok { + if str, ok := _PrimeMap[i]; ok { return str } return fmt.Sprintf("Prime(%d)", i) } -var _PrimeNameToValue_map = map[string]Prime{ - _Prime_name[0:2]: 2, - _Prime_name[2:4]: 3, - _Prime_name[4:6]: 5, - _Prime_name[6:8]: 7, - _Prime_name[8:11]: 11, - _Prime_name[11:14]: 13, - _Prime_name[14:17]: 17, - _Prime_name[17:20]: 19, - _Prime_name[20:23]: 23, - _Prime_name[23:26]: 29, - _Prime_name[26:29]: 31, - _Prime_name[29:32]: 41, - _Prime_name[32:35]: 43, +var _PrimeNameToValueMap = map[string]Prime{ + _PrimeName[0:2]: 2, + _PrimeName[2:4]: 3, + _PrimeName[4:6]: 5, + _PrimeName[6:8]: 7, + _PrimeName[8:11]: 11, + _PrimeName[11:14]: 13, + _PrimeName[14:17]: 17, + _PrimeName[17:20]: 19, + _PrimeName[20:23]: 23, + _PrimeName[23:26]: 29, + _PrimeName[26:29]: 31, + _PrimeName[29:32]: 41, + _PrimeName[32:35]: 43, } func PrimeString(s string) (Prime, error) { - if val, ok := _PrimeNameToValue_map[s]; ok { + if val, ok := _PrimeNameToValueMap[s]; ok { return val, nil } return 0, fmt.Errorf("%s does not belong to Prime values", s) @@ -458,49 +458,49 @@ const ( ` const prime_yaml_out = ` -const _Prime_name = "p2p3p5p7p11p13p17p19p23p29p37p41p43" +const _PrimeName = "p2p3p5p7p11p13p17p19p23p29p37p41p43" -var _Prime_map = map[Prime]string{ - 2: _Prime_name[0:2], - 3: _Prime_name[2:4], - 5: _Prime_name[4:6], - 7: _Prime_name[6:8], - 11: _Prime_name[8:11], - 13: _Prime_name[11:14], - 17: _Prime_name[14:17], - 19: _Prime_name[17:20], - 23: _Prime_name[20:23], - 29: _Prime_name[23:26], - 31: _Prime_name[26:29], - 41: _Prime_name[29:32], - 43: _Prime_name[32:35], +var _PrimeMap = map[Prime]string{ + 2: _PrimeName[0:2], + 3: _PrimeName[2:4], + 5: _PrimeName[4:6], + 7: _PrimeName[6:8], + 11: _PrimeName[8:11], + 13: _PrimeName[11:14], + 17: _PrimeName[14:17], + 19: _PrimeName[17:20], + 23: _PrimeName[20:23], + 29: _PrimeName[23:26], + 31: _PrimeName[26:29], + 41: _PrimeName[29:32], + 43: _PrimeName[32:35], } func (i Prime) String() string { - if str, ok := _Prime_map[i]; ok { + if str, ok := _PrimeMap[i]; ok { return str } return fmt.Sprintf("Prime(%d)", i) } -var _PrimeNameToValue_map = map[string]Prime{ - _Prime_name[0:2]: 2, - _Prime_name[2:4]: 3, - _Prime_name[4:6]: 5, - _Prime_name[6:8]: 7, - _Prime_name[8:11]: 11, - _Prime_name[11:14]: 13, - _Prime_name[14:17]: 17, - _Prime_name[17:20]: 19, - _Prime_name[20:23]: 23, - _Prime_name[23:26]: 29, - _Prime_name[26:29]: 31, - _Prime_name[29:32]: 41, - _Prime_name[32:35]: 43, +var _PrimeNameToValueMap = map[string]Prime{ + _PrimeName[0:2]: 2, + _PrimeName[2:4]: 3, + _PrimeName[4:6]: 5, + _PrimeName[6:8]: 7, + _PrimeName[8:11]: 11, + _PrimeName[11:14]: 13, + _PrimeName[14:17]: 17, + _PrimeName[17:20]: 19, + _PrimeName[20:23]: 23, + _PrimeName[23:26]: 29, + _PrimeName[26:29]: 31, + _PrimeName[29:32]: 41, + _PrimeName[32:35]: 43, } func PrimeString(s string) (Prime, error) { - if val, ok := _PrimeNameToValue_map[s]; ok { + if val, ok := _PrimeNameToValueMap[s]; ok { return val, nil } return 0, fmt.Errorf("%s does not belong to Prime values", s) @@ -542,49 +542,49 @@ const ( ` const prime_sql_out = ` -const _Prime_name = "p2p3p5p7p11p13p17p19p23p29p37p41p43" +const _PrimeName = "p2p3p5p7p11p13p17p19p23p29p37p41p43" -var _Prime_map = map[Prime]string{ - 2: _Prime_name[0:2], - 3: _Prime_name[2:4], - 5: _Prime_name[4:6], - 7: _Prime_name[6:8], - 11: _Prime_name[8:11], - 13: _Prime_name[11:14], - 17: _Prime_name[14:17], - 19: _Prime_name[17:20], - 23: _Prime_name[20:23], - 29: _Prime_name[23:26], - 31: _Prime_name[26:29], - 41: _Prime_name[29:32], - 43: _Prime_name[32:35], +var _PrimeMap = map[Prime]string{ + 2: _PrimeName[0:2], + 3: _PrimeName[2:4], + 5: _PrimeName[4:6], + 7: _PrimeName[6:8], + 11: _PrimeName[8:11], + 13: _PrimeName[11:14], + 17: _PrimeName[14:17], + 19: _PrimeName[17:20], + 23: _PrimeName[20:23], + 29: _PrimeName[23:26], + 31: _PrimeName[26:29], + 41: _PrimeName[29:32], + 43: _PrimeName[32:35], } func (i Prime) String() string { - if str, ok := _Prime_map[i]; ok { + if str, ok := _PrimeMap[i]; ok { return str } return fmt.Sprintf("Prime(%d)", i) } -var _PrimeNameToValue_map = map[string]Prime{ - _Prime_name[0:2]: 2, - _Prime_name[2:4]: 3, - _Prime_name[4:6]: 5, - _Prime_name[6:8]: 7, - _Prime_name[8:11]: 11, - _Prime_name[11:14]: 13, - _Prime_name[14:17]: 17, - _Prime_name[17:20]: 19, - _Prime_name[20:23]: 23, - _Prime_name[23:26]: 29, - _Prime_name[26:29]: 31, - _Prime_name[29:32]: 41, - _Prime_name[32:35]: 43, +var _PrimeNameToValueMap = map[string]Prime{ + _PrimeName[0:2]: 2, + _PrimeName[2:4]: 3, + _PrimeName[4:6]: 5, + _PrimeName[6:8]: 7, + _PrimeName[8:11]: 11, + _PrimeName[11:14]: 13, + _PrimeName[14:17]: 17, + _PrimeName[17:20]: 19, + _PrimeName[20:23]: 23, + _PrimeName[23:26]: 29, + _PrimeName[26:29]: 31, + _PrimeName[29:32]: 41, + _PrimeName[32:35]: 43, } func PrimeString(s string) (Prime, error) { - if val, ok := _PrimeNameToValue_map[s]; ok { + if val, ok := _PrimeNameToValueMap[s]; ok { return val, nil } return 0, fmt.Errorf("%s does not belong to Prime values", s) @@ -639,49 +639,49 @@ const ( ` const prime_json_and_sql_out = ` -const _Prime_name = "p2p3p5p7p11p13p17p19p23p29p37p41p43" +const _PrimeName = "p2p3p5p7p11p13p17p19p23p29p37p41p43" -var _Prime_map = map[Prime]string{ - 2: _Prime_name[0:2], - 3: _Prime_name[2:4], - 5: _Prime_name[4:6], - 7: _Prime_name[6:8], - 11: _Prime_name[8:11], - 13: _Prime_name[11:14], - 17: _Prime_name[14:17], - 19: _Prime_name[17:20], - 23: _Prime_name[20:23], - 29: _Prime_name[23:26], - 31: _Prime_name[26:29], - 41: _Prime_name[29:32], - 43: _Prime_name[32:35], +var _PrimeMap = map[Prime]string{ + 2: _PrimeName[0:2], + 3: _PrimeName[2:4], + 5: _PrimeName[4:6], + 7: _PrimeName[6:8], + 11: _PrimeName[8:11], + 13: _PrimeName[11:14], + 17: _PrimeName[14:17], + 19: _PrimeName[17:20], + 23: _PrimeName[20:23], + 29: _PrimeName[23:26], + 31: _PrimeName[26:29], + 41: _PrimeName[29:32], + 43: _PrimeName[32:35], } func (i Prime) String() string { - if str, ok := _Prime_map[i]; ok { + if str, ok := _PrimeMap[i]; ok { return str } return fmt.Sprintf("Prime(%d)", i) } -var _PrimeNameToValue_map = map[string]Prime{ - _Prime_name[0:2]: 2, - _Prime_name[2:4]: 3, - _Prime_name[4:6]: 5, - _Prime_name[6:8]: 7, - _Prime_name[8:11]: 11, - _Prime_name[11:14]: 13, - _Prime_name[14:17]: 17, - _Prime_name[17:20]: 19, - _Prime_name[20:23]: 23, - _Prime_name[23:26]: 29, - _Prime_name[26:29]: 31, - _Prime_name[29:32]: 41, - _Prime_name[32:35]: 43, +var _PrimeNameToValueMap = map[string]Prime{ + _PrimeName[0:2]: 2, + _PrimeName[2:4]: 3, + _PrimeName[4:6]: 5, + _PrimeName[6:8]: 7, + _PrimeName[8:11]: 11, + _PrimeName[11:14]: 13, + _PrimeName[14:17]: 17, + _PrimeName[17:20]: 19, + _PrimeName[20:23]: 23, + _PrimeName[23:26]: 29, + _PrimeName[26:29]: 31, + _PrimeName[29:32]: 41, + _PrimeName[32:35]: 43, } func PrimeString(s string) (Prime, error) { - if val, ok := _PrimeNameToValue_map[s]; ok { + if val, ok := _PrimeNameToValueMap[s]; ok { return val, nil } return 0, fmt.Errorf("%s does not belong to Prime values", s) diff --git a/stringer.go b/stringer.go index ccfb304..fa7c383 100644 --- a/stringer.go +++ b/stringer.go @@ -58,6 +58,11 @@ // where t is the lower-cased name of the first type listed. It can be overridden // with the -output flag. // +// ------ +// This is a patched versin of the original stringer. The original generates source code +// using '_' (underscore) as part of generated golang identifiers. This creates a number of +// golint warnings. The patched version simply leaves the '_' out + package main import ( @@ -559,10 +564,10 @@ func (g *Generator) createIndexAndNameDecl(run []Value, typeName string, suffix b.WriteString(run[i].name) indexes[i] = b.Len() } - nameConst := fmt.Sprintf("_%s_name%s = %q", typeName, suffix, b.String()) + nameConst := fmt.Sprintf("_%sName%s = %q", typeName, suffix, b.String()) nameLen := b.Len() b.Reset() - fmt.Fprintf(b, "_%s_index%s = [...]uint%d{0, ", typeName, suffix, usize(nameLen)) + fmt.Fprintf(b, "_%sIndex%s = [...]uint%d{0, ", typeName, suffix, usize(nameLen)) for i, v := range indexes { if i > 0 { fmt.Fprintf(b, ", ") @@ -575,7 +580,7 @@ func (g *Generator) createIndexAndNameDecl(run []Value, typeName string, suffix // declareNameVars declares the concatenated names string representing all the values in the runs. func (g *Generator) declareNameVars(runs [][]Value, typeName string, suffix string) { - g.Printf("const _%s_name%s = \"", typeName, suffix) + g.Printf("const _%sName%s = \"", typeName, suffix) for _, run := range runs { for i := range run { g.Printf("%s", run[i].name) @@ -606,10 +611,10 @@ func (g *Generator) buildOneRun(runs [][]Value, typeName string) { // [2]: size of index element (8 for uint8 etc.) // [3]: less than zero check (for signed types) const stringOneRun = `func (i %[1]s) String() string { - if %[3]si >= %[1]s(len(_%[1]s_index)-1) { + if %[3]si >= %[1]s(len(_%[1]sIndex)-1) { return fmt.Sprintf("%[1]s(%%d)", i) } - return _%[1]s_name[_%[1]s_index[i]:_%[1]s_index[i+1]] + return _%[1]sName[_%[1]sIndex[i]:_%[1]sIndex[i+1]] } ` @@ -622,10 +627,10 @@ const stringOneRun = `func (i %[1]s) String() string { */ const stringOneRunWithOffset = `func (i %[1]s) String() string { i -= %[2]s - if %[4]si >= %[1]s(len(_%[1]s_index)-1) { + if %[4]si >= %[1]s(len(_%[1]sIndex)-1) { return fmt.Sprintf("%[1]s(%%d)", i + %[2]s) } - return _%[1]s_name[_%[1]s_index[i] : _%[1]s_index[i+1]] + return _%[1]sName[_%[1]sIndex[i] : _%[1]sIndex[i+1]] } ` @@ -639,14 +644,14 @@ func (g *Generator) buildMultipleRuns(runs [][]Value, typeName string) { for i, values := range runs { if len(values) == 1 { g.Printf("\tcase i == %s:\n", &values[0]) - g.Printf("\t\treturn _%s_name_%d\n", typeName, i) + g.Printf("\t\treturn _%sName_%d\n", typeName, i) continue } g.Printf("\tcase %s <= i && i <= %s:\n", &values[0], &values[len(values)-1]) if values[0].value != 0 { g.Printf("\t\ti -= %s\n", &values[0]) } - g.Printf("\t\treturn _%s_name_%d[_%s_index_%d[i]:_%s_index_%d[i+1]]\n", + g.Printf("\t\treturn _%sName_%d[_%sIndex_%d[i]:_%sIndex_%d[i+1]]\n", typeName, i, typeName, i, typeName, i) } g.Printf("\tdefault:\n") @@ -660,11 +665,11 @@ func (g *Generator) buildMultipleRuns(runs [][]Value, typeName string) { func (g *Generator) buildMap(runs [][]Value, typeName string) { g.Printf("\n") g.declareNameVars(runs, typeName, "") - g.Printf("\nvar _%s_map = map[%s]string{\n", typeName, typeName) + g.Printf("\nvar _%sMap = map[%s]string{\n", typeName, typeName) n := 0 for _, values := range runs { for _, value := range values { - g.Printf("\t%s: _%s_name[%d:%d],\n", &value, typeName, n, n+len(value.name)) + g.Printf("\t%s: _%sName[%d:%d],\n", &value, typeName, n, n+len(value.name)) n += len(value.name) } } @@ -674,7 +679,7 @@ func (g *Generator) buildMap(runs [][]Value, typeName string) { // Argument to format is the type name. const stringMap = `func (i %[1]s) String() string { - if str, ok := _%[1]s_map[i]; ok { + if str, ok := _%[1]sMap[i]; ok { return str } return fmt.Sprintf("%[1]s(%%d)", i) From 95a15ae31e4a6fcd203ccc3bc7c6c74b63c31ce1 Mon Sep 17 00:00:00 2001 From: Michael Wolber Date: Thu, 4 Jan 2018 11:38:06 +0100 Subject: [PATCH 2/3] Updated stringer.go to current version (2018/01) --- stringer.go | 120 ++++++++++++++++++++-------------------------------- 1 file changed, 47 insertions(+), 73 deletions(-) diff --git a/stringer.go b/stringer.go index fa7c383..e3f5067 100644 --- a/stringer.go +++ b/stringer.go @@ -2,8 +2,6 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build go1.5 - // Stringer is a tool to automate the creation of methods that satisfy the fmt.Stringer // interface. Given the name of a (signed or unsigned) integer type T that has constants // defined, stringer will create a new self-contained Go source file implementing @@ -58,8 +56,8 @@ // where t is the lower-cased name of the first type listed. It can be overridden // with the -output flag. // -// ------ -// This is a patched versin of the original stringer. The original generates source code +// ----------------- +// This is a patched version of the original stringer. The original generates source code // using '_' (underscore) as part of generated golang identifiers. This creates a number of // golint warnings. The patched version simply leaves the '_' out @@ -86,12 +84,10 @@ import ( ) var ( - typeNames = flag.String("type", "", "comma-separated list of type names; must be set") - sql = flag.Bool("sql", false, "if true, the Scanner and Valuer interface will be implemented.") - json = flag.Bool("json", false, "if true, json marshaling methods will be generated. Default: false") - yaml = flag.Bool("yaml", false, "if true, yaml marshaling methods will be generated. Default: false") - output = flag.String("output", "", "output file name; default srcdir/_string.go") - transformMethod = flag.String("transform", "noop", "enum item name transformation method. Default: noop") + typeNames = flag.String("type", "", "comma-separated list of type names; must be set") + output = flag.String("output", "", "output file name; default srcdir/_string.go") + trimprefix = flag.String("trimprefix", "", "trim the `prefix` from the generated constant names") + linecomment = flag.Bool("linecomment", false, "use line comment text as printed text when present") ) // Usage is a replacement usage function for the flags package. @@ -107,7 +103,7 @@ func Usage() { func main() { log.SetFlags(0) - log.SetPrefix("enumer: ") + log.SetPrefix("stringer: ") flag.Usage = Usage flag.Parse() if len(*typeNames) == 0 { @@ -124,11 +120,11 @@ func main() { } // Parse the package once. - var ( - dir string - g Generator - ) - + var dir string + g := Generator{ + trimPrefix: *trimprefix, + lineComment: *linecomment, + } if len(args) == 1 && isDirectory(args[0]) { dir = args[0] g.parsePackageDir(args[0]) @@ -138,23 +134,15 @@ func main() { } // Print the header and package clause. - g.Printf("// Code generated by \"enumer %s\"; DO NOT EDIT\n", strings.Join(os.Args[1:], " ")) + g.Printf("// Code generated by \"stringer %s\"; DO NOT EDIT.\n", strings.Join(os.Args[1:], " ")) g.Printf("\n") g.Printf("package %s", g.pkg.name) g.Printf("\n") - g.Printf("import (\n") - g.Printf("\t\"fmt\"\n") - if *sql { - g.Printf("\t\"database/sql/driver\"\n") - } - if *json { - g.Printf("\t\"encoding/json\"\n") - } - g.Printf(")\n") + g.Printf("import \"strconv\"\n") // Used by all methods. // Run generate for each type. for _, typeName := range types { - g.generate(typeName, *json, *yaml, *sql, *transformMethod) + g.generate(typeName) } // Format the output. @@ -186,6 +174,9 @@ func isDirectory(name string) bool { type Generator struct { buf bytes.Buffer // Accumulated output. pkg *Package // Package we are scanning. + + trimPrefix string + lineComment bool } func (g *Generator) Printf(format string, args ...interface{}) { @@ -199,6 +190,9 @@ type File struct { // These fields are reset for each type being generated. typeName string // Name of the constant type. values []Value // Accumulator for constant values of that type. + + trimPrefix string + lineComment bool } type Package struct { @@ -255,14 +249,16 @@ func (g *Generator) parsePackage(directory string, names []string, text interfac if !strings.HasSuffix(name, ".go") { continue } - parsedFile, err := parser.ParseFile(fs, name, text, 0) + parsedFile, err := parser.ParseFile(fs, name, text, parser.ParseComments) if err != nil { log.Fatalf("parsing package: %s: %s", name, err) } astFiles = append(astFiles, parsedFile) files = append(files, &File{ - file: parsedFile, - pkg: g.pkg, + file: parsedFile, + pkg: g.pkg, + trimPrefix: g.trimPrefix, + lineComment: g.lineComment, }) } if len(astFiles) == 0 { @@ -289,24 +285,8 @@ func (pkg *Package) check(fs *token.FileSet, astFiles []*ast.File) { pkg.typesPkg = typesPkg } -func (g *Generator) transformValueNames(values []Value, transformMethod string) { - var transform func(string) string - switch transformMethod { - case "snake": - transform = toSnakeCase - case "kebab": - transform = toKebabCase - default: - return - } - - for i := range values { - values[i].name = transform(values[i].name) - } -} - // generate produces the String method for the named type. -func (g *Generator) generate(typeName string, includeJSON, includeYAML, includeSQL bool, transformMethod string) { +func (g *Generator) generate(typeName string) { values := make([]Value, 0, 100) for _, file := range g.pkg.files { // Set the state for this run of the walker. @@ -321,9 +301,6 @@ func (g *Generator) generate(typeName string, includeJSON, includeYAML, includeS if len(values) == 0 { log.Fatalf("no values defined for type %s", typeName) } - - g.transformValueNames(values, transformMethod) - runs := splitIntoRuns(values) // The decision of which pattern to use depends on the number of // runs in the numbers. If there's only one, it's easy. For more than @@ -337,26 +314,14 @@ func (g *Generator) generate(typeName string, includeJSON, includeYAML, includeS // being necessary for any realistic example other than bitmasks // is very low. And bitmasks probably deserve their own analysis, // to be done some other day. - const runsThreshold = 10 switch { case len(runs) == 1: g.buildOneRun(runs, typeName) - case len(runs) <= runsThreshold: + case len(runs) <= 10: g.buildMultipleRuns(runs, typeName) default: g.buildMap(runs, typeName) } - - g.buildValueToNameMap(runs, typeName, runsThreshold) - if includeJSON { - g.buildJSONMethods(runs, typeName, runsThreshold) - } - if includeYAML { - g.buildYAMLMethods(runs, typeName, runsThreshold) - } - if includeSQL { - g.addValueAndScanMethod(typeName) - } } // splitIntoRuns breaks the values into runs of contiguous sequences. @@ -406,7 +371,7 @@ func (g *Generator) format() []byte { // Value represents a declared constant. type Value struct { - name string // The name of the constant after transformation (i.e. camel case => snake case) + name string // The name of the constant. // The value is stored as a bit pattern alone. The boolean tells us // whether to interpret it as an int64 or a uint64; the only place // this matters is when sorting. @@ -505,6 +470,10 @@ func (f *File) genDecl(node ast.Node) bool { signed: info&types.IsUnsigned == 0, str: value.String(), } + if c := vspec.Comment; f.lineComment && c != nil && len(c.List) == 1 { + v.name = strings.TrimSpace(c.Text()) + } + v.name = strings.TrimPrefix(v.name, f.trimPrefix) f.values = append(f.values, v) } } @@ -534,7 +503,9 @@ func (g *Generator) declareIndexAndNameVars(runs [][]Value, typeName string) { var indexes, names []string for i, run := range runs { index, name := g.createIndexAndNameDecl(run, typeName, fmt.Sprintf("_%d", i)) - indexes = append(indexes, index) + if len(run) != 1 { + indexes = append(indexes, index) + } names = append(names, name) } g.Printf("const (\n") @@ -542,11 +513,14 @@ func (g *Generator) declareIndexAndNameVars(runs [][]Value, typeName string) { g.Printf("\t%s\n", name) } g.Printf(")\n\n") - g.Printf("var (") - for _, index := range indexes { - g.Printf("\t%s\n", index) + + if len(indexes) > 0 { + g.Printf("var (") + for _, index := range indexes { + g.Printf("\t%s\n", index) + } + g.Printf(")\n\n") } - g.Printf(")\n\n") } // declareIndexAndNameVar is the single-run version of declareIndexAndNameVars @@ -612,7 +586,7 @@ func (g *Generator) buildOneRun(runs [][]Value, typeName string) { // [3]: less than zero check (for signed types) const stringOneRun = `func (i %[1]s) String() string { if %[3]si >= %[1]s(len(_%[1]sIndex)-1) { - return fmt.Sprintf("%[1]s(%%d)", i) + return "%[1]s(" + strconv.FormatInt(int64(i), 10) + ")" } return _%[1]sName[_%[1]sIndex[i]:_%[1]sIndex[i+1]] } @@ -628,7 +602,7 @@ const stringOneRun = `func (i %[1]s) String() string { const stringOneRunWithOffset = `func (i %[1]s) String() string { i -= %[2]s if %[4]si >= %[1]s(len(_%[1]sIndex)-1) { - return fmt.Sprintf("%[1]s(%%d)", i + %[2]s) + return "%[1]s(" + strconv.FormatInt(int64(i + %[2]s), 10) + ")" } return _%[1]sName[_%[1]sIndex[i] : _%[1]sIndex[i+1]] } @@ -655,7 +629,7 @@ func (g *Generator) buildMultipleRuns(runs [][]Value, typeName string) { typeName, i, typeName, i, typeName, i) } g.Printf("\tdefault:\n") - g.Printf("\t\treturn fmt.Sprintf(\"%s(%%d)\", i)\n", typeName) + g.Printf("\t\treturn \"%s(\" + strconv.FormatInt(int64(i), 10) + \")\"\n", typeName) g.Printf("\t}\n") g.Printf("}\n") } @@ -682,6 +656,6 @@ const stringMap = `func (i %[1]s) String() string { if str, ok := _%[1]sMap[i]; ok { return str } - return fmt.Sprintf("%[1]s(%%d)", i) + return "%[1]s(" + strconv.FormatInt(int64(i), 10) + ")" } ` From 5d97e5e788a7daa135b98de5db0fc8674d8b8e14 Mon Sep 17 00:00:00 2001 From: Michael Wolber Date: Thu, 4 Jan 2018 12:01:03 +0100 Subject: [PATCH 3/3] Revert "Updated stringer.go to current version (2018/01)" This reverts commit 95a15ae31e4a6fcd203ccc3bc7c6c74b63c31ce1. --- stringer.go | 120 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 73 insertions(+), 47 deletions(-) diff --git a/stringer.go b/stringer.go index e3f5067..fa7c383 100644 --- a/stringer.go +++ b/stringer.go @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// +build go1.5 + // Stringer is a tool to automate the creation of methods that satisfy the fmt.Stringer // interface. Given the name of a (signed or unsigned) integer type T that has constants // defined, stringer will create a new self-contained Go source file implementing @@ -56,8 +58,8 @@ // where t is the lower-cased name of the first type listed. It can be overridden // with the -output flag. // -// ----------------- -// This is a patched version of the original stringer. The original generates source code +// ------ +// This is a patched versin of the original stringer. The original generates source code // using '_' (underscore) as part of generated golang identifiers. This creates a number of // golint warnings. The patched version simply leaves the '_' out @@ -84,10 +86,12 @@ import ( ) var ( - typeNames = flag.String("type", "", "comma-separated list of type names; must be set") - output = flag.String("output", "", "output file name; default srcdir/_string.go") - trimprefix = flag.String("trimprefix", "", "trim the `prefix` from the generated constant names") - linecomment = flag.Bool("linecomment", false, "use line comment text as printed text when present") + typeNames = flag.String("type", "", "comma-separated list of type names; must be set") + sql = flag.Bool("sql", false, "if true, the Scanner and Valuer interface will be implemented.") + json = flag.Bool("json", false, "if true, json marshaling methods will be generated. Default: false") + yaml = flag.Bool("yaml", false, "if true, yaml marshaling methods will be generated. Default: false") + output = flag.String("output", "", "output file name; default srcdir/_string.go") + transformMethod = flag.String("transform", "noop", "enum item name transformation method. Default: noop") ) // Usage is a replacement usage function for the flags package. @@ -103,7 +107,7 @@ func Usage() { func main() { log.SetFlags(0) - log.SetPrefix("stringer: ") + log.SetPrefix("enumer: ") flag.Usage = Usage flag.Parse() if len(*typeNames) == 0 { @@ -120,11 +124,11 @@ func main() { } // Parse the package once. - var dir string - g := Generator{ - trimPrefix: *trimprefix, - lineComment: *linecomment, - } + var ( + dir string + g Generator + ) + if len(args) == 1 && isDirectory(args[0]) { dir = args[0] g.parsePackageDir(args[0]) @@ -134,15 +138,23 @@ func main() { } // Print the header and package clause. - g.Printf("// Code generated by \"stringer %s\"; DO NOT EDIT.\n", strings.Join(os.Args[1:], " ")) + g.Printf("// Code generated by \"enumer %s\"; DO NOT EDIT\n", strings.Join(os.Args[1:], " ")) g.Printf("\n") g.Printf("package %s", g.pkg.name) g.Printf("\n") - g.Printf("import \"strconv\"\n") // Used by all methods. + g.Printf("import (\n") + g.Printf("\t\"fmt\"\n") + if *sql { + g.Printf("\t\"database/sql/driver\"\n") + } + if *json { + g.Printf("\t\"encoding/json\"\n") + } + g.Printf(")\n") // Run generate for each type. for _, typeName := range types { - g.generate(typeName) + g.generate(typeName, *json, *yaml, *sql, *transformMethod) } // Format the output. @@ -174,9 +186,6 @@ func isDirectory(name string) bool { type Generator struct { buf bytes.Buffer // Accumulated output. pkg *Package // Package we are scanning. - - trimPrefix string - lineComment bool } func (g *Generator) Printf(format string, args ...interface{}) { @@ -190,9 +199,6 @@ type File struct { // These fields are reset for each type being generated. typeName string // Name of the constant type. values []Value // Accumulator for constant values of that type. - - trimPrefix string - lineComment bool } type Package struct { @@ -249,16 +255,14 @@ func (g *Generator) parsePackage(directory string, names []string, text interfac if !strings.HasSuffix(name, ".go") { continue } - parsedFile, err := parser.ParseFile(fs, name, text, parser.ParseComments) + parsedFile, err := parser.ParseFile(fs, name, text, 0) if err != nil { log.Fatalf("parsing package: %s: %s", name, err) } astFiles = append(astFiles, parsedFile) files = append(files, &File{ - file: parsedFile, - pkg: g.pkg, - trimPrefix: g.trimPrefix, - lineComment: g.lineComment, + file: parsedFile, + pkg: g.pkg, }) } if len(astFiles) == 0 { @@ -285,8 +289,24 @@ func (pkg *Package) check(fs *token.FileSet, astFiles []*ast.File) { pkg.typesPkg = typesPkg } +func (g *Generator) transformValueNames(values []Value, transformMethod string) { + var transform func(string) string + switch transformMethod { + case "snake": + transform = toSnakeCase + case "kebab": + transform = toKebabCase + default: + return + } + + for i := range values { + values[i].name = transform(values[i].name) + } +} + // generate produces the String method for the named type. -func (g *Generator) generate(typeName string) { +func (g *Generator) generate(typeName string, includeJSON, includeYAML, includeSQL bool, transformMethod string) { values := make([]Value, 0, 100) for _, file := range g.pkg.files { // Set the state for this run of the walker. @@ -301,6 +321,9 @@ func (g *Generator) generate(typeName string) { if len(values) == 0 { log.Fatalf("no values defined for type %s", typeName) } + + g.transformValueNames(values, transformMethod) + runs := splitIntoRuns(values) // The decision of which pattern to use depends on the number of // runs in the numbers. If there's only one, it's easy. For more than @@ -314,14 +337,26 @@ func (g *Generator) generate(typeName string) { // being necessary for any realistic example other than bitmasks // is very low. And bitmasks probably deserve their own analysis, // to be done some other day. + const runsThreshold = 10 switch { case len(runs) == 1: g.buildOneRun(runs, typeName) - case len(runs) <= 10: + case len(runs) <= runsThreshold: g.buildMultipleRuns(runs, typeName) default: g.buildMap(runs, typeName) } + + g.buildValueToNameMap(runs, typeName, runsThreshold) + if includeJSON { + g.buildJSONMethods(runs, typeName, runsThreshold) + } + if includeYAML { + g.buildYAMLMethods(runs, typeName, runsThreshold) + } + if includeSQL { + g.addValueAndScanMethod(typeName) + } } // splitIntoRuns breaks the values into runs of contiguous sequences. @@ -371,7 +406,7 @@ func (g *Generator) format() []byte { // Value represents a declared constant. type Value struct { - name string // The name of the constant. + name string // The name of the constant after transformation (i.e. camel case => snake case) // The value is stored as a bit pattern alone. The boolean tells us // whether to interpret it as an int64 or a uint64; the only place // this matters is when sorting. @@ -470,10 +505,6 @@ func (f *File) genDecl(node ast.Node) bool { signed: info&types.IsUnsigned == 0, str: value.String(), } - if c := vspec.Comment; f.lineComment && c != nil && len(c.List) == 1 { - v.name = strings.TrimSpace(c.Text()) - } - v.name = strings.TrimPrefix(v.name, f.trimPrefix) f.values = append(f.values, v) } } @@ -503,9 +534,7 @@ func (g *Generator) declareIndexAndNameVars(runs [][]Value, typeName string) { var indexes, names []string for i, run := range runs { index, name := g.createIndexAndNameDecl(run, typeName, fmt.Sprintf("_%d", i)) - if len(run) != 1 { - indexes = append(indexes, index) - } + indexes = append(indexes, index) names = append(names, name) } g.Printf("const (\n") @@ -513,14 +542,11 @@ func (g *Generator) declareIndexAndNameVars(runs [][]Value, typeName string) { g.Printf("\t%s\n", name) } g.Printf(")\n\n") - - if len(indexes) > 0 { - g.Printf("var (") - for _, index := range indexes { - g.Printf("\t%s\n", index) - } - g.Printf(")\n\n") + g.Printf("var (") + for _, index := range indexes { + g.Printf("\t%s\n", index) } + g.Printf(")\n\n") } // declareIndexAndNameVar is the single-run version of declareIndexAndNameVars @@ -586,7 +612,7 @@ func (g *Generator) buildOneRun(runs [][]Value, typeName string) { // [3]: less than zero check (for signed types) const stringOneRun = `func (i %[1]s) String() string { if %[3]si >= %[1]s(len(_%[1]sIndex)-1) { - return "%[1]s(" + strconv.FormatInt(int64(i), 10) + ")" + return fmt.Sprintf("%[1]s(%%d)", i) } return _%[1]sName[_%[1]sIndex[i]:_%[1]sIndex[i+1]] } @@ -602,7 +628,7 @@ const stringOneRun = `func (i %[1]s) String() string { const stringOneRunWithOffset = `func (i %[1]s) String() string { i -= %[2]s if %[4]si >= %[1]s(len(_%[1]sIndex)-1) { - return "%[1]s(" + strconv.FormatInt(int64(i + %[2]s), 10) + ")" + return fmt.Sprintf("%[1]s(%%d)", i + %[2]s) } return _%[1]sName[_%[1]sIndex[i] : _%[1]sIndex[i+1]] } @@ -629,7 +655,7 @@ func (g *Generator) buildMultipleRuns(runs [][]Value, typeName string) { typeName, i, typeName, i, typeName, i) } g.Printf("\tdefault:\n") - g.Printf("\t\treturn \"%s(\" + strconv.FormatInt(int64(i), 10) + \")\"\n", typeName) + g.Printf("\t\treturn fmt.Sprintf(\"%s(%%d)\", i)\n", typeName) g.Printf("\t}\n") g.Printf("}\n") } @@ -656,6 +682,6 @@ const stringMap = `func (i %[1]s) String() string { if str, ok := _%[1]sMap[i]; ok { return str } - return "%[1]s(" + strconv.FormatInt(int64(i), 10) + ")" + return fmt.Sprintf("%[1]s(%%d)", i) } `