generate: add RequestForKey function
@@ -96,13 +96,16 @@ func main() {
l.Write("// Code generated by kgo/generate. DO NOT EDIT.\n")
l.Write("// MaxKey is the maximum key used for any messages in this package.")
- l.Write("// Note that this value may change as Kafka adds more messages.")
+ l.Write("// Note that this value will change as Kafka adds more messages.")
l.Write("const MaxKey = %d\n", maxKey)
+ var keys2structs []Struct
+
for _, s := range newStructs {
s.WriteDefn(l)
if s.TopLevel {
if s.ResponseKind != "" {
+ keys2structs = append(keys2structs, s)
s.WriteKeyFunc(l)
s.WriteMaxVersionFunc(l)
s.WriteMinVersionFunc(l)
@@ -120,5 +123,16 @@ func main() {
}
}
+ l.Write("// RequestForKey returns the request corresponding to the given request key")
+ l.Write("// or nil if the key is unknown.")
+ l.Write("func RequestForKey(key int16) Request {")
+ l.Write("switch key {")
+ l.Write("default: return nil")
+ for _, key2struct := range keys2structs {
+ l.Write("case %d: return new(%s)", key2struct.Key, key2struct.Name)
+ }
+ l.Write("}")
+ l.Write("}")
+
fmt.Println(l.buf.String())
}
