Skip to content

Commit 8216b7c

Browse files
committed
kmsg: add EnumStrings(), ParseEnum() functions
This should make it easier to list valid options for the string value of an enum, as well as easier to parse a string into an enum. The parsing is aggressive in what we accept by lowercasing and stripping all dots and underscores. I expect most input to just match based off lowercasing, but a stray _ shouldn't affect anything.
1 parent 2f676c0 commit 8216b7c

File tree

3 files changed

+409
-0
lines changed

3 files changed

+409
-0
lines changed

generate/gen.go

+46
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,52 @@ func (e Enum) WriteStringFunc(l *LineWriter) {
768768
l.Write("}")
769769
}
770770

771+
func (e Enum) WriteStringsFunc(l *LineWriter) {
772+
l.Write("func %sStrings() []string {", e.Name)
773+
l.Write("return []string{")
774+
for _, v := range e.Values {
775+
l.Write(`"%s",`, v.Word)
776+
}
777+
l.Write("}")
778+
l.Write("}")
779+
}
780+
781+
func (e Enum) WriteParseFunc(l *LineWriter) {
782+
l.Write("// Parse%s normalizes the input s and returns", e.Name)
783+
l.Write("// the value represented by the string.")
784+
l.Write("//")
785+
l.Write("// Normalizing works by stripping all dots and underscores,")
786+
l.Write("// trimming spaces, and lowercasing.")
787+
l.Write("func Parse%[1]s(s string) (%[1]s, error) {", e.Name)
788+
l.Write("switch strnorm(s) {")
789+
for _, v := range e.Values {
790+
l.Write(`case "%s":`, strnorm(v.Word))
791+
l.Write("return %d, nil", v.Value)
792+
}
793+
l.Write("default:")
794+
l.Write(`return 0, fmt.Errorf("%s: unable to parse %%q", s)`, e.Name)
795+
l.Write("}")
796+
l.Write("}")
797+
}
798+
799+
func strnorm(s string) string {
800+
s = strings.ReplaceAll(s, ".", "")
801+
s = strings.ReplaceAll(s, "_", "")
802+
s = strings.TrimSpace(s)
803+
s = strings.ToLower(s)
804+
return s
805+
}
806+
807+
func writeStrnorm(l *LineWriter) {
808+
l.Write(`func strnorm(s string) string {`)
809+
l.Write(`s = strings.ReplaceAll(s, ".", "")`)
810+
l.Write(`s = strings.ReplaceAll(s, "_", "")`)
811+
l.Write(`s = strings.TrimSpace(s)`)
812+
l.Write(`s = strings.ToLower(s)`)
813+
l.Write(`return s`)
814+
l.Write(`}`)
815+
}
816+
771817
func (e Enum) WriteConsts(l *LineWriter) {
772818
l.Write("const (")
773819
if !e.HasZero {

generate/main.go

+6
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,8 @@ func main() {
404404
l.Write("package kmsg")
405405
l.Write("import (")
406406
l.Write(`"context"`)
407+
l.Write(`"fmt"`)
408+
l.Write(`"strings"`)
407409
l.Write(`"reflect"`)
408410
l.Write("")
409411
l.Write(`"github.com/twmb/franz-go/pkg/kmsg/internal/kbin"`)
@@ -507,8 +509,12 @@ func main() {
507509
for _, e := range newEnums {
508510
e.WriteDefn(l)
509511
e.WriteStringFunc(l)
512+
e.WriteStringsFunc(l)
513+
e.WriteParseFunc(l)
510514
e.WriteConsts(l)
511515
}
512516

517+
writeStrnorm(l)
518+
513519
fmt.Println(l.buf.String())
514520
}

0 commit comments

Comments
 (0)