Open Mido-sys opened 1 year ago
This is working as intended:
During deserialization, unrecognized enum values will be preserved in the message, though how this is represented when the message is deserialized is language-dependent. In languages that support open enum types with values outside the range of specified symbols, such as C++ and Go, the unknown enum value is simply stored as its underlying integer representation.
https://developers.google.com/protocol-buffers/docs/proto3#enum
@puellanivis, Thanks for getting back to me. Why can't the enum getter handle this? It already does return the default if x == nil
?
So something like this
func (x *Product) GetStatus() Status {
if x != nil {
if _, ok := Status_value[int32(x.TransactionType)]; ok {
return x.TransactionType
}
}
return Product_UNKNOWN
}
It returns the default value if x == nil
because the default value of a message is a message wherein all its fields are their default values. Thus the value of (*Product)(nil).GetStatus()
would have to be the default value of the enum, which by proto3 is defined as the first entry, which must be = 0
. That this is Product_UNKNOWN
here, is merely a habit of convenience. But for a common case like enum MobileType { IOS = 0; ANDROID = 1; }
the default value would then be IOS
and not an undefined value.
What version of protobuf and what language are you using?
Protobuf
v3
protoc-gen-gov1.28.1
protocv3.15.8
Go1.18.1
What did you do? PROTO File
Golang Client
Golang Server
What did you expect to see? The
req.ProductStatus
should have a status ofpb.Product_UNKNOWN
, which is the default value when it can't match the value What did you see instead? I get100
when I do in the serverreq.GetProductStatus()
req.ProductStatus
orreq.GetProductStatus().String