github.com/cloudwego/dynamicgo@v0.2.6-0.20240519101509-707f41b6b834/conv/t2j/README.md (about)

     1  <!-- Code generated by gomarkdoc. DO NOT EDIT -->
     2  
     3  # t2j
     4  
     5  ```go
     6  import "github.com/cloudwego/dynamicgo/conv/t2j"
     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, tbytes []byte) (json []byte, err error)](<#func-binaryconv-do>)
    14    - [func (self *BinaryConv) DoInto(ctx context.Context, desc *thrift.TypeDescriptor, tbytes []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, desc *thrift.FunctionDescriptor) *HTTPConv](<#func-newhttpconv>)
    18    - [func (h HTTPConv) Do(ctx context.Context, resp http.ResponseSetter, tbytes []byte, opt conv.Options) (err error)](<#func-httpconv-do>)
    19    - [func (h HTTPConv) DoInto(ctx context.Context, resp http.ResponseSetter, tbytes []byte, buf *[]byte, opt conv.Options) (err error)](<#func-httpconv-dointo>)
    20  
    21  
    22  ## type BinaryConv
    23  
    24  BinaryConv is a converter from thrift binary to json
    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, tbytes []byte) (json []byte, err error)
    44  ```
    45  
    46  #### Do converts thrift binary \(tbytes\) to json bytes \(jbytes\)
    47  
    48  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
    49  
    50  <details><summary>Example</summary>
    51  <p>
    52  
    53  ```go
    54  {
    55  
    56  	desc := thrift.FnResponse(thrift.GetFnDescFromFile("testdata/idl/example3.thrift", "ExampleMethod", thrift.Options{}))
    57  	data := getExample3Data()
    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  	// validate result
    67  	var exp, act example3.ExampleResp
    68  	_, err = exp.FastRead(data)
    69  	if err != nil {
    70  		panic(err)
    71  	}
    72  	err = json.Unmarshal(out, &act)
    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, tbytes []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 thrift message to http response
   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, desc *thrift.FunctionDescriptor) *HTTPConv
   115  ```
   116  
   117  NewHTTPConv returns a new HTTPConv
   118  
   119  ### func \(HTTPConv\) Do
   120  
   121  ```go
   122  func (h HTTPConv) Do(ctx context.Context, resp http.ResponseSetter, tbytes []byte, opt conv.Options) (err error)
   123  ```
   124  
   125  Do converts thrift message \(tbytes\) into http response. resp body is set as json protocol if has
   126  
   127  ### func \(HTTPConv\) DoInto
   128  
   129  ```go
   130  func (h HTTPConv) DoInto(ctx context.Context, resp http.ResponseSetter, tbytes []byte, buf *[]byte, opt conv.Options) (err error)
   131  ```
   132  
   133  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.
   134  
   135  <details><summary>Example</summary>
   136  <p>
   137  
   138  ```go
   139  {
   140  
   141  	desc := thrift.GetFnDescFromFile("testdata/idl/example3.thrift", "ExampleMethod", thrift.Options{})
   142  
   143  	data := getExample3Data()
   144  	in, err := thrift.WrapBinaryBody(data, "ExampleMethod", thrift.REPLY, thrift.FieldID(0), 1)
   145  	if err != nil {
   146  		panic(err)
   147  	}
   148  
   149  	resp := http.NewHTTPResponse()
   150  	resp.StatusCode = 200
   151  
   152  	cv := NewHTTPConv(meta.EncodingThriftBinary, desc)
   153  
   154  	buf := make([]byte, 0, len(data)*2)
   155  	err = cv.DoInto(context.Background(), resp, in, &buf, opts)
   156  	if err != nil {
   157  		panic(err)
   158  	}
   159  
   160  	// validate result
   161  	var act example3.ExampleResp
   162  	err = json.Unmarshal(buf, &act)
   163  	if err != nil {
   164  		panic(err)
   165  	}
   166  
   167  	spew.Dump(act)
   168  
   169  	spew.Dump(resp)
   170  }
   171  ```
   172  
   173  </p>
   174  </details>
   175  
   176  
   177  
   178  Generated by [gomarkdoc](<https://github.com/princjef/gomarkdoc>)