如何在gRPC原型文件中创建关联?

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


以堆栈溢出为例,假设我对标签有一个用于 CRUD 的操作。我也有一个地方可以有.让我们也假设我有一个和一个也可以附加标签LabelServiceQuestionServiceQuestionLabelsUserServiceUser


# label.proto

service LabelService {

    rpc CreateLabel() returns();

    ...etc

}

message Label {

   string text = 1;

}

但现在我想创建我的和消息。我是否将这两个文件关联到 go 代码中如何或完成此级别的关联?QuestionServiceQuestion


# 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 创建表。


梵蒂冈之花
浏览 134回答 2
2回答

阿晨1998

从文档中:import "myproject/other_protos.proto";所以只需将一个添加到您的.这与导入其他标准定义(如 和) 时没有什么不同:importquestion.protouser.protoprototimestampdurationimport "google/protobuf/timestamp.proto";import "google/protobuf/duration.proto";

饮歌长啸

您是否已经导入了有问题的用户原型?在问题中。import "user.proto"<- 我想你可以使用Label labels
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go