-
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
Example for how to work with oneofs #523
Comments
After messing around a bit longer, I got to something that seems to be working, but I'm not sure if this is the proper approach: For a proto file "simple.proto":
const protobuf = require( "protobufjs" );
protobuf.load("simple.proto", function(err, root)
{
if (err) throw err;
// Lookup message type
var MyMessage = root.lookup("MyMessage");
// Create
var myMessage = MyMessage.create(
{
uid:1,
pid:2,
utime:3,
msg2: { value: true},
msg1: { value: 7}
});
// Encode
var bb = MyMessage.encode( myMessage ).finish();
// Decode
var decodedMessage = MyMessage.decode(bb);
var which = decodedMessage.payload;
}) Anything more to this? More idiomatic approach? |
Using var myMessage = MyMessage.create(
{
uid:1,
pid:2,
utime:3,
msg2: { value: true},
msg1: { value: 7}
}); When creating a message with oneofs, the last declared field value that is part of the oneof will cause the virtual oneof field to be set to this field's name (here: payload = "msg1"). Other field values part of the same oneof will be deleted (here: msg2, only msg1 is encoded). (see: Prototype constructor, setVirtual)
When decoding, the virtual oneof field should point to the field name of the last field of the oneof that was present on the wire. However, if multiple fields were present (which should be prevented usually), all of the present field values are set on the wire, but the virtual field will still just point to the last seen on the wire. (see getVirtual - this actually returns the first defined oneof field's name present on the wire, which is not to the spec and should be fixed).
When working with oneofs, like changing the field, you can set the virtual oneof field to the field name of the field you want to be the sole field to encode (see setVirtual above): myMessage.msg2 = ... ;
myMessage.payload = "msg2"; // deletes msg1 - use .setPayload("msg2") to support IE8 |
Great, thanks for the additional information and for the great work you've put into this! |
is there other better way for decodedMsg than the code below? |
I feel like this should be in the documentation as it's a pretty common case. |
Can someone possibly help me with the JSON integration of this? The object I'd like to encode looks like this:
My JSON:
I know that the JSON is faulty with regard to the bio field, as I am just unsure on how to properly format this. Any help is much appreciated, thanks a lot! |
I've been hunting around for some up to date documentation that explains the basics of how to work with oneof fields in protobuf.js v6, but haven't been able to find anything. If there are some examples, could you point me in the right direction, and if not, would you mind giving a brief example of creating, encoding, and decoding a message that has a oneof field? Thanks.
The text was updated successfully, but these errors were encountered: