Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BinaryConv ¶
type BinaryConv struct {
// contains filtered or unexported fields
}
BinaryConv is a converter from thrift binary to json
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 ¶
// get descriptor and data desc := thrift.FnResponse(thrift.GetFnDescFromFile("testdata/idl/example3.thrift", "ExampleMethod", thrift.Options{})) data := getExample3Data() // make BinaryConv cv := NewBinaryConv(opts) // do conversion 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") }
Output:
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 ¶
type HTTPConv struct {
// contains filtered or unexported fields
}
HTTPConv is a converter from thrift message to http response
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 ¶
// get function descriptor desc := thrift.GetFnDescFromFile("testdata/idl/example3.thrift", "ExampleMethod", thrift.Options{}) // make thrift message data := getExample3Data() in, err := thrift.WrapBinaryBody(data, "ExampleMethod", thrift.REPLY, thrift.FieldID(0), 1) if err != nil { panic(err) } // get http.ResponseSetter resp := http.NewHTTPResponse() resp.StatusCode = 200 // make HTTPConv cv := NewHTTPConv(meta.EncodingThriftBinary, desc) // do conversion 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) } // non-http annotations fields spew.Dump(act) // http annotations fields spew.Dump(resp)
Output: