Search code examples
gomicroservicesgrpcgrpc-goproto3

snake case style in protobuff generated by grpc proto3


Is it possible to change default model of proto3 from CamelCase to snake_case in grpc?

example :

file anyproto.proto

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

now the protoc -I. --go_out=plugins=grpc:. anyproto.proto command generate this model:

file : 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"`
}

I want to change style of requestStatus and requestMessage to request_status and request_message


Solution

  • is this what you are looking for?
    https://developers.google.com/protocol-buffers/docs/style#message_and_field_names
    Quoted from link:

    Use CamelCase (with an initial capital) for message names – for example, SongServerRequest. Use underscore_separated_names for field names (including oneof field and extension names) – for example, song_name.

    message SongServerRequest {
      required string song_name = 1;
    }