微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用“grpc-web_out”命令保留原始消息字段的“camelCase”?任何其他可用的选项?

如何解决如何使用“grpc-web_out”命令保留原始消息字段的“camelCase”?任何其他可用的选项?

我通过使用“grpc-web_out”命令编译 proto 文件生成 JavaScript 文件。 这些 myptoto_pb.js 文件包含 flatcase 中的所有 proto 消息字段(不区分大小写)。我在这里错过了任何其他选项吗? 如何生成带有区分大小写的 camelCase 字段的 js 文件? (消息字段应保持原样)

我使用以下命令来生成这些文件

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

这是示例原型文件

//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;
}

生成的 JavaScript 代码

//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;
};

这里,proto中的userId在js代码中转化为userid。其他字段(userEmail、companyDepartmentId)也是如此。对于所有字段,生成的 JavaScript 代码应具有类似的 camelCase 格式。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。