Set proto buf (#278)

* 新加SetProtoBuf接口

* 开发ok
This commit is contained in:
guonaihong
2021-04-15 23:43:51 +08:00
committed by GitHub
parent 380ca7d30c
commit 7de0dd5ab2
10 changed files with 361 additions and 1 deletions

2
Makefile Normal file
View File

@@ -0,0 +1,2 @@
protobuf:
protoc --go_out=. --go_opt=paths=source_relative testdata/test.proto

View File

@@ -57,6 +57,7 @@ gout 是go写的http 客户端,为提高工作效率而开发
- [xml](#xml)
- [form-data](#form-data)
- [x-www-form-urlencoded](#x-www-form-urlencoded)
- [protobuf](#protobuf)
- [callback](#callback)
- [Set request timeout](#Set-request-timeout)
- [proxy](#proxy)
@@ -878,7 +879,23 @@ float64=3.14&int=3&string=test-www-Form
*/
```
### protobuf
SetProtoBuf支持protobuf序列化后的[]byte或者生成的protobuf结构体指针
```go
package main
import (
"github.com/guonaihong/gout"
)
func main() {
httpCode := 0
err := GET(":8080/echo").
SetProtoBuf( /* protobuf 生成的结构体,必须传指针类型*/ ).
Code(&httpCode).
Do()
}
```
### callback
callback主要用在服务端会返回多种格式body的场景, 比如404返回的是html, 200返回json。
这时候要用Callback挂载多种处理函数处理不同的数据结构

View File

@@ -193,6 +193,14 @@ func (df *DataFlow) SetYAML(obj interface{}) *DataFlow {
return df
}
// SetProtoBuf send yaml to the http body, Support struct types
// obj必须是结构体指针或者[]byte类型
func (df *DataFlow) SetProtoBuf(obj interface{}) *DataFlow {
df.out.opt.ReqBodyType = "protobuf"
df.Req.bodyEncoder = encode.NewProtoBufEncode(obj)
return df
}
func (df *DataFlow) initTransport() {
if df.out.Client.Transport == nil {
df.out.Client.Transport = &http.Transport{}

47
encode/protobuf.go Normal file
View File

@@ -0,0 +1,47 @@
package encode
import (
"errors"
"io"
"github.com/guonaihong/gout/core"
"google.golang.org/protobuf/proto"
)
var ErrNotImplMessage = errors.New("The proto.Message interface is not implemented")
type ProtoBufEncode struct {
obj interface{}
}
func NewProtoBufEncode(obj interface{}) *ProtoBufEncode {
return &ProtoBufEncode{obj: obj}
}
func (p *ProtoBufEncode) Encode(w io.Writer) (err error) {
if v, ok := core.GetBytes(p.obj); ok {
//TODO找一个检测protobuf数据格式的函数
_, err = w.Write(v)
return err
}
var m proto.Message
var ok bool
m, ok = p.obj.(proto.Message)
if !ok {
// 这里如果能把普通结构体转成指针类型结构体就
return ErrNotImplMessage
}
all, err := proto.Marshal(m)
if err != nil {
return err
}
_, err = w.Write(all)
return err
}
func (p *ProtoBufEncode) Name() string {
return "protobuf"
}

52
encode/protobuf_test.go Normal file
View File

@@ -0,0 +1,52 @@
package encode
import (
"bytes"
"fmt"
"testing"
"github.com/guonaihong/gout/testdata"
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/proto"
)
func TestProtoBuf_Name(t *testing.T) {
assert.Equal(t, NewProtoBufEncode("").Name(), "protobuf")
}
func TestProtoBuf_Fail(t *testing.T) {
out := bytes.Buffer{}
for _, v := range []interface{}{testdata.Req{}} {
p := NewProtoBufEncode(v)
out.Reset()
err := p.Encode(&out)
assert.Error(t, err)
}
}
func TestProtoBuf_Encode(t *testing.T) {
data1, err1 := proto.Marshal(&testdata.Req{Seq: 1, Res: "fk"})
assert.NoError(t, err1)
data2 := &testdata.Req{Seq: 1, Res: "fk"}
data := []interface{}{data1, data2, string(data1)}
out := bytes.Buffer{}
for i, v := range data {
p := NewProtoBufEncode(v)
out.Reset()
p.Encode(&out)
got := testdata.Req{}
err := proto.Unmarshal(out.Bytes(), &got)
assert.NoError(t, err)
assert.Equal(t, got.Seq, int32(1), fmt.Sprintf("fail index:%d", i))
assert.Equal(t, got.Res, "fk")
}
}

1
go.mod
View File

@@ -7,5 +7,6 @@ require (
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.4.0
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa
google.golang.org/protobuf v1.26.0
gopkg.in/yaml.v2 v2.2.8
)

10
go.sum
View File

@@ -13,8 +13,11 @@ github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD87
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
github.com/go-playground/validator/v10 v10.2.0 h1:KgJ0snyC2R9VXYN2rneOtQcw5aHQB1Vv0sFl1UcHBOY=
github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI=
github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I=
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/golang/protobuf v1.5.0 h1:LUVKkCeviFUMKqHa4tXIIij/lbhnMbP7Fn5wKdKkRh4=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
@@ -49,6 +52,11 @@ golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

63
setprotobuf_test.go Normal file
View File

@@ -0,0 +1,63 @@
package gout
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
"github.com/guonaihong/gout/testdata"
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/proto"
"github.com/gin-gonic/gin"
)
func setupEcho(total *int32) *gin.Engine {
router := gin.Default()
cb := func(c *gin.Context) {
atomic.AddInt32(total, 1)
io.Copy(c.Writer, c.Request.Body)
}
router.GET("/echo", cb)
return router
}
func Test_SetProtoBuf(t *testing.T) {
total := int32(0)
router := setupEcho(&total)
ts := httptest.NewServer(http.HandlerFunc(router.ServeHTTP))
defer ts.Close()
data1, err := proto.Marshal(&testdata.Req{Seq: 1, Res: "fk"})
assert.NoError(t, err)
testCount := 0
for i, v := range []interface{}{
data1,
&testdata.Req{Seq: 1, Res: "fk"},
} {
code := 0
var gotRes []byte
err := GET(ts.URL + "/echo").SetProtoBuf(v).BindBody(&gotRes).Code(&code).Do()
assert.NoError(t, err)
assert.Equal(t, code, 200)
got := &testdata.Req{}
err = proto.Unmarshal(gotRes, got)
assert.NoError(t, err)
assert.Equal(t, got.Seq, int32(1), fmt.Sprintf("fail index:%d", i))
assert.Equal(t, got.Res, "fk")
testCount++
}
assert.Equal(t, total, int32(testCount))
}

152
testdata/test.pb.go vendored Normal file
View File

@@ -0,0 +1,152 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.26.0
// protoc v3.15.6
// source: testdata/test.proto
package testdata
import (
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
reflect "reflect"
sync "sync"
)
const (
// Verify that this generated code is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
// Verify that runtime/protoimpl is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
)
type Req struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Seq int32 `protobuf:"varint,1,opt,name=seq,proto3" json:"seq,omitempty"`
Res string `protobuf:"bytes,2,opt,name=res,proto3" json:"res,omitempty"`
}
func (x *Req) Reset() {
*x = Req{}
if protoimpl.UnsafeEnabled {
mi := &file_testdata_test_proto_msgTypes[0]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *Req) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*Req) ProtoMessage() {}
func (x *Req) ProtoReflect() protoreflect.Message {
mi := &file_testdata_test_proto_msgTypes[0]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use Req.ProtoReflect.Descriptor instead.
func (*Req) Descriptor() ([]byte, []int) {
return file_testdata_test_proto_rawDescGZIP(), []int{0}
}
func (x *Req) GetSeq() int32 {
if x != nil {
return x.Seq
}
return 0
}
func (x *Req) GetRes() string {
if x != nil {
return x.Res
}
return ""
}
var File_testdata_test_proto protoreflect.FileDescriptor
var file_testdata_test_proto_rawDesc = []byte{
0x0a, 0x13, 0x74, 0x65, 0x73, 0x74, 0x64, 0x61, 0x74, 0x61, 0x2f, 0x74, 0x65, 0x73, 0x74, 0x2e,
0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x08, 0x74, 0x65, 0x73, 0x74, 0x64, 0x61, 0x74, 0x61, 0x22,
0x29, 0x0a, 0x03, 0x72, 0x65, 0x71, 0x12, 0x10, 0x0a, 0x03, 0x73, 0x65, 0x71, 0x18, 0x01, 0x20,
0x01, 0x28, 0x05, 0x52, 0x03, 0x73, 0x65, 0x71, 0x12, 0x10, 0x0a, 0x03, 0x72, 0x65, 0x73, 0x18,
0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x72, 0x65, 0x73, 0x42, 0x25, 0x5a, 0x23, 0x67, 0x69,
0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x67, 0x75, 0x6f, 0x6e, 0x61, 0x69, 0x68,
0x6f, 0x6e, 0x67, 0x2f, 0x67, 0x6f, 0x75, 0x74, 0x2f, 0x74, 0x65, 0x73, 0x74, 0x64, 0x61, 0x74,
0x61, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
file_testdata_test_proto_rawDescOnce sync.Once
file_testdata_test_proto_rawDescData = file_testdata_test_proto_rawDesc
)
func file_testdata_test_proto_rawDescGZIP() []byte {
file_testdata_test_proto_rawDescOnce.Do(func() {
file_testdata_test_proto_rawDescData = protoimpl.X.CompressGZIP(file_testdata_test_proto_rawDescData)
})
return file_testdata_test_proto_rawDescData
}
var file_testdata_test_proto_msgTypes = make([]protoimpl.MessageInfo, 1)
var file_testdata_test_proto_goTypes = []interface{}{
(*Req)(nil), // 0: testdata.req
}
var file_testdata_test_proto_depIdxs = []int32{
0, // [0:0] is the sub-list for method output_type
0, // [0:0] is the sub-list for method input_type
0, // [0:0] is the sub-list for extension type_name
0, // [0:0] is the sub-list for extension extendee
0, // [0:0] is the sub-list for field type_name
}
func init() { file_testdata_test_proto_init() }
func file_testdata_test_proto_init() {
if File_testdata_test_proto != nil {
return
}
if !protoimpl.UnsafeEnabled {
file_testdata_test_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*Req); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
}
type x struct{}
out := protoimpl.TypeBuilder{
File: protoimpl.DescBuilder{
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_testdata_test_proto_rawDesc,
NumEnums: 0,
NumMessages: 1,
NumExtensions: 0,
NumServices: 0,
},
GoTypes: file_testdata_test_proto_goTypes,
DependencyIndexes: file_testdata_test_proto_depIdxs,
MessageInfos: file_testdata_test_proto_msgTypes,
}.Build()
File_testdata_test_proto = out.File
file_testdata_test_proto_rawDesc = nil
file_testdata_test_proto_goTypes = nil
file_testdata_test_proto_depIdxs = nil
}

10
testdata/test.proto vendored Normal file
View File

@@ -0,0 +1,10 @@
syntax = "proto3";
option go_package = "github.com/guonaihong/gout/testdata";
package testdata;
message req {
int32 seq = 1;
string res = 2;
}