I would like to define my messages in a nested way like this:
FooRequest
And use the Requests in my grpc service defintions like this:
service SessionManager {
rpc CreateSession (CreateSessionRequest) returns (CreateSessionResponse) {}
rpc DropSession (DropSessionRequest) returns (DropSessionResponse) {}
}
For this it would be nice to define the Request
proto only once and re-use it for all the Requests I want to create.
message Request {
string messageId = 1;
string origin = 2;
string correlationId = 3;
int32 sentAt = 4;
string type = 5;
int32 version = 6;
google.protobuf.Any metadata = 7;
google.protobuf.Any payload = 8;
}
Instead of doing this:
message CreateSessionRequest {
string messageId = 1;
string origin = 2;
string correlationId = 3;
int32 sentAt = 4;
string type = 5;
int32 version = 6;
google.protobuf.Any metadata = 7;
CreateSessionPayload payload = 8;
}
message DropSessionRequest {
string messageId = 1;
string origin = 2;
string correlationId = 3;
int32 sentAt = 4;
string type = 5;
int32 version = 6;
google.protobuf.Any metadata = 7;
DropSessionPayload payload = 8;
}
Is this somehow possible?
This is definitely possible, though there are some other ways to do it. You can factor the common fields into a single message, which is included in each of your requests / responses:
message CommonRequestFields {
string messageId = 1;
string origin = 2;
string correlationId = 3;
int32 sentAt = 4;
string type = 5;
int32 version = 6;
}
You can include this as a formal field on each request message type, without needing to depend on Any
:
message CreateSessionRequest {
CommonRequestFields common = 1;
Session session = 2;
// ...
}
message DropSessionRequest {
CommonRequestFields common = 1;
string sessionId = 2;
// ...
}
This approach has several upsides:
However, there are some downsides:
Lastly, if you are using gRPC, you can send Metadata along with each request, which lives outside of the request body. Metadata fields are String key-value pairs (same as HTTP headers) and may be appropriate if you are sending them for every requests or response. You can even encode protos as header fields, though this may require some more advanced API usage.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With