2

I have generated JavaScript files by compiling proto files with "grpc-web_out" command. These myptoto_pb.js file contains all proto message fields in flatcase (case insensitive). Is there any additional option which I am missing out here? How do I generate js files, with case sensitive camelCase fields? (message fields should be retained as it is)

I have used below command to generate these files:

protoc.exe -I../grpc/proto/ --js_out=import_style=commonjs,binary:../src/grpchandlers/ ../proto/*.proto 
protoc.exe -I../grpc/proto/ --grpc-web_out=import_style=commonjs,mode=grpcwebtext:../src/grpchandlers/ ../grpc/proto/*.proto

Here is the sample proto file

//myproto.proto
syntax = "proto3";
option java_multiple_files = true;
option java_package = "com.grpc.messages";
option java_outer_classname = "CommonOuter";
option java_generic_services = true;
package remote;
import "base.proto";

message UserProfileInfo {
int64 userId = 1;
string userEmail = 2;
int64 companyDepartmentId = 3;
}

message UserProfileInfoArray {
    repeated UserProfileInfo value = 1;
}

and generated JavaScript code:

//myproto_pb.js
/**
 * Static version of the {@see toObject} method.
 * @param {boolean|undefined} includeInstance Deprecated. Whether to include
 *     the JSPB instance for transitional soy proto support:
 *     http://goto/soy-param-migration
 * @param {!proto.remote.UserProfileInfo} msg The msg instance to transform.
 * @return {!Object}
 * @suppress {unusedLocalVariables} f is only used for nested messages
 */
proto.remote.UserProfileInfo.toObject = function(includeInstance, msg) {
 var f, obj = {
    userid: jspb.Message.getFieldWithDefault(msg, 1, 0),
    useremail: jspb.Message.getFieldWithDefault(msg, 2, ""),
    companydepartmentid: jspb.Message.getFieldWithDefault(msg, 3, 0)
 };
 if (includeInstance) {
    obj.$jspbMessageInstance = msg;
 }
 return obj;
};

Here, userId in proto is converted to userid in js code. Same for other fields (userEmail, companyDepartmentId). Generated JavaScript code should have similar camelCase format for all fields.

K10
  • 21
  • 4
  • Please include an example field from your proto and the generated JavaScript. I've not generated to JavaScript (mostly Golang|Rust here) **but** as you (and I) expect, you should get camelCase. See: https://developers.google.com/protocol-buffers/docs/reference/javascript-generated#fields – DazWilkin Jun 15 '21 at 16:32
  • Hi @DazWilkin thanks for replying. I have updated question with sample fields. Also, I don't want to edit name of each generated js field because there are multiple proto files, and each contains many message structures, these are quite significant in number. Hence looking for better option to achieve this. – K10 Jun 16 '21 at 08:38
  • I'm able to repro your issue using the latest `protoc` too. Found this issue: https://github.com/protocolbuffers/protobuf/issues/8608 – DazWilkin Jun 16 '21 at 15:47
  • @DazWilkin Yeah looks like there is no fix available as of now. Still curious to explore, how we can overcome this. I'm sure this community is full of genius minds :) – K10 Jun 17 '21 at 07:04
  • An interim hack may be to shim the protobuf-generated messages into objects that you write that correctly represent the fields camelCased. Then, when the underlying issue is addressed, you can delete the shim. – DazWilkin Jun 17 '21 at 15:22
  • Yes, I am thinking of fixing this into the existing protoc source code, Possibly to begin with "map.js" file (protobuf-3.17.3\js\). This file contains "toObject" and "fromObject" methods. Will post updates here if it works. – K10 Jun 22 '21 at 05:42
  • protobuf-3.17.3\src\google\protobuf\descriptor.cc This file contains code related to field's formatting (camelCase, lowercase etc) – K10 Jun 22 '21 at 08:22

1 Answers1

1

You should name the field with snake_case.

Then it will be generated with camelCase.

reference: https://developers.google.com/protocol-buffers/docs/style#message_and_field_names

kyun
  • 9,710
  • 9
  • 31
  • 66