Skip to content

Commit

Permalink
feat: Move lower-cased names to a separate map to improve case support
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Oct 11, 2024
1 parent bcbe617 commit b25a89e
Show file tree
Hide file tree
Showing 18 changed files with 246 additions and 172 deletions.
25 changes: 24 additions & 1 deletion enumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func %[1]sString(s string) (%[1]s, error) {
return val, nil
}
if val, ok := _%[1]sNameToValueMap[strings.ToLower(s)]; ok {
if val, ok := _%[1]sLowerNameToValueMap[strings.ToLower(s)]; ok {
return val, nil
}
return 0, fmt.Errorf("%%s does not belong to %[1]s values", s)
Expand Down Expand Up @@ -84,6 +84,7 @@ func (g *Generator) buildBasicExtras(runs [][]Value, typeName string, runsThresh

// Print the map between name and value
g.printValueMap(runs, typeName, runsThreshold)
g.printLowerValueMap(runs, typeName, runsThreshold)

// Print the slice of names
g.printNamesSlice(runs, typeName, runsThreshold)
Expand Down Expand Up @@ -115,12 +116,34 @@ func (g *Generator) printValueMap(runs [][]Value, typeName string, runsThreshold

for _, value := range values {
g.Printf("\t_%sName%s[%d:%d]: %s,\n", typeName, runID, n, n+len(value.name), value.originalName)
n += len(value.name)
}
}
g.Printf("}\n\n")
}

func (g *Generator) printLowerValueMap(runs [][]Value, typeName string, runsThreshold int) {
thereAreRuns := len(runs) > 1 && len(runs) <= runsThreshold
g.Printf("\nvar _%sLowerNameToValueMap = map[string]%s{\n", typeName, typeName)

var n int
var runID string
for i, values := range runs {
if thereAreRuns {
runID = "_" + fmt.Sprintf("%d", i)
n = 0
} else {
runID = ""
}

for _, value := range values {
g.Printf("\t_%sLowerName%s[%d:%d]: %s,\n", typeName, runID, n, n+len(value.name), value.originalName)
n += len(value.name)
}
}
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)
Expand Down
19 changes: 11 additions & 8 deletions testdata/day.golden
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,22 @@ func _DayNoOp() {
var _DayValues = []Day{Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday}

var _DayNameToValueMap = map[string]Day{
_DayName[0:6]: Monday,
_DayName[0:6]: Monday,
_DayName[6:13]: Tuesday,
_DayName[13:22]: Wednesday,
_DayName[22:30]: Thursday,
_DayName[30:36]: Friday,
_DayName[36:44]: Saturday,
_DayName[44:50]: Sunday,
}

var _DayLowerNameToValueMap = map[string]Day{
_DayLowerName[0:6]: Monday,
_DayName[6:13]: Tuesday,
_DayLowerName[6:13]: Tuesday,
_DayName[13:22]: Wednesday,
_DayLowerName[13:22]: Wednesday,
_DayName[22:30]: Thursday,
_DayLowerName[22:30]: Thursday,
_DayName[30:36]: Friday,
_DayLowerName[30:36]: Friday,
_DayName[36:44]: Saturday,
_DayLowerName[36:44]: Saturday,
_DayName[44:50]: Sunday,
_DayLowerName[44:50]: Sunday,
}

Expand All @@ -65,7 +68,7 @@ func DayString(s string) (Day, error) {
return val, nil
}

if val, ok := _DayNameToValueMap[strings.ToLower(s)]; ok {
if val, ok := _DayLowerNameToValueMap[strings.ToLower(s)]; ok {
return val, nil
}
return 0, fmt.Errorf("%s does not belong to Day values", s)
Expand Down
19 changes: 11 additions & 8 deletions testdata/dayTrimAndPrefix.golden
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,22 @@ func _DayNoOp() {
var _DayValues = []Day{DayMonday, DayTuesday, DayWednesday, DayThursday, DayFriday, DaySaturday, DaySunday}

var _DayNameToValueMap = map[string]Day{
_DayName[0:11]: DayMonday,
_DayName[0:11]: DayMonday,
_DayName[11:23]: DayTuesday,
_DayName[23:37]: DayWednesday,
_DayName[37:50]: DayThursday,
_DayName[50:61]: DayFriday,
_DayName[61:74]: DaySaturday,
_DayName[74:85]: DaySunday,
}

var _DayLowerNameToValueMap = map[string]Day{
_DayLowerName[0:11]: DayMonday,
_DayName[11:23]: DayTuesday,
_DayLowerName[11:23]: DayTuesday,
_DayName[23:37]: DayWednesday,
_DayLowerName[23:37]: DayWednesday,
_DayName[37:50]: DayThursday,
_DayLowerName[37:50]: DayThursday,
_DayName[50:61]: DayFriday,
_DayLowerName[50:61]: DayFriday,
_DayName[61:74]: DaySaturday,
_DayLowerName[61:74]: DaySaturday,
_DayName[74:85]: DaySunday,
_DayLowerName[74:85]: DaySunday,
}

Expand All @@ -61,7 +64,7 @@ func DayString(s string) (Day, error) {
return val, nil
}

if val, ok := _DayNameToValueMap[strings.ToLower(s)]; ok {
if val, ok := _DayLowerNameToValueMap[strings.ToLower(s)]; ok {
return val, nil
}
return 0, fmt.Errorf("%s does not belong to Day values", s)
Expand Down
19 changes: 11 additions & 8 deletions testdata/dayWithLinecomment.golden
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,22 @@ func _DayNoOp() {
var _DayValues = []Day{Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday}

var _DayNameToValueMap = map[string]Day{
_DayName[0:5]: Monday,
_DayName[0:5]: Monday,
_DayName[5:12]: Tuesday,
_DayName[12:21]: Wednesday,
_DayName[21:29]: Thursday,
_DayName[29:36]: Friday,
_DayName[36:44]: Saturday,
_DayName[44:50]: Sunday,
}

var _DayLowerNameToValueMap = map[string]Day{
_DayLowerName[0:5]: Monday,
_DayName[5:12]: Tuesday,
_DayLowerName[5:12]: Tuesday,
_DayName[12:21]: Wednesday,
_DayLowerName[12:21]: Wednesday,
_DayName[21:29]: Thursday,
_DayLowerName[21:29]: Thursday,
_DayName[29:36]: Friday,
_DayLowerName[29:36]: Friday,
_DayName[36:44]: Saturday,
_DayLowerName[36:44]: Saturday,
_DayName[44:50]: Sunday,
_DayLowerName[44:50]: Sunday,
}

Expand All @@ -61,7 +64,7 @@ func DayString(s string) (Day, error) {
return val, nil
}

if val, ok := _DayNameToValueMap[strings.ToLower(s)]; ok {
if val, ok := _DayLowerNameToValueMap[strings.ToLower(s)]; ok {
return val, nil
}
return 0, fmt.Errorf("%s does not belong to Day values", s)
Expand Down
19 changes: 11 additions & 8 deletions testdata/dayWithPrefix.golden
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,22 @@ func _DayNoOp() {
var _DayValues = []Day{Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday}

var _DayNameToValueMap = map[string]Day{
_DayName[0:9]: Monday,
_DayName[0:9]: Monday,
_DayName[9:19]: Tuesday,
_DayName[19:31]: Wednesday,
_DayName[31:42]: Thursday,
_DayName[42:51]: Friday,
_DayName[51:62]: Saturday,
_DayName[62:71]: Sunday,
}

var _DayLowerNameToValueMap = map[string]Day{
_DayLowerName[0:9]: Monday,
_DayName[9:19]: Tuesday,
_DayLowerName[9:19]: Tuesday,
_DayName[19:31]: Wednesday,
_DayLowerName[19:31]: Wednesday,
_DayName[31:42]: Thursday,
_DayLowerName[31:42]: Thursday,
_DayName[42:51]: Friday,
_DayLowerName[42:51]: Friday,
_DayName[51:62]: Saturday,
_DayLowerName[51:62]: Saturday,
_DayName[62:71]: Sunday,
_DayLowerName[62:71]: Sunday,
}

Expand All @@ -61,7 +64,7 @@ func DayString(s string) (Day, error) {
return val, nil
}

if val, ok := _DayNameToValueMap[strings.ToLower(s)]; ok {
if val, ok := _DayLowerNameToValueMap[strings.ToLower(s)]; ok {
return val, nil
}
return 0, fmt.Errorf("%s does not belong to Day values", s)
Expand Down
21 changes: 12 additions & 9 deletions testdata/gap.golden
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,24 @@ func _GapNoOp() {
var _GapValues = []Gap{Two, Three, Five, Six, Seven, Eight, Nine, Eleven}

var _GapNameToValueMap = map[string]Gap{
_GapName_0[0:3]: Two,
_GapName_0[0:3]: Two,
_GapName_0[3:8]: Three,
_GapName_1[0:4]: Five,
_GapName_1[4:7]: Six,
_GapName_1[7:12]: Seven,
_GapName_1[12:17]: Eight,
_GapName_1[17:21]: Nine,
_GapName_2[0:6]: Eleven,
}

var _GapLowerNameToValueMap = map[string]Gap{
_GapLowerName_0[0:3]: Two,
_GapName_0[3:8]: Three,
_GapLowerName_0[3:8]: Three,
_GapName_1[0:4]: Five,
_GapLowerName_1[0:4]: Five,
_GapName_1[4:7]: Six,
_GapLowerName_1[4:7]: Six,
_GapName_1[7:12]: Seven,
_GapLowerName_1[7:12]: Seven,
_GapName_1[12:17]: Eight,
_GapLowerName_1[12:17]: Eight,
_GapName_1[17:21]: Nine,
_GapLowerName_1[17:21]: Nine,
_GapName_2[0:6]: Eleven,
_GapLowerName_2[0:6]: Eleven,
}

Expand All @@ -86,7 +89,7 @@ func GapString(s string) (Gap, error) {
return val, nil
}

if val, ok := _GapNameToValueMap[strings.ToLower(s)]; ok {
if val, ok := _GapLowerNameToValueMap[strings.ToLower(s)]; ok {
return val, nil
}
return 0, fmt.Errorf("%s does not belong to Gap values", s)
Expand Down
15 changes: 9 additions & 6 deletions testdata/num.golden
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,18 @@ func _NumNoOp() {
var _NumValues = []Num{m_2, m_1, m0, m1, m2}

var _NumNameToValueMap = map[string]Num{
_NumName[0:3]: m_2,
_NumName[0:3]: m_2,
_NumName[3:6]: m_1,
_NumName[6:8]: m0,
_NumName[8:10]: m1,
_NumName[10:12]: m2,
}

var _NumLowerNameToValueMap = map[string]Num{
_NumLowerName[0:3]: m_2,
_NumName[3:6]: m_1,
_NumLowerName[3:6]: m_1,
_NumName[6:8]: m0,
_NumLowerName[6:8]: m0,
_NumName[8:10]: m1,
_NumLowerName[8:10]: m1,
_NumName[10:12]: m2,
_NumLowerName[10:12]: m2,
}

Expand All @@ -58,7 +61,7 @@ func NumString(s string) (Num, error) {
return val, nil
}

if val, ok := _NumNameToValueMap[strings.ToLower(s)]; ok {
if val, ok := _NumLowerNameToValueMap[strings.ToLower(s)]; ok {
return val, nil
}
return 0, fmt.Errorf("%s does not belong to Num values", s)
Expand Down
11 changes: 7 additions & 4 deletions testdata/offset.golden
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@ func _NumberNoOp() {
var _NumberValues = []Number{One, Two, Three}

var _NumberNameToValueMap = map[string]Number{
_NumberName[0:3]: One,
_NumberName[0:3]: One,
_NumberName[3:6]: Two,
_NumberName[6:11]: Three,
}

var _NumberLowerNameToValueMap = map[string]Number{
_NumberLowerName[0:3]: One,
_NumberName[3:6]: Two,
_NumberLowerName[3:6]: Two,
_NumberName[6:11]: Three,
_NumberLowerName[6:11]: Three,
}

Expand All @@ -50,7 +53,7 @@ func NumberString(s string) (Number, error) {
return val, nil
}

if val, ok := _NumberNameToValueMap[strings.ToLower(s)]; ok {
if val, ok := _NumberLowerNameToValueMap[strings.ToLower(s)]; ok {
return val, nil
}
return 0, fmt.Errorf("%s does not belong to Number values", s)
Expand Down
31 changes: 17 additions & 14 deletions testdata/prime.golden
Original file line number Diff line number Diff line change
Expand Up @@ -51,31 +51,34 @@ func _PrimeNoOp() {
var _PrimeValues = []Prime{p2, p3, p5, p7, p11, p13, p17, p19, p23, p29, p37, p41, p43}

var _PrimeNameToValueMap = map[string]Prime{
_PrimeName[0:2]: p2,
_PrimeName[0:2]: p2,
_PrimeName[2:4]: p3,
_PrimeName[4:6]: p5,
_PrimeName[6:8]: p7,
_PrimeName[8:11]: p11,
_PrimeName[11:14]: p13,
_PrimeName[14:17]: p17,
_PrimeName[17:20]: p19,
_PrimeName[20:23]: p23,
_PrimeName[23:26]: p29,
_PrimeName[26:29]: p37,
_PrimeName[29:32]: p41,
_PrimeName[32:35]: p43,
}

var _PrimeLowerNameToValueMap = map[string]Prime{
_PrimeLowerName[0:2]: p2,
_PrimeName[2:4]: p3,
_PrimeLowerName[2:4]: p3,
_PrimeName[4:6]: p5,
_PrimeLowerName[4:6]: p5,
_PrimeName[6:8]: p7,
_PrimeLowerName[6:8]: p7,
_PrimeName[8:11]: p11,
_PrimeLowerName[8:11]: p11,
_PrimeName[11:14]: p13,
_PrimeLowerName[11:14]: p13,
_PrimeName[14:17]: p17,
_PrimeLowerName[14:17]: p17,
_PrimeName[17:20]: p19,
_PrimeLowerName[17:20]: p19,
_PrimeName[20:23]: p23,
_PrimeLowerName[20:23]: p23,
_PrimeName[23:26]: p29,
_PrimeLowerName[23:26]: p29,
_PrimeName[26:29]: p37,
_PrimeLowerName[26:29]: p37,
_PrimeName[29:32]: p41,
_PrimeLowerName[29:32]: p41,
_PrimeName[32:35]: p43,
_PrimeLowerName[32:35]: p43,
}

Expand All @@ -102,7 +105,7 @@ func PrimeString(s string) (Prime, error) {
return val, nil
}

if val, ok := _PrimeNameToValueMap[strings.ToLower(s)]; ok {
if val, ok := _PrimeLowerNameToValueMap[strings.ToLower(s)]; ok {
return val, nil
}
return 0, fmt.Errorf("%s does not belong to Prime values", s)
Expand Down
Loading

0 comments on commit b25a89e

Please sign in to comment.