【问题标题】:snake case style in protobuff generated by grpc proto3由 grpc proto3 生成的 protobuff 中的蛇形案例样式
【发布时间】:2023-03-11 09:22:01
【问题描述】:

是否可以在 grpc 中将 proto3 的默认模型从 CamelCase 更改为 snake_case?

示例:

文件anyproto.proto

...
message Request {
  bool RequestStatus = 1;
  string RequestMessage = 2
}
...

现在protoc -I. --go_out=plugins=grpc:. anyproto.proto 命令生成这个模型:

文件:anyproto.pb.go

type Request struct {
    state         protoimpl.MessageState
    sizeCache     protoimpl.SizeCache
    unknownFields protoimpl.UnknownFields

    RequestStatus  bool    `protobuf:"varint,1,opt,name=requestStatus,proto3" json:"requestStatus,omitempty"`
    RequestMessage string  `protobuf:"bytes,2,opt,name=requestMessage,proto3" json:"requestMessage,omitempty"`
}

我想将requestStatusrequestMessage 的样式更改为request_statusrequest_message

【问题讨论】:

  • 我不确定这是否可行,但这不是一个好主意。它反对Go's naming convention。您可以说它是“刚刚生成的代码”,但您生成代码以便可以在“您的”代码中使用它。

标签: go microservices grpc grpc-go proto3


【解决方案1】:

这是你要找的吗?
https://developers.google.com/protocol-buffers/docs/style#message_and_field_names
引用自链接:

使用 CamelCase(首字母大写)作为消息名称 - 例如 SongServerRequest。使用 underscore_separated_names 作为字段名称(包括 oneof 字段和扩展名)——例如,song_name。

message SongServerRequest {
  required string song_name = 1;
}

【讨论】:

  • 是的,实际上我想在proto 中使用camelCase,结果是snake_case,但有了你的回答,我找到了真正的解决方案;)
猜你喜欢
  • 1970-01-01
  • 2021-06-22
  • 1970-01-01
  • 2019-07-13
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-08
相关资源
最近更新 更多