Reputation: 102207
Here is the content of .proto
file:
syntax="proto3";
package topic;
option go_package="github.com/mrdulin/grpc-go-cnode/internal/protobufs/topic";
import "internal/protobufs/shared/user.proto";
import "internal/protobufs/reply/domain.proto";
enum Tab {
share = 0;
ask = 1;
good = 2;
job = 3;
}
message Topic {
string id = 1;
string author_id = 2;
Tab tab = 3;
string title = 4;
string content = 5;
shared.UserBase author = 6;
bool good = 7;
bool top = 8;
int32 reply_count = 9;
int32 visit_count = 10;
string create_at = 11;
string last_reply_at = 12;
}
message TopicDetail {
string id = 1;
string author_id = 2;
Tab tab = 3;
string title = 4;
string content = 5;
shared.UserBase author = 6;
bool good = 7;
bool top = 8;
int32 reply_count = 9;
int32 visit_count = 10;
string create_at = 11;
string last_reply_at = 12;
// different fields
repeated reply.Reply replies = 13;
bool is_collect = 14;
}
As you can see, in Topic
and TopicDetail
message types, most of the fields are the same. Is there any way such as inheritance, composition(embed message type) so I can DRY?
Upvotes: 2
Views: 2857
Reputation: 874
Sure, you can do something like this:
message Topic {
string id = 1;
string author_id = 2;
Tab tab = 3;
string title = 4;
string content = 5;
shared.UserBase author = 6;
bool good = 7;
bool top = 8;
int32 reply_count = 9;
int32 visit_count = 10;
string create_at = 11;
string last_reply_at = 12;
}
message TopicDetail {
Topic topic = 1;
repeated reply.Reply replies = 2;
bool is_collect = 3;
}
For details you can check https://developers.google.com/protocol-buffers/docs/proto3#other or if you would like to keep them nested, you can do that too, see: https://developers.google.com/protocol-buffers/docs/proto3#nested
Upvotes: 2