1、基础语法
https://blog.csdn.net/weixin_42282999/article/details/106274099
2、proto转化为go语言结构对应
(1)message<==>struct
message FieldListRequest {
// 抽取器id
uint32 extractor_id = 1;
}
==>
type FieldListRequest struct {
// 抽取器id
ExtractorId uint32 `protobuf:"varint,1,opt,name=extractor_id,json=extractorId,proto3" json:"extractor_id,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
(2)double <=>float64
(3)bytes<=>[]byte
(4)repeated<==>slice
message FieldListResponse {
// 字段列表
repeated FieldDetail field_list = 1;
}
==>
type FieldListResponse struct {
// 字段列表
FieldList []*FieldDetail `protobuf:"bytes,1,rep,name=field_list,json=fieldList,proto3" json:"field_list,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
(5)enum<==>const 常量
enum ClassifierCopyStatus {
// 默认
CLASSIFIER_COPY_STATUS_DEFAULT = 0;
// 等待
CLASSIFIER_COPY_STATUS_PENDING = 1;
// 进行中
CLASSIFIER_COPY_STATUS_DOING = 2;
// 完成
CLASSIFIER_COPY_STATUS_SUCCESS = 3;
// 失败
CLASSIFIER_COPY_STATUS_FAIL = 4;
}
==>
const (
// 默认
ClassifierCopyStatus_CLASSIFIER_COPY_STATUS_DEFAULT ClassifierCopyStatus = 0
// 等待
ClassifierCopyStatus_CLASSIFIER_COPY_STATUS_PENDING ClassifierCopyStatus = 1
// 进行中
ClassifierCopyStatus_CLASSIFIER_COPY_STATUS_DOING ClassifierCopyStatus = 2
// 完成
ClassifierCopyStatus_CLASSIFIER_COPY_STATUS_SUCCESS ClassifierCopyStatus = 3
// 失败
ClassifierCopyStatus_CLASSIFIER_COPY_STATUS_FAIL ClassifierCopyStatus = 4
)
(6) oneof<==>interface,oneof中的结构体都实现interface中的函数
message ClassDetail {
oneof detail {
KeywordClassDetail keyword_class = 1;
ModelClassDetail model_class = 2;
}
}
==>
type ClassDetail struct {
// Types that are valid to be assigned to Detail:
// *ClassDetail_KeywordClass
// *ClassDetail_ModelClass
Detail isClassDetail_Detail `protobuf_oneof:"detail"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
type isClassDetail_Detail interface {
isClassDetail_Detail()
}
(7)rpc<==>interface中的func
rpc CreateExtractor(CreateExtractorRequest) returns (ExtractorOperationResponse) {}
==>
CreateExtractor(ctx context.Context, in *CreateExtractorRequest, opts ...grpc.CallOption) (*ExtractorOperationResponse, error)
(8)service转换
生成Server和Client的interface实现proto中的方法
客户端使用:
client := New*Client()
client.func
服务端使用:
s := grpc.NewServer
server:=Register*Server(s,*Controller)//将接口的实现注册入服务端对象,目的是关联实现函数
(9)option 设置参数选项
message ClassifyRequest {
option (grpc.gateway.protoc_gen_swagger.options.openapiv2_schema) = {
json_schema: {
required: ["doc"]
};
};
// 待进行分类的文本内容
string doc = 1;
}
⚠️注:设置doc字段必选
Required:
表示是一个必须字段
,必须相对于发送方
,在发送消息之前必须设置该字段的值
,对于接收方
,必须能够识别该字段
的意思。发送之前没有设置required字段或者无法识别required字段都会引发编解码异常,导致消息被丢弃
。
rpc Extract(ExtractRequest) returns (ExtractResponse) {
option (grpc.gateway.protoc_gen_swagger.options.openapiv2_operation) = {
description: "文本信息抽取能力是来也自研的NLP能力,实现从文本中按照一定逻辑提取关键信息的过程。"
};
option (google.api.http) = {
post:"/v1/document/extract"
body:"*"
};
}
⚠️注:设置函数操作的描述,http访问模式
ervice RestService {
rpc Get(StringMessage) returns (StringMessage) {
option (google.api.http) = {
get: "/get/{value}"
};
}
rpc Post(StringMessage) returns (StringMessage) {
option (google.api.http) = {
post: "/post"
body: "*"
};
}
}
option (google.api.http)转化成go语言会生成一个文件,包含路径
例如:
pattern_OcrService_OcrGeneral_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2, 2, 3}, []string{"v1", "document", "ocr", "general"}, ""))