-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Open
Milestone
Description
I'm noticing that text serialization (and to some degree JSON) is used to quickly initialize a lot of data in tests (and sometimes in not-test code at init
) with a pattern like:
const initData = `...` // text data
m := new(foopb.MyMessage)
if err := prototext.Unmarshal([]byte(initData), m); err != nil {
panic(err)
}
return m
With the upcoming introduction of generics, perhaps we should add a helper for this:
func MustUnmarshal[M proto.Message](s string) M {
var m M
m = m.ProtoReflect().New().Interface()
if err := Unmarshal([]byte(s), m); err != nil {
panic(err)
}
return m
}
Thus, usage of this would be something like:
m := prototext.MustUnmarshal[*foopb.MyMessage](initData)
Metadata
Metadata
Assignees
Labels
No labels