You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm using an Enum to represent the list of HTTP verbs. When I serialize this, I'd like to see the tag, not the integer (other places in the system use the integer, but this is for documentation). Is there any way I can call .to_hash on an enum value and return the name rather than the tag/int?
Ie, Mason::HTTPVerb::PATCH.name is always cast to 3 when .to_hash is called, but I'd like this version of the representation to show :PATCH instead. Possible?
Hey, thanks for opening an issue! Currently, there isn't a way of doing this. When any enum field setter is called, protobuf will convert that value into an enum class. Whenever you attempt to encode something, the behavior is to call #to_i. Based on your example, though, you can remove the Mason::Edit protobuf model, and get what you want by building the hash directly.
Also, I'm not sure there's a strong use-case for adding this feature. I think if you're sending from a protobuf client to some protobuf server, then you won't need to worry about losing value of the enum type. If you're sending json from protobuf, then building a hash from scratch, or using some other tool like rabl might be your solution (my opinion).
I'm using an Enum to represent the list of HTTP verbs. When I serialize this, I'd like to see the tag, not the integer (other places in the system use the integer, but this is for documentation). Is there any way I can call
.to_hash
on an enum value and return the name rather than the tag/int?Ie,
Mason::HTTPVerb::PATCH.name
is always cast to3
when.to_hash
is called, but I'd like this version of the representation to show:PATCH
instead. Possible?Desired output:
Current output:
The text was updated successfully, but these errors were encountered: