1

Is there a way to reuse protobuf structs across multiple proto files?

in the client.proto file I have the following

message SingleOperation {
  string command = 1;
}

message ClientBatch {
  string unique_id = 1;
  repeated SingleOperation requests = 2;
  int64 sender = 3;
}

Then in network.proto file, I have the following

message MemPool {
  int32 sender = 1;
  string unique_id = 2;
  SingleOperation op = 3;
}
 

In the network.proto file, I need to reuse the SingleOperation message.

However, I do not see an option to do this.

Does protobuff support some sort of packaging to support struct reuse?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Pasindu Tennage
  • 1,480
  • 3
  • 14
  • 31
  • 1
    I think adding import "client.proto"; in network.proto file should work fine. – Zaid Afzal Jan 09 '23 at 21:11
  • 1
    Yes, as Zaid says, "import" is the way to go. Structure your .proto files much like you would .h files in C, or class libraries; you might (for example) have a "sharedmessages.proto" containing all the message definitions you use in multiple places. Import that into other files. – bazza Jan 14 '23 at 20:55

0 Answers0