-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Best way to get string representation of enum value? #349
Comments
You can get the runtime enum, which is a plain object, just like you'd get message classes: var SourceType = builder.build("SourceType");
// { TestSource: 0 } Afterwards, you can use that to look up the enum name from a value manually, or simply use a reflection helper: ...
var sourceTypeName = ProtoBuf.Reflect.Enum.getName(SourceType, foo.sourceType);
// "TestSource" Of course the runtime enum is also available within the global namespace: message MyMessage {
enum SourceType {
TestSource = 0;
}
} ...
var root = builder.build();
var SourceType = root.MyMessage.SourceType;
... |
@dcodeIO: I tried this, but for nested messages I see the effect that once I use the object defined as above, it falls back to the "integer behavior", even if I set the enum name manually. I also asked a StackOverflow question. I'm working with the mesos.proto which is rather large. |
It calls @terranmoccasin: You should find this works: var foo = proto.decode(bytes);
// foo.sourceType => 0
foo = proto.decode(bytes).toRaw();
// foo.sourceType => "TestSource" |
Closing this for now. Feel free to send a pull request if this is still a requirement. |
I was able to convert then using decode options as following:
Complete example using Awesome.proto example
|
After decoding the byte buffer into a proto, my enum values are being returned (correctly) as integers. I'd like to convert them into their name if possible.
For example, I have a sourceType enum:
What's the best way to convert this integer into the enum value of SourceType?
The text was updated successfully, but these errors were encountered: