github.com/cloudwego/dynamicgo@v0.2.6-0.20240519101509-707f41b6b834/conv/j2p/example_test.go (about) 1 package j2p 2 3 import ( 4 "context" 5 "encoding/json" 6 "reflect" 7 8 "github.com/cloudwego/dynamicgo/conv" 9 "github.com/cloudwego/dynamicgo/testdata/kitex_gen/pb/example2" 10 "google.golang.org/protobuf/encoding/protowire" 11 ) 12 13 var opts = conv.Options{} 14 15 func ExampleBinaryConv_Do() { 16 // get descriptor and data 17 desc := getExampleDesc() 18 data := getExampleData() 19 20 // make BinaryConv 21 cv := NewBinaryConv(opts) 22 23 // do conversion 24 out, err := cv.Do(context.Background(), desc, data) 25 if err != nil { 26 panic(err) 27 } 28 29 // validate result 30 exp := &example2.ExampleReq{} 31 err = json.Unmarshal(data, exp) 32 if err != nil { 33 panic(err) 34 } 35 act := &example2.ExampleReq{} 36 l := 0 37 dataLen := len(out) 38 // fastRead to get target struct 39 for l < dataLen { 40 id, wtyp, tagLen := protowire.ConsumeTag(out) 41 if tagLen < 0 { 42 panic("parseTag failed") 43 } 44 l += tagLen 45 out = out[tagLen:] 46 offset, err := act.FastRead(out, int8(wtyp), int32(id)) 47 if err != nil { 48 panic(err) 49 } 50 out = out[offset:] 51 l += offset 52 } 53 if !reflect.DeepEqual(exp, act) { 54 panic("not equal") 55 } 56 }