protobuf 总结

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"}, ""))
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值