Skip to content
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

Closed
terranmoccasin opened this issue Nov 6, 2015 · 5 comments
Closed

Best way to get string representation of enum value? #349

terranmoccasin opened this issue Nov 6, 2015 · 5 comments
Labels

Comments

@terranmoccasin
Copy link

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:

enum SourceType {
  TestSource = 0;
}
var foo = proto.decode(bytes);
// foo.sourceType => 0

What's the best way to convert this integer into the enum value of SourceType?

@dcodeIO
Copy link
Member

dcodeIO commented Nov 6, 2015

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;
...

@tobilg
Copy link

tobilg commented Jun 7, 2016

@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.

@michaelhogg
Copy link

ProtoBuf.Builder.Message.toRaw() seems to work.

It calls cloneRaw() which "converts enum values to their respective names".

@terranmoccasin: You should find this works:

var foo = proto.decode(bytes);
// foo.sourceType => 0

foo = proto.decode(bytes).toRaw();
// foo.sourceType => "TestSource"

@dcodeIO
Copy link
Member

dcodeIO commented Nov 28, 2016

Closing this for now.

protobuf.js 6.0.0

Feel free to send a pull request if this is still a requirement.

@dcodeIO dcodeIO closed this as completed Nov 28, 2016
@MaheshSasidharan
Copy link

MaheshSasidharan commented Feb 22, 2019

I was able to convert then using decode options as following:

var fooDecoded = proto.decode(bytes);
var foo = proto.toObject(fooDecoded, {
            enums: String,
            longs: String,
            arrays: true,
            objects: true
        });

Complete example using Awesome.proto example

var am = root.lookup("awesomepackage.AwesomeMessage");
var amessage = { awesomeField: "AwesomeString", fields: ["one","two"] };
var decoded = am.decode(am.encode(amessage).finish());
am.toObject(decoded, {
    enums: String,
    longs: String,
    arrays: true,
    objects: true
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants