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

     1  <!-- Code generated by gomarkdoc. DO NOT EDIT -->
     2  
     3  # j2p
     4  
     5  ```go
     6  import "github.com/cloudwego/dynamicgo/conv/j2p"
     7  ```
     8  
     9  ## Index
    10  
    11  - [type BinaryConv](<#BinaryConv>)
    12    - [func NewBinaryConv(opts conv.Options) BinaryConv](<#NewBinaryConv>)
    13    - [func (self *BinaryConv) Do(ctx context.Context, desc *proto.TypeDescriptor, jbytes []byte) (tbytes []byte, err error)](<#BinaryConv.Do>)
    14    - [func (self *BinaryConv) DoInto(ctx context.Context, desc *proto.TypeDescriptor, jbytes []byte, buf *[]byte) error](<#BinaryConv.DoInto>)
    15  
    16  
    17  <a name="BinaryConv"></a>
    18  ## type [BinaryConv](<https://github.com/khan-yin/dynamicgo/blob/main/conv/j2p/conv.go#L13-L15>)
    19  
    20  BinaryConv is a converter from json to protobuf binary
    21  
    22  ```go
    23  type BinaryConv struct {
    24      // contains filtered or unexported fields
    25  }
    26  ```
    27  
    28  <a name="NewBinaryConv"></a>
    29  ### func [NewBinaryConv](<https://github.com/khan-yin/dynamicgo/blob/main/conv/j2p/conv.go#L18>)
    30  
    31  ```go
    32  func NewBinaryConv(opts conv.Options) BinaryConv
    33  ```
    34  
    35  NewBinaryConv returns a new BinaryConv
    36  
    37  <a name="BinaryConv.Do"></a>
    38  ### func (*BinaryConv) [Do](<https://github.com/khan-yin/dynamicgo/blob/main/conv/j2p/conv.go#L26>)
    39  
    40  ```go
    41  func (self *BinaryConv) Do(ctx context.Context, desc *proto.TypeDescriptor, jbytes []byte) (tbytes []byte, err error)
    42  ```
    43  
    44  Do converts json bytes (jbytes) to protobuf binary (tbytes) desc is the protobuf type descriptor of the protobuf binary, usually it the request Message type
    45  
    46  <details><summary>Example</summary>
    47  <p>
    48  
    49  
    50  
    51  ```go
    52  package main
    53  
    54  import (
    55  	"context"
    56  	"encoding/json"
    57  	"reflect"
    58  
    59  	"github.com/cloudwego/dynamicgo/conv"
    60  	"github.com/cloudwego/dynamicgo/testdata/kitex_gen/pb/example2"
    61  	"google.golang.org/protobuf/encoding/protowire"
    62  )
    63  
    64  var opts = conv.Options{}
    65  
    66  func main() {
    67  	// get descriptor and data
    68  	desc := getExampleDesc()
    69  	data := getExampleData()
    70  
    71  	// make BinaryConv
    72  	cv := NewBinaryConv(opts)
    73  
    74  	// do conversion
    75  	out, err := cv.Do(context.Background(), desc, data)
    76  	if err != nil {
    77  		panic(err)
    78  	}
    79  
    80  	// validate result
    81  	exp := &example2.ExampleReq{}
    82  	err = json.Unmarshal(data, exp)
    83  	if err != nil {
    84  		panic(err)
    85  	}
    86  	act := &example2.ExampleReq{}
    87  	l := 0
    88  	dataLen := len(out)
    89  	// fastRead to get target struct
    90  	for l < dataLen {
    91  		id, wtyp, tagLen := protowire.ConsumeTag(out)
    92  		if tagLen < 0 {
    93  			panic("parseTag failed")
    94  		}
    95  		l += tagLen
    96  		out = out[tagLen:]
    97  		offset, err := act.FastRead(out, int8(wtyp), int32(id))
    98  		if err != nil {
    99  			panic(err)
   100  		}
   101  		out = out[offset:]
   102  		l += offset
   103  	}
   104  	if !reflect.DeepEqual(exp, act) {
   105  		panic("not equal")
   106  	}
   107  }
   108  ```
   109  
   110  </p>
   111  </details>
   112  
   113  <a name="BinaryConv.DoInto"></a>
   114  ### func (*BinaryConv) [DoInto](<https://github.com/khan-yin/dynamicgo/blob/main/conv/j2p/conv.go#L55>)
   115  
   116  ```go
   117  func (self *BinaryConv) DoInto(ctx context.Context, desc *proto.TypeDescriptor, jbytes []byte, buf *[]byte) error
   118  ```
   119  
   120  DoInto behaves like Do, but it writes the result to buffer directly instead of returning a new buffer
   121  
   122  Generated by [gomarkdoc](<https://github.com/princjef/gomarkdoc>)