diff --git a/generate/gen.go b/generate/gen.go index e1127d38..0974b8f2 100644 --- a/generate/gen.go +++ b/generate/gen.go @@ -768,6 +768,52 @@ func (e Enum) WriteStringFunc(l *LineWriter) { l.Write("}") } +func (e Enum) WriteStringsFunc(l *LineWriter) { + l.Write("func %sStrings() []string {", e.Name) + l.Write("return []string{") + for _, v := range e.Values { + l.Write(`"%s",`, v.Word) + } + l.Write("}") + l.Write("}") +} + +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("// trimming spaces, and lowercasing.") + l.Write("func Parse%[1]s(s string) (%[1]s, error) {", e.Name) + l.Write("switch strnorm(s) {") + for _, v := range e.Values { + l.Write(`case "%s":`, strnorm(v.Word)) + l.Write("return %d, nil", v.Value) + } + l.Write("default:") + l.Write(`return 0, fmt.Errorf("%s: unable to parse %%q", s)`, e.Name) + l.Write("}") + l.Write("}") +} + +func strnorm(s string) string { + s = strings.ReplaceAll(s, ".", "") + s = strings.ReplaceAll(s, "_", "") + s = strings.TrimSpace(s) + s = strings.ToLower(s) + return s +} + +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.TrimSpace(s)`) + l.Write(`s = strings.ToLower(s)`) + l.Write(`return s`) + l.Write(`}`) +} + func (e Enum) WriteConsts(l *LineWriter) { l.Write("const (") if !e.HasZero { diff --git a/generate/main.go b/generate/main.go index d0cadc02..6b4bdcd2 100644 --- a/generate/main.go +++ b/generate/main.go @@ -404,6 +404,8 @@ func main() { l.Write("package kmsg") l.Write("import (") l.Write(`"context"`) + l.Write(`"fmt"`) + l.Write(`"strings"`) l.Write(`"reflect"`) l.Write("") l.Write(`"github.com/twmb/franz-go/pkg/kmsg/internal/kbin"`) @@ -507,8 +509,12 @@ func main() { for _, e := range newEnums { e.WriteDefn(l) e.WriteStringFunc(l) + e.WriteStringsFunc(l) + e.WriteParseFunc(l) e.WriteConsts(l) } + writeStrnorm(l) + fmt.Println(l.buf.String()) } diff --git a/pkg/kmsg/generated.go b/pkg/kmsg/generated.go index 2199d2aa..ee7a622f 100644 --- a/pkg/kmsg/generated.go +++ b/pkg/kmsg/generated.go @@ -2,7 +2,9 @@ package kmsg import ( "context" + "fmt" "reflect" + "strings" "github.com/twmb/franz-go/pkg/kmsg/internal/kbin" ) @@ -39135,6 +39137,32 @@ func (v ConfigResourceType) String() string { } } +func ConfigResourceTypeStrings() []string { + return []string{ + "TOPIC", + "BROKER", + "BROKER_LOGGER", + } +} + +// ParseConfigResourceType normalizes the input s and returns +// the value represented by the string. +// +// Normalizing works by stripping all dots and underscores, +// trimming spaces, and lowercasing. +func ParseConfigResourceType(s string) (ConfigResourceType, error) { + switch strnorm(s) { + case "topic": + return 2, nil + case "broker": + return 4, nil + case "brokerlogger": + return 8, nil + default: + return 0, fmt.Errorf("ConfigResourceType: unable to parse %q", s) + } +} + const ( ConfigResourceTypeUnknown ConfigResourceType = 0 ConfigResourceTypeTopic ConfigResourceType = 2 @@ -39186,6 +39214,41 @@ func (v ConfigSource) String() string { } } +func ConfigSourceStrings() []string { + return []string{ + "DYNAMIC_TOPIC_CONFIG", + "DYNAMIC_BROKER_CONFIG", + "DYNAMIC_DEFAULT_BROKER_CONFIG", + "STATIC_BROKER_CONFIG", + "DEFAULT_CONFIG", + "DYNAMIC_BROKER_LOGGER_CONFIG", + } +} + +// ParseConfigSource normalizes the input s and returns +// the value represented by the string. +// +// Normalizing works by stripping all dots and underscores, +// trimming spaces, and lowercasing. +func ParseConfigSource(s string) (ConfigSource, error) { + switch strnorm(s) { + case "dynamictopicconfig": + return 1, nil + case "dynamicbrokerconfig": + return 2, nil + case "dynamicdefaultbrokerconfig": + return 3, nil + case "staticbrokerconfig": + return 4, nil + case "defaultconfig": + return 5, nil + case "dynamicbrokerloggerconfig": + return 6, nil + default: + return 0, fmt.Errorf("ConfigSource: unable to parse %q", s) + } +} + const ( ConfigSourceUnknown ConfigSource = 0 ConfigSourceDynamicTopicConfig ConfigSource = 1 @@ -39245,6 +39308,50 @@ func (v ConfigType) String() string { } } +func ConfigTypeStrings() []string { + return []string{ + "BOOLEAN", + "STRING", + "INT", + "SHORT", + "LONG", + "DOUBLE", + "LIST", + "CLASS", + "PASSWORD", + } +} + +// ParseConfigType normalizes the input s and returns +// the value represented by the string. +// +// Normalizing works by stripping all dots and underscores, +// trimming spaces, and lowercasing. +func ParseConfigType(s string) (ConfigType, error) { + switch strnorm(s) { + case "boolean": + return 1, nil + case "string": + return 2, nil + case "int": + return 3, nil + case "short": + return 4, nil + case "long": + return 5, nil + case "double": + return 6, nil + case "list": + return 7, nil + case "class": + return 8, nil + case "password": + return 9, nil + default: + return 0, fmt.Errorf("ConfigType: unable to parse %q", s) + } +} + const ( ConfigTypeUnknown ConfigType = 0 ConfigTypeBoolean ConfigType = 1 @@ -39287,6 +39394,35 @@ func (v IncrementalAlterConfigOp) String() string { } } +func IncrementalAlterConfigOpStrings() []string { + return []string{ + "SET", + "DELETE", + "APPEND", + "SUBTRACT", + } +} + +// ParseIncrementalAlterConfigOp normalizes the input s and returns +// the value represented by the string. +// +// Normalizing works by stripping all dots and underscores, +// trimming spaces, and lowercasing. +func ParseIncrementalAlterConfigOp(s string) (IncrementalAlterConfigOp, error) { + switch strnorm(s) { + case "set": + return 0, nil + case "delete": + return 1, nil + case "append": + return 2, nil + case "subtract": + return 3, nil + default: + return 0, fmt.Errorf("IncrementalAlterConfigOp: unable to parse %q", s) + } +} + const ( IncrementalAlterConfigOpSet IncrementalAlterConfigOp = 0 IncrementalAlterConfigOpDelete IncrementalAlterConfigOp = 1 @@ -39331,6 +39467,41 @@ func (v ACLResourceType) String() string { } } +func ACLResourceTypeStrings() []string { + return []string{ + "ANY", + "TOPIC", + "GROUP", + "CLUSTER", + "TRANSACTIONAL_ID", + "DELEGATION_TOKEN", + } +} + +// ParseACLResourceType normalizes the input s and returns +// the value represented by the string. +// +// Normalizing works by stripping all dots and underscores, +// trimming spaces, and lowercasing. +func ParseACLResourceType(s string) (ACLResourceType, error) { + switch strnorm(s) { + case "any": + return 1, nil + case "topic": + return 2, nil + case "group": + return 3, nil + case "cluster": + return 4, nil + case "transactionalid": + return 5, nil + case "delegationtoken": + return 6, nil + default: + return 0, fmt.Errorf("ACLResourceType: unable to parse %q", s) + } +} + const ( ACLResourceTypeUnknown ACLResourceType = 0 ACLResourceTypeAny ACLResourceType = 1 @@ -39376,6 +39547,35 @@ func (v ACLResourcePatternType) String() string { } } +func ACLResourcePatternTypeStrings() []string { + return []string{ + "ANY", + "MATCH", + "LITERAL", + "PREFIXED", + } +} + +// ParseACLResourcePatternType normalizes the input s and returns +// the value represented by the string. +// +// Normalizing works by stripping all dots and underscores, +// trimming spaces, and lowercasing. +func ParseACLResourcePatternType(s string) (ACLResourcePatternType, error) { + switch strnorm(s) { + case "any": + return 1, nil + case "match": + return 2, nil + case "literal": + return 3, nil + case "prefixed": + return 4, nil + default: + return 0, fmt.Errorf("ACLResourcePatternType: unable to parse %q", s) + } +} + const ( ACLResourcePatternTypeUnknown ACLResourcePatternType = 0 ACLResourcePatternTypeAny ACLResourcePatternType = 1 @@ -39412,6 +39612,32 @@ func (v ACLPermissionType) String() string { } } +func ACLPermissionTypeStrings() []string { + return []string{ + "ANY", + "DENY", + "ALLOW", + } +} + +// ParseACLPermissionType normalizes the input s and returns +// the value represented by the string. +// +// Normalizing works by stripping all dots and underscores, +// trimming spaces, and lowercasing. +func ParseACLPermissionType(s string) (ACLPermissionType, error) { + switch strnorm(s) { + case "any": + return 1, nil + case "deny": + return 2, nil + case "allow": + return 3, nil + default: + return 0, fmt.Errorf("ACLPermissionType: unable to parse %q", s) + } +} + const ( ACLPermissionTypeUnknown ACLPermissionType = 0 ACLPermissionTypeAny ACLPermissionType = 1 @@ -39482,6 +39708,59 @@ func (v ACLOperation) String() string { } } +func ACLOperationStrings() []string { + return []string{ + "ANY", + "ALL", + "READ", + "WRITE", + "CREATE", + "DELETE", + "ALTER", + "DESCRIBE", + "CLUSTER_ACTION", + "DESCRIBE_CONFIGS", + "ALTER_CONFIGS", + "IDEMPOTENT_WRITE", + } +} + +// ParseACLOperation normalizes the input s and returns +// the value represented by the string. +// +// Normalizing works by stripping all dots and underscores, +// trimming spaces, and lowercasing. +func ParseACLOperation(s string) (ACLOperation, error) { + switch strnorm(s) { + case "any": + return 1, nil + case "all": + return 2, nil + case "read": + return 3, nil + case "write": + return 4, nil + case "create": + return 5, nil + case "delete": + return 6, nil + case "alter": + return 7, nil + case "describe": + return 8, nil + case "clusteraction": + return 9, nil + case "describeconfigs": + return 10, nil + case "alterconfigs": + return 11, nil + case "idempotentwrite": + return 12, nil + default: + return 0, fmt.Errorf("ACLOperation: unable to parse %q", s) + } +} + const ( ACLOperationUnknown ACLOperation = 0 ACLOperationAny ACLOperation = 1 @@ -39543,6 +39822,47 @@ func (v TransactionState) String() string { } } +func TransactionStateStrings() []string { + return []string{ + "Empty", + "Ongoing", + "PrepareCommit", + "PrepareAbort", + "CompleteCommit", + "CompleteAbort", + "Dead", + "PrepareEpochFence", + } +} + +// ParseTransactionState normalizes the input s and returns +// the value represented by the string. +// +// Normalizing works by stripping all dots and underscores, +// trimming spaces, and lowercasing. +func ParseTransactionState(s string) (TransactionState, error) { + switch strnorm(s) { + case "empty": + return 0, nil + case "ongoing": + return 1, nil + case "preparecommit": + return 2, nil + case "prepareabort": + return 3, nil + case "completecommit": + return 4, nil + case "completeabort": + return 5, nil + case "dead": + return 6, nil + case "prepareepochfence": + return 7, nil + default: + return 0, fmt.Errorf("TransactionState: unable to parse %q", s) + } +} + const ( TransactionStateEmpty TransactionState = 0 TransactionStateOngoing TransactionState = 1 @@ -39581,9 +39901,46 @@ func (v ControlRecordKeyType) String() string { } } +func ControlRecordKeyTypeStrings() []string { + return []string{ + "ABORT", + "COMMIT", + "QUORUM_REASSIGNMENT", + "LEADER_CHANGE", + } +} + +// ParseControlRecordKeyType normalizes the input s and returns +// the value represented by the string. +// +// Normalizing works by stripping all dots and underscores, +// trimming spaces, and lowercasing. +func ParseControlRecordKeyType(s string) (ControlRecordKeyType, error) { + switch strnorm(s) { + case "abort": + return 0, nil + case "commit": + return 1, nil + case "quorumreassignment": + return 2, nil + case "leaderchange": + return 3, nil + default: + return 0, fmt.Errorf("ControlRecordKeyType: unable to parse %q", s) + } +} + const ( ControlRecordKeyTypeAbort ControlRecordKeyType = 0 ControlRecordKeyTypeCommit ControlRecordKeyType = 1 ControlRecordKeyTypeQuorumReassignment ControlRecordKeyType = 2 ControlRecordKeyTypeLeaderChange ControlRecordKeyType = 3 ) + +func strnorm(s string) string { + s = strings.ReplaceAll(s, ".", "") + s = strings.ReplaceAll(s, "_", "") + s = strings.TrimSpace(s) + s = strings.ToLower(s) + return s +}