-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support high-performance generic-call SDK for protobuf protocol (…
…#37)
- Loading branch information
Showing
99 changed files
with
37,820 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
name: Push Check Linux-X64 | ||
|
||
on: push | ||
on: [ push, pull_request ] | ||
|
||
jobs: | ||
build: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
<!-- Code generated by gomarkdoc. DO NOT EDIT --> | ||
|
||
# j2p | ||
|
||
```go | ||
import "github.com/cloudwego/dynamicgo/conv/j2p" | ||
``` | ||
|
||
## Index | ||
|
||
- [type BinaryConv](<#BinaryConv>) | ||
- [func NewBinaryConv(opts conv.Options) BinaryConv](<#NewBinaryConv>) | ||
- [func (self *BinaryConv) Do(ctx context.Context, desc *proto.TypeDescriptor, jbytes []byte) (tbytes []byte, err error)](<#BinaryConv.Do>) | ||
- [func (self *BinaryConv) DoInto(ctx context.Context, desc *proto.TypeDescriptor, jbytes []byte, buf *[]byte) error](<#BinaryConv.DoInto>) | ||
|
||
|
||
<a name="BinaryConv"></a> | ||
## type [BinaryConv](<https://github.com/khan-yin/dynamicgo/blob/main/conv/j2p/conv.go#L13-L15>) | ||
|
||
BinaryConv is a converter from json to protobuf binary | ||
|
||
```go | ||
type BinaryConv struct { | ||
// contains filtered or unexported fields | ||
} | ||
``` | ||
|
||
<a name="NewBinaryConv"></a> | ||
### func [NewBinaryConv](<https://github.com/khan-yin/dynamicgo/blob/main/conv/j2p/conv.go#L18>) | ||
|
||
```go | ||
func NewBinaryConv(opts conv.Options) BinaryConv | ||
``` | ||
|
||
NewBinaryConv returns a new BinaryConv | ||
|
||
<a name="BinaryConv.Do"></a> | ||
### func (*BinaryConv) [Do](<https://github.com/khan-yin/dynamicgo/blob/main/conv/j2p/conv.go#L26>) | ||
|
||
```go | ||
func (self *BinaryConv) Do(ctx context.Context, desc *proto.TypeDescriptor, jbytes []byte) (tbytes []byte, err error) | ||
``` | ||
|
||
Do converts json bytes (jbytes) to protobuf binary (tbytes) desc is the protobuf type descriptor of the protobuf binary, usually it the request Message type | ||
|
||
<details><summary>Example</summary> | ||
<p> | ||
|
||
|
||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"reflect" | ||
|
||
"github.com/cloudwego/dynamicgo/conv" | ||
"github.com/cloudwego/dynamicgo/testdata/kitex_gen/pb/example2" | ||
"google.golang.org/protobuf/encoding/protowire" | ||
) | ||
|
||
var opts = conv.Options{} | ||
|
||
func main() { | ||
// get descriptor and data | ||
desc := getExampleDesc() | ||
data := getExampleData() | ||
|
||
// make BinaryConv | ||
cv := NewBinaryConv(opts) | ||
|
||
// do conversion | ||
out, err := cv.Do(context.Background(), desc, data) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// validate result | ||
exp := &example2.ExampleReq{} | ||
err = json.Unmarshal(data, exp) | ||
if err != nil { | ||
panic(err) | ||
} | ||
act := &example2.ExampleReq{} | ||
l := 0 | ||
dataLen := len(out) | ||
// fastRead to get target struct | ||
for l < dataLen { | ||
id, wtyp, tagLen := protowire.ConsumeTag(out) | ||
if tagLen < 0 { | ||
panic("parseTag failed") | ||
} | ||
l += tagLen | ||
out = out[tagLen:] | ||
offset, err := act.FastRead(out, int8(wtyp), int32(id)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
out = out[offset:] | ||
l += offset | ||
} | ||
if !reflect.DeepEqual(exp, act) { | ||
panic("not equal") | ||
} | ||
} | ||
``` | ||
|
||
</p> | ||
</details> | ||
|
||
<a name="BinaryConv.DoInto"></a> | ||
### func (*BinaryConv) [DoInto](<https://github.com/khan-yin/dynamicgo/blob/main/conv/j2p/conv.go#L55>) | ||
|
||
```go | ||
func (self *BinaryConv) DoInto(ctx context.Context, desc *proto.TypeDescriptor, jbytes []byte, buf *[]byte) error | ||
``` | ||
|
||
DoInto behaves like Do, but it writes the result to buffer directly instead of returning a new buffer | ||
|
||
Generated by [gomarkdoc](<https://github.com/princjef/gomarkdoc>) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//go:build amd64 && go1.16 | ||
// +build amd64,go1.16 | ||
|
||
// Copyright 2023 CloudWeGo Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package j2p | ||
|
||
import ( | ||
"github.com/chenzhuoyu/base64x" | ||
) | ||
|
||
func decodeBase64(src string) ([]byte, error) { | ||
return base64x.StdEncoding.DecodeString(src) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
//go:build !amd64 || !go1.16 | ||
// +build !amd64 !go1.16 | ||
|
||
// Copyright 2023 CloudWeGo Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package j2p | ||
|
||
import "encoding/base64" | ||
|
||
func decodeBase64(src string) ([]byte, error) { | ||
return base64.StdEncoding.DecodeString(src) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package j2p | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/cloudwego/dynamicgo/conv" | ||
"github.com/cloudwego/dynamicgo/http" | ||
"github.com/cloudwego/dynamicgo/meta" | ||
"github.com/cloudwego/dynamicgo/proto" | ||
) | ||
|
||
// BinaryConv is a converter from json to protobuf binary | ||
type BinaryConv struct { | ||
opts conv.Options | ||
} | ||
|
||
// NewBinaryConv returns a new BinaryConv | ||
func NewBinaryConv(opts conv.Options) BinaryConv { | ||
return BinaryConv{ | ||
opts: opts, | ||
} | ||
} | ||
|
||
// Do converts json bytes (jbytes) to protobuf binary (tbytes) | ||
// desc is the protobuf type descriptor of the protobuf binary, usually it the request Message type | ||
func (self *BinaryConv) Do(ctx context.Context, desc *proto.TypeDescriptor, jbytes []byte) (tbytes []byte, err error) { | ||
buf := conv.NewBytes() | ||
|
||
// req alloc but not use | ||
var req http.RequestGetter | ||
if self.opts.EnableHttpMapping { | ||
reqi := ctx.Value(conv.CtxKeyHTTPRequest) | ||
if reqi != nil { | ||
reqi, ok := reqi.(http.RequestGetter) | ||
if !ok { | ||
return nil, newError(meta.ErrInvalidParam, "invalid http.RequestGetter", nil) | ||
} | ||
req = reqi | ||
} else { | ||
return nil, newError(meta.ErrInvalidParam, "EnableHttpMapping but no http response in context", nil) | ||
} | ||
} | ||
|
||
err = self.do(ctx, jbytes, desc, buf, req) | ||
if err == nil && len(*buf) > 0 { | ||
tbytes = make([]byte, len(*buf)) | ||
copy(tbytes, *buf) | ||
} | ||
|
||
conv.FreeBytes(buf) | ||
return | ||
} | ||
|
||
// DoInto behaves like Do, but it writes the result to buffer directly instead of returning a new buffer | ||
func (self *BinaryConv) DoInto(ctx context.Context, desc *proto.TypeDescriptor, jbytes []byte, buf *[]byte) error { | ||
// req alloc but not use | ||
var req http.RequestGetter | ||
if self.opts.EnableHttpMapping { | ||
reqi := ctx.Value(conv.CtxKeyHTTPRequest) | ||
if reqi != nil { | ||
reqi, ok := reqi.(http.RequestGetter) | ||
if !ok { | ||
return newError(meta.ErrInvalidParam, "invalid http.RequestGetter", nil) | ||
} | ||
req = reqi | ||
} else { | ||
return newError(meta.ErrInvalidParam, "EnableHttpMapping but no http response in context", nil) | ||
} | ||
} | ||
return self.do(ctx, jbytes, desc, buf, req) | ||
} |
Oops, something went wrong.