Protobuf.js: Issue regarding underscores.

Created on 7 Dec 2017  路  7Comments  路  Source: protobufjs/protobuf.js

protobuf.js version: 6.8.3 (? downloaded from npm yesterday)

When the variable in this case "int32 id_number = 1;" has an underscore, protobuf.js is not able to set it when used like this. Basically by changing the variable id_number to idnumber in both the proto and javascript file, everything works as expected. Is this some sort of expected behavior I'm not understanding, or is this a bug?

var staticRadarMessage = root.lookupType("radarpackage.MsgRadar");

var idMsg = staticRadarMessage.create( 
 {
    msgType:1,
    sendIdMessage: { id_number:55}
});

In this code, the proto field id_number will not be set.
If I were to console.log the object idMsg, it would result in this

MsgRadar { msgType: 1, sendIdMessage: { id_number: 55 } }

If I were to verify, serialize the data to a buffer and then decode that buffer, the result when console.log would be this

MsgRadar { msgType: 1, sendIdMessage: MsgSendId {} }

When making the necessary modifications

Basically by changing the variable id_number to idnumber in both the proto and javascript file, everything works as expected.

Those same console results from before result as one would expect

MsgRadar { msgType: 1, sendIdMessage: { idnumber: 55 } }

MsgRadar { msgType: 1, sendIdMessage: MsgSendId { idnumber: 55 } }

*Both versions "verify" correctly *

Relevant .proto definitions

syntax = "proto3";
package radarpackage;

message MsgRadar {
     MessageType msgType = 1;
     MsgSendId sendIdMessage = 2;
}

message MsgSendId {
    int32 id_number = 1;
}

Most helpful comment

Can you please document this choice in your README? I found it very confusing. There's no errors when I get it wrong, just silent failure to set/get fields.

I see the style guide for protobufs recommends using underscores for field names in the definitions, and gives the Java example in camel case. So I guess we are following that. But it would be nice to know this upfront about the library.

All 7 comments

By default, field names are converted to camel case notation. id_number becomes idNumber. This behavior can be overridden here by providing the option keepCase: true (see). Likewise, command line tools accept the --keep-case argument.

@dcodeIO how does one do that?

I tried like so, but this is not working

protobuf.load("radarcomm.proto",{keepCase: true},function(err, root) {

Just do this instead:

var root = new protobuf.Root();
root.load("radarcomm.proto", { keepCase: true }, function(err) {
  ...
});

Closing this issue for now as it hasn't received any replies recently. Feel free to reopen it if necessary!

Can you please document this choice in your README? I found it very confusing. There's no errors when I get it wrong, just silent failure to set/get fields.

I see the style guide for protobufs recommends using underscores for field names in the definitions, and gives the Java example in camel case. So I guess we are following that. But it would be nice to know this upfront about the library.

Just do this instead:

var root = new protobuf.Root();
root.load("radarcomm.proto", { keepCase: true }, function(err) {
  ...
});

it works!

Why we can't use just:

profobuf.load("radarcomm.proto", { keepCase: true }, function(err) {
  ...
});

Can anyone explain it?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

VitalyName picture VitalyName  路  3Comments

ghost picture ghost  路  3Comments

thedillonb picture thedillonb  路  4Comments

kostyay picture kostyay  路  3Comments

wesleytodd picture wesleytodd  路  4Comments