3

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

ttrasn
  • 4,322
  • 4
  • 26
  • 43
  • 1
    I'm not sure it's possible, but this isn't a good idea. It's against [Go's naming convention](https://github.com/golang/go/wiki/CodeReviewComments). You could say it's "just generated code", but you generate the code so you can use it in "your" code. – icza Jul 13 '20 at 09:51

1 Answers1

2

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;
}
Nikko Khresna
  • 1,024
  • 8
  • 10
  • yes, actually I wanted to use camelCase in `proto` and result be `snake_case`, but with your answer I found the real solution;) – ttrasn Jul 13 '20 at 10:09
  • 2
    I found this solution creating language inconsistency among generated dialects: python code will use song_name; js and dart use songName. Very annoying – kakyo Sep 09 '22 at 09:52