github.com/cloudwego/dynamicgo@v0.2.6-0.20240519101509-707f41b6b834/conv/j2t/README.md (about) 1 <!-- Code generated by gomarkdoc. DO NOT EDIT --> 2 3 # j2t 4 5 ```go 6 import "github.com/cloudwego/dynamicgo/conv/j2t" 7 ``` 8 9 ## Index 10 11 - [type BinaryConv](<#type-binaryconv>) 12 - [func NewBinaryConv(opts conv.Options) BinaryConv](<#func-newbinaryconv>) 13 - [func (self *BinaryConv) Do(ctx context.Context, desc *thrift.TypeDescriptor, jbytes []byte) (tbytes []byte, err error)](<#func-binaryconv-do>) 14 - [func (self *BinaryConv) DoInto(ctx context.Context, desc *thrift.TypeDescriptor, jbytes []byte, buf *[]byte) (err error)](<#func-binaryconv-dointo>) 15 - [func (self *BinaryConv) SetOptions(opts conv.Options)](<#func-binaryconv-setoptions>) 16 - [type HTTPConv](<#type-httpconv>) 17 - [func NewHTTPConv(proto meta.Encoding, fnDesc *thrift.FunctionDescriptor) *HTTPConv](<#func-newhttpconv>) 18 - [func (h HTTPConv) Do(ctx context.Context, req http.RequestGetter, opt conv.Options) (tbytes []byte, err error)](<#func-httpconv-do>) 19 - [func (h HTTPConv) DoInto(ctx context.Context, req http.RequestGetter, buf *[]byte, opt conv.Options) (err error)](<#func-httpconv-dointo>) 20 21 22 ## type BinaryConv 23 24 BinaryConv is a converter from json to thrift binary 25 26 ```go 27 type BinaryConv struct { 28 // contains filtered or unexported fields 29 } 30 ``` 31 32 ### func NewBinaryConv 33 34 ```go 35 func NewBinaryConv(opts conv.Options) BinaryConv 36 ``` 37 38 NewBinaryConv returns a new BinaryConv 39 40 ### func \(\*BinaryConv\) Do 41 42 ```go 43 func (self *BinaryConv) Do(ctx context.Context, desc *thrift.TypeDescriptor, jbytes []byte) (tbytes []byte, err error) 44 ``` 45 46 #### Do converts json bytes \(jbytes\) to thrift binary \(tbytes\) 47 48 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 49 50 <details><summary>Example</summary> 51 <p> 52 53 ```go 54 { 55 56 desc := getExampleDesc() 57 data := getExampleData() 58 59 cv := NewBinaryConv(opts) 60 61 out, err := cv.Do(context.Background(), desc, data) 62 if err != nil { 63 panic(err) 64 } 65 66 exp := example3.NewExampleReq() 67 err = json.Unmarshal(data, exp) 68 if err != nil { 69 panic(err) 70 } 71 act := example3.NewExampleReq() 72 _, err = act.FastRead(out) 73 if err != nil { 74 panic(err) 75 } 76 if !reflect.DeepEqual(exp, act) { 77 panic("not equal") 78 } 79 } 80 ``` 81 82 </p> 83 </details> 84 85 ### func \(\*BinaryConv\) DoInto 86 87 ```go 88 func (self *BinaryConv) DoInto(ctx context.Context, desc *thrift.TypeDescriptor, jbytes []byte, buf *[]byte) (err error) 89 ``` 90 91 DoInto behaves like Do, but it writes the result to buffer directly instead of returning a new buffer 92 93 ### func \(\*BinaryConv\) SetOptions 94 95 ```go 96 func (self *BinaryConv) SetOptions(opts conv.Options) 97 ``` 98 99 SetOptions sets options 100 101 ## type HTTPConv 102 103 HTTPConv is a converter from http request to thrift message 104 105 ```go 106 type HTTPConv struct { 107 // contains filtered or unexported fields 108 } 109 ``` 110 111 ### func NewHTTPConv 112 113 ```go 114 func NewHTTPConv(proto meta.Encoding, fnDesc *thrift.FunctionDescriptor) *HTTPConv 115 ``` 116 117 NewHTTPConv returns a new HTTPConv, which contains the thrift message header and footer 118 119 proto is specified thrift encoding protocol \(meta.EncodingThriftBinary|meta.EncodingThriftCompact\) fnDesc is the thrift method descriptor corresponding to the http request url 120 121 ### func \(HTTPConv\) Do 122 123 ```go 124 func (h HTTPConv) Do(ctx context.Context, req http.RequestGetter, opt conv.Options) (tbytes []byte, err error) 125 ``` 126 127 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 128 129 ### func \(HTTPConv\) DoInto 130 131 ```go 132 func (h HTTPConv) DoInto(ctx context.Context, req http.RequestGetter, buf *[]byte, opt conv.Options) (err error) 133 ``` 134 135 <details><summary>Example</summary> 136 <p> 137 138 ```go 139 { 140 141 svc, err := thrift.NewDescritorFromPath(context.Background(), exampleIDLPath) 142 if err != nil { 143 panic(err) 144 } 145 fn := svc.Functions()["ExampleMethod"] 146 147 cv := NewHTTPConv(meta.EncodingThriftBinary, fn) 148 149 jdata := `{"msg":"hello","InnerBase":{}}` 150 stdreq, err := stdhttp.NewRequest("POST", 151 "http://localhost:8080/example?query=1,2,3&inner_query=中文", 152 strings.NewReader(jdata)) 153 if err != nil { 154 panic(err) 155 } 156 stdreq.Header.Set("Content-Type", "application/json") 157 stdreq.Header.Set("heeader", "true") 158 stdreq.Header.Set("inner_string", "igorned") 159 160 req, err := http.NewHTTPRequestFromStdReq( 161 stdreq, 162 http.Param{Key: "path", Value: "OK"}, 163 http.Param{Key: "inner_string", Value: "priority"}, 164 ) 165 166 buf := make([]byte, 0, len(jdata)*2/3) 167 168 err = cv.DoInto(context.Background(), req, &buf, opts) 169 if err != nil { 170 panic(err) 171 } 172 173 p := thrift.NewBinaryProtocol(buf) 174 method, mType, seqID, reqID, stru, err := p.UnwrapBody() 175 println(method, mType, seqID, reqID) 176 177 act := example3.NewExampleReq() 178 _, err = act.FastRead(stru) 179 if err != nil { 180 panic(err) 181 } 182 spew.Dump(act) 183 } 184 ``` 185 186 </p> 187 </details> 188 189 190 191 Generated by [gomarkdoc](<https://github.com/princjef/gomarkdoc>)