import "github.com/cloudwego/dynamicgo/conv/j2t"
BinaryConv is a converter from json to thrift binary
type BinaryConv struct {
// contains filtered or unexported fields
}
func NewBinaryConv(opts conv.Options) BinaryConv
NewBinaryConv returns a new BinaryConv
func (self *BinaryConv) Do(ctx context.Context, desc *thrift.TypeDescriptor, jbytes []byte) (tbytes []byte, err error)
desc is the thrift type descriptor of the thrift binary, usually it the request STRUCT type ctx is the context, which can be used to pass arguments as below: - conv.CtxKeyHTTPRequest: http.RequestGetter as http request - conv.CtxKeyThriftRespBase: thrift.Base as base metadata of thrift response
{
desc := getExampleDesc()
data := getExampleData()
cv := NewBinaryConv(opts)
out, err := cv.Do(context.Background(), desc, data)
if err != nil {
panic(err)
}
exp := example3.NewExampleReq()
err = json.Unmarshal(data, exp)
if err != nil {
panic(err)
}
act := example3.NewExampleReq()
_, err = act.FastRead(out)
if err != nil {
panic(err)
}
if !reflect.DeepEqual(exp, act) {
panic("not equal")
}
}
func (self *BinaryConv) DoInto(ctx context.Context, desc *thrift.TypeDescriptor, jbytes []byte, buf *[]byte) (err error)
DoInto behaves like Do, but it writes the result to buffer directly instead of returning a new buffer
func (self *BinaryConv) SetOptions(opts conv.Options)
SetOptions sets options
HTTPConv is a converter from http request to thrift message
type HTTPConv struct {
// contains filtered or unexported fields
}
func NewHTTPConv(proto meta.Encoding, fnDesc *thrift.FunctionDescriptor) *HTTPConv
NewHTTPConv returns a new HTTPConv, which contains the thrift message header and footer
proto is specified thrift encoding protocol (meta.EncodingThriftBinary|meta.EncodingThriftCompact) fnDesc is the thrift method descriptor corresponding to the http request url
func (h HTTPConv) Do(ctx context.Context, req http.RequestGetter, opt conv.Options) (tbytes []byte, err error)
Do converts http request into thrift message. req body must be one of following: - json (application/json) - url encoded form (application/x-www-form-urlencoded) - empty
func (h HTTPConv) DoInto(ctx context.Context, req http.RequestGetter, buf *[]byte, opt conv.Options) (err error)
{
svc, err := thrift.NewDescritorFromPath(context.Background(), exampleIDLPath)
if err != nil {
panic(err)
}
fn := svc.Functions()["ExampleMethod"]
cv := NewHTTPConv(meta.EncodingThriftBinary, fn)
jdata := `{"msg":"hello","InnerBase":{}}`
stdreq, err := stdhttp.NewRequest("POST",
"http://localhost:8080/example?query=1,2,3&inner_query=中文",
strings.NewReader(jdata))
if err != nil {
panic(err)
}
stdreq.Header.Set("Content-Type", "application/json")
stdreq.Header.Set("heeader", "true")
stdreq.Header.Set("inner_string", "igorned")
req, err := http.NewHTTPRequestFromStdReq(
stdreq,
http.Param{Key: "path", Value: "OK"},
http.Param{Key: "inner_string", Value: "priority"},
)
buf := make([]byte, 0, len(jdata)*2/3)
err = cv.DoInto(context.Background(), req, &buf, opts)
if err != nil {
panic(err)
}
p := thrift.NewBinaryProtocol(buf)
method, mType, seqID, reqID, stru, err := p.UnwrapBody()
println(method, mType, seqID, reqID)
act := example3.NewExampleReq()
_, err = act.FastRead(stru)
if err != nil {
panic(err)
}
spew.Dump(act)
}
Generated by gomarkdoc
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。