Description
Is your feature request related to a problem? Please describe.
It's trivial to go from an enum's integer value to its string representation (name). You can just do enum.String()
, which under the hood uses proto.EnumName
.
However, it's not possible to go from the name to the integer value with a simple method call.
Describe the solution you'd like
An added method for all enums, like UnmarshalText(text []byte) error
. A simpler alternative might be something like what flag.Value
has, Set(string) error
.
Describe alternatives you've considered
If one has access to the containing struct type, the struct field tag will contain protobuf:"enum=foo.Bar"`
, which can be fed to proto.EnumValueMap
to obtain the relevant map[string]int32
. However, this has a few major disadvantages:
- The field tag is attached to the containing struct type, so it's unreachable if all you have is an enum value or type
- The use of
proto.EnumValueMap
forces a module to require the protobuf module, whenencoding.TextUnmarshaler
would suffice - Even if the two issues above were fixed, it's still unnecessarily complex to go from
string
toint32
, given how trivial it is to go the opposite way.
Additional context
I realise the answer for 99% of users is to simply use your JSON decoder. But I'm not decoding JSON here :)