What's best practice for separating the declaration of messages used in services in (Go-specific) Protocol Buffers? The context is a large application with multiple gRPC services. Some messages are used in multiple services. The thought was to divide the definitions of message and services, like this (simplified a bit):
airline/pb/airline_messages.proto
:
syntax = "proto3";
option go_package = "github.com/example.com/example-repo/airline/pb";
message Airline {
string code = 1;
string name = 2;
string country = 3;
}
airline/pb/airline_services.proto
:
syntax = "proto3";
option go_package = "github.com/example.com/example-repo/airline/pb";
import "airline/pb/airline_messages.proto"
service AirlineService {
rpc GetAirline(string) returns (Airline) {}
rpc GetAirlines(GetAirlinesRequest) returns (repeated Airline) {}
}
message GetAirlinesRequest {
int max = 1;
string country = 2;
string pattern = 3;
string sortby = 4;
}
I'm calling protoc
like this:
protoc --go_out=. \
--go_opt=paths=source_relative \
--go-grpc_out=. \
--go-grpc_opt=paths=source_relative \
--proto_path=. \
--proto_path=../../ airline_services.proto
This doesn't work because Airline
isn't defined. Invoking it like this:
protoc ... airline_services.proto airline_messages.proto
generates an error message the Airline is multiply defined. Doing this:
protoc ... airline_messages.proto
protoc ... airline_services.proto
just overwrites the Go files and is equivalent to just compiling airline_services.proto
.
What's best practice for being able to reuse message definitions in multiple services?