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 json to thrift binary
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 ¶
// 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 := 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") }
Output:
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 ¶
type HTTPConv struct {
// contains filtered or unexported fields
}
HTTPConv is a converter from http request to thrift message
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 ¶
// get function descriptor svc, err := thrift.NewDescritorFromPath(context.Background(), exampleIDLPath) if err != nil { panic(err) } fn := svc.Functions()["ExampleMethod"] // make HTTPConv cv := NewHTTPConv(meta.EncodingThriftBinary, fn) // make std http request 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") // wrap std http request as http.RequestGetter req, err := http.NewHTTPRequestFromStdReq( stdreq, http.Param{Key: "path", Value: "OK"}, http.Param{Key: "inner_string", Value: "priority"}, ) // allocate output buffer // TIPS: json data size is usually 1.5x times of thrift data size buf := make([]byte, 0, len(jdata)*2/3) // do conversion err = cv.DoInto(context.Background(), req, &buf, opts) if err != nil { panic(err) } // unwrap thrift message p := thrift.NewBinaryProtocol(buf) method, mType, seqID, reqID, stru, err := p.UnwrapBody() println(method, mType, seqID, reqID) // ExampleMethod 1 0 1 // validate result act := example3.NewExampleReq() _, err = act.FastRead(stru) if err != nil { panic(err) } spew.Dump(act)
Output: