如何在gRPCproto文件中创建关联?

我可能会以错误的方式处理它,但我想定义两个或多个结构(消息)之间的关系。

以 StackOverflow 为例,假设我LabelService在标签上有一个for CRUD 操作。我也有QuestionService一个Question可以有的地方Labels。我们还假设我有 aUserService和 aUser也可以附上标签

# label.proto
service LabelService {
    rpc CreateLabel() returns();
    ...etc
}
message Label {
   string text = 1;
}

但现在我想创建我的QuestionServiceQuestion消息。我是如何将这两个文件关联起来的,还是在 go 代码中完成了这种关联级别?

# question.proto
service QuestionService {
    rpc CreateQuestion() returns();
    ...etc
}
message Question {
   string text = 1;
   repeat Label labels = 2 # <-- how to do this?
}

# user.proto
service UserService {
    rpc CreateQuestion() returns();
    ...etc
}
message User {
   string name = 1;
   repeat Label labels = 2 # <-- how to do this?
}

我想我很困惑,因为对于 REST API 和使用 gorm.io 例如,我会在结构中设置关联并让 gorm.io 创建表。

回答

从文档:

import "myproject/other_protos.proto";

所以只需import在您question.protouser.proto. 这与导入其他标准proto定义(如timestampand )时没有什么不同duration

import "google/protobuf/timestamp.proto";
import "google/protobuf/duration.proto";


以上是如何在gRPCproto文件中创建关联?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>