3 Star 0 Fork 0

Gitee 极速下载 / dynamicgo

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

t2j

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

Index

type BinaryConv

BinaryConv is a converter from thrift binary to json

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, tbytes []byte) (json []byte, err error)

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

desc is the thrift type descriptor of the thrift binary, usually it is a response STRUCT type ctx is the context, which can be used to pass arguments as below: - conv.CtxKeyHTTPResponse: http.ResponseSetter as http request - conv.CtxKeyThriftRespBase: thrift.Base as base metadata of thrift response

Example

{

	desc := thrift.FnResponse(thrift.GetFnDescFromFile("testdata/idl/example3.thrift", "ExampleMethod", thrift.Options{}))
	data := getExample3Data()

	cv := NewBinaryConv(opts)

	out, err := cv.Do(context.Background(), desc, data)
	if err != nil {
		panic(err)
	}

	// validate result
	var exp, act example3.ExampleResp
	_, err = exp.FastRead(data)
	if err != nil {
		panic(err)
	}
	err = json.Unmarshal(out, &act)
	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, tbytes []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 thrift message to http response

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

func NewHTTPConv

func NewHTTPConv(proto meta.Encoding, desc *thrift.FunctionDescriptor) *HTTPConv

NewHTTPConv returns a new HTTPConv

func (HTTPConv) Do

func (h HTTPConv) Do(ctx context.Context, resp http.ResponseSetter, tbytes []byte, opt conv.Options) (err error)

Do converts thrift message (tbytes) into http response. resp body is set as json protocol if has

func (HTTPConv) DoInto

func (h HTTPConv) DoInto(ctx context.Context, resp http.ResponseSetter, tbytes []byte, buf *[]byte, opt conv.Options) (err error)

DoInto converts the thrift message (tbytes) to into buf in JSON protocol, as well as other http response arguments. WARN: This will set buf to resp, thus DONOT reuse the buf afterward.

Example

{

	desc := thrift.GetFnDescFromFile("testdata/idl/example3.thrift", "ExampleMethod", thrift.Options{})

	data := getExample3Data()
	in, err := thrift.WrapBinaryBody(data, "ExampleMethod", thrift.REPLY, thrift.FieldID(0), 1)
	if err != nil {
		panic(err)
	}

	resp := http.NewHTTPResponse()
	resp.StatusCode = 200

	cv := NewHTTPConv(meta.EncodingThriftBinary, desc)

	buf := make([]byte, 0, len(data)*2)
	err = cv.DoInto(context.Background(), resp, in, &buf, opts)
	if err != nil {
		panic(err)
	}

	// validate result
	var act example3.ExampleResp
	err = json.Unmarshal(buf, &act)
	if err != nil {
		panic(err)
	}

	spew.Dump(act)

	spew.Dump(resp)
}

Generated by gomarkdoc

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

搜索帮助