github.com/cloudwego/dynamicgo@v0.2.6-0.20240519101509-707f41b6b834/conv/p2j/README.md (about) 1 <!-- Code generated by gomarkdoc. DO NOT EDIT --> 2 3 # p2j 4 5 ```go 6 import "github.com/cloudwego/dynamicgo/conv/p2j" 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, pbytes []byte) (json []byte, err error)](<#BinaryConv.Do>) 14 - [func (self *BinaryConv) DoInto(ctx context.Context, desc *proto.TypeDescriptor, pbytes []byte, buf *[]byte) (err error)](<#BinaryConv.DoInto>) 15 - [func (self *BinaryConv) SetOptions(opts conv.Options)](<#BinaryConv.SetOptions>) 16 17 18 <a name="BinaryConv"></a> 19 ## type [BinaryConv](<https://github.com/khan-yin/dynamicgo/blob/main/conv/p2j/conv.go#L12-L14>) 20 21 22 23 ```go 24 type BinaryConv struct { 25 // contains filtered or unexported fields 26 } 27 ``` 28 29 <a name="NewBinaryConv"></a> 30 ### func [NewBinaryConv](<https://github.com/khan-yin/dynamicgo/blob/main/conv/p2j/conv.go#L17>) 31 32 ```go 33 func NewBinaryConv(opts conv.Options) BinaryConv 34 ``` 35 36 NewBinaryConv returns a new BinaryConv 37 38 <a name="BinaryConv.Do"></a> 39 ### func (*BinaryConv) [Do](<https://github.com/khan-yin/dynamicgo/blob/main/conv/p2j/conv.go#L28>) 40 41 ```go 42 func (self *BinaryConv) Do(ctx context.Context, desc *proto.TypeDescriptor, pbytes []byte) (json []byte, err error) 43 ``` 44 45 Do converts protobuf binary (pbytes) to json bytes (jbytes) desc is the protobuf type descriptor of the protobuf binary, usually it is a response Message type 46 47 <details><summary>Example</summary> 48 <p> 49 50 51 52 ```go 53 package main 54 55 import ( 56 "context" 57 "encoding/json" 58 "reflect" 59 60 "github.com/cloudwego/dynamicgo/conv" 61 "github.com/cloudwego/dynamicgo/internal/util_test" 62 "github.com/cloudwego/dynamicgo/proto" 63 "github.com/cloudwego/dynamicgo/testdata/kitex_gen/pb/example2" 64 goprotowire "google.golang.org/protobuf/encoding/protowire" 65 ) 66 67 var opts = conv.Options{} 68 69 func main() { 70 // get descriptor and data 71 includeDirs := util_test.MustGitPath("testdata/idl/") // includeDirs is used to find the include files. 72 desc := proto.FnRequest(proto.GetFnDescFromFile(exampleIDLPath, "ExampleMethod", proto.Options{}, includeDirs)) 73 74 // make BinaryConv 75 cv := NewBinaryConv(conv.Options{}) 76 in := readExampleReqProtoBufData() 77 78 // do conversion 79 out, err := cv.Do(context.Background(), desc, in) 80 if err != nil { 81 panic(err) 82 } 83 exp := example2.ExampleReq{} 84 85 // use kitex_util to check proto data validity 86 l := 0 87 dataLen := len(in) 88 for l < dataLen { 89 id, wtyp, tagLen := goprotowire.ConsumeTag(in) 90 if tagLen < 0 { 91 panic("proto data error format") 92 } 93 l += tagLen 94 in = in[tagLen:] 95 offset, err := exp.FastRead(in, int8(wtyp), int32(id)) 96 if err != nil { 97 panic(err) 98 } 99 in = in[offset:] 100 l += offset 101 } 102 if len(in) != 0 { 103 panic("proto data error format") 104 } 105 106 // validate result 107 var act example2.ExampleReq 108 err = json.Unmarshal([]byte(out), &act) 109 if err != nil { 110 panic(err) 111 } 112 if !reflect.DeepEqual(exp, act) { 113 panic("not equal") 114 } 115 } 116 ``` 117 118 </p> 119 </details> 120 121 <a name="BinaryConv.DoInto"></a> 122 ### func (*BinaryConv) [DoInto](<https://github.com/khan-yin/dynamicgo/blob/main/conv/p2j/conv.go#L57>) 123 124 ```go 125 func (self *BinaryConv) DoInto(ctx context.Context, desc *proto.TypeDescriptor, pbytes []byte, buf *[]byte) (err error) 126 ``` 127 128 DoInto behaves like Do, but it writes the result to buffer directly instead of returning a new buffer 129 130 <a name="BinaryConv.SetOptions"></a> 131 ### func (*BinaryConv) [SetOptions](<https://github.com/khan-yin/dynamicgo/blob/main/conv/p2j/conv.go#L22>) 132 133 ```go 134 func (self *BinaryConv) SetOptions(opts conv.Options) 135 ``` 136 137 SetOptions sets options 138 139 Generated by [gomarkdoc](<https://github.com/princjef/gomarkdoc>)