Skip to content

Commit

Permalink
enum parsing: strip dashes as well
Browse files Browse the repository at this point in the history
We may as well be generous with the separator stripping and eliminate
dashes as well.
  • Loading branch information
twmb committed Sep 15, 2021
1 parent 8216b7c commit c153b9a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
4 changes: 3 additions & 1 deletion generate/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ func (e Enum) WriteParseFunc(l *LineWriter) {
l.Write("// Parse%s normalizes the input s and returns", e.Name)
l.Write("// the value represented by the string.")
l.Write("//")
l.Write("// Normalizing works by stripping all dots and underscores,")
l.Write("// Normalizing works by stripping all dots, underscores, and dashes,")
l.Write("// trimming spaces, and lowercasing.")
l.Write("func Parse%[1]s(s string) (%[1]s, error) {", e.Name)
l.Write("switch strnorm(s) {")
Expand All @@ -799,6 +799,7 @@ func (e Enum) WriteParseFunc(l *LineWriter) {
func strnorm(s string) string {
s = strings.ReplaceAll(s, ".", "")
s = strings.ReplaceAll(s, "_", "")
s = strings.ReplaceAll(s, "-", "")
s = strings.TrimSpace(s)
s = strings.ToLower(s)
return s
Expand All @@ -808,6 +809,7 @@ func writeStrnorm(l *LineWriter) {
l.Write(`func strnorm(s string) string {`)
l.Write(`s = strings.ReplaceAll(s, ".", "")`)
l.Write(`s = strings.ReplaceAll(s, "_", "")`)
l.Write(`s = strings.ReplaceAll(s, "-", "")`)
l.Write(`s = strings.TrimSpace(s)`)
l.Write(`s = strings.ToLower(s)`)
l.Write(`return s`)
Expand Down
21 changes: 11 additions & 10 deletions pkg/kmsg/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c153b9a

Please sign in to comment.