3 Star 0 Fork 0

Gitee 极速下载 / dynamicgo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/cloudwego/dynamicgo
克隆/下载
README.md 4.91 KB
一键复制 编辑 原始数据 按行查看 历史
duanyi.aster 提交于 2023-01-05 17:30 . Initial commit

j2t

import "github.com/cloudwego/dynamicgo/conv/j2t"

Index

type BinaryConv

BinaryConv is a converter from json to thrift binary

type BinaryConv struct {
    // contains filtered or unexported fields
}

func NewBinaryConv

func NewBinaryConv(opts conv.Options) BinaryConv

NewBinaryConv returns a new BinaryConv

func (*BinaryConv) Do

func (self *BinaryConv) Do(ctx context.Context, desc *thrift.TypeDescriptor, jbytes []byte) (tbytes []byte, err error)

Do converts json bytes (jbytes) to thrift binary (tbytes)

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

Example

{

	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 (*BinaryConv) DoInto

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 (*BinaryConv) SetOptions

func (self *BinaryConv) SetOptions(opts conv.Options)

SetOptions sets options

type HTTPConv

HTTPConv is a converter from http request to thrift message

type HTTPConv struct {
    // contains filtered or unexported fields
}

func NewHTTPConv

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 (HTTPConv) Do

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 (HTTPConv) DoInto

func (h HTTPConv) DoInto(ctx context.Context, req http.RequestGetter, buf *[]byte, opt conv.Options) (err error)
Example

{

	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

1
https://gitee.com/mirrors/dynamicgo.git
git@gitee.com:mirrors/dynamicgo.git
mirrors
dynamicgo
dynamicgo
main

搜索帮助