@@ -23,6 +23,7 @@ import (
2323 "testing"
2424 "time"
2525
26+ "github.com/cloudwego/gopkg/gridbuf"
2627 "github.com/cloudwego/gopkg/protocol/thrift"
2728 "github.com/stretchr/testify/assert"
2829 "github.com/stretchr/testify/require"
@@ -181,7 +182,7 @@ func TestDecode(t *testing.T) {
181182 name := tc .name
182183 updatef := tc .update
183184 testf := tc .test
184- t .Run (name , func (t * testing.T ) {
185+ t .Run (name + "_append_decode" , func (t * testing.T ) {
185186 p0 := NewTestTypes ()
186187 updatef (p0 ) // update by testcase func
187188
@@ -202,6 +203,29 @@ func TestDecode(t *testing.T) {
202203
203204 testf (t , p1 ) // test by testcase func
204205 })
206+ t .Run (name + "_grid_write_read" , func (t * testing.T ) {
207+ p0 := NewTestTypes ()
208+ updatef (p0 ) // update by testcase func
209+
210+ b , err := Append (nil , p0 )
211+ require .NoError (t , err )
212+
213+ wb := gridbuf .NewWriteBuffer ()
214+ err = GridWrite (wb , p0 )
215+ require .NoError (t , err )
216+ bs := wb .Bytes ()
217+
218+ rb := gridbuf .NewReadBuffer (bs )
219+ ufs , err := thrift .GridBuffer .Skip (rb , thrift .TType (tSTRUCT ), nil , true )
220+ require .NoError (t , err )
221+ require .Equal (t , len (ufs ), len (b ))
222+
223+ p1 := & TestTypes {}
224+ rb = gridbuf .NewReadBuffer (bs )
225+ err = GridRead (rb , p1 )
226+ require .NoError (t , err )
227+ testf (t , p1 ) // test by testcase func
228+ })
205229 }
206230}
207231
@@ -283,7 +307,7 @@ func TestDecodeOptional(t *testing.T) {
283307 name := tc .name
284308 updatef := tc .update
285309 testf := tc .test
286- t .Run (name , func (t * testing.T ) {
310+ t .Run (name + "_append_decode" , func (t * testing.T ) {
287311 p0 := NewTestTypesOptional ()
288312 updatef (p0 ) // update by testcase func
289313
@@ -304,6 +328,29 @@ func TestDecodeOptional(t *testing.T) {
304328
305329 testf (t , p1 ) // test by testcase func
306330 })
331+ t .Run (name + "_grid_write_read" , func (t * testing.T ) {
332+ p0 := NewTestTypesOptional ()
333+ updatef (p0 ) // update by testcase func
334+
335+ b , err := Append (nil , p0 )
336+ require .NoError (t , err )
337+
338+ wb := gridbuf .NewWriteBuffer ()
339+ err = GridWrite (wb , p0 )
340+ require .NoError (t , err )
341+ bs := wb .Bytes ()
342+
343+ rb := gridbuf .NewReadBuffer (bs )
344+ ufs , err := thrift .GridBuffer .Skip (rb , thrift .TType (tSTRUCT ), nil , true )
345+ require .NoError (t , err )
346+ require .Equal (t , len (ufs ), len (b ))
347+
348+ p1 := & TestTypesOptional {}
349+ rb = gridbuf .NewReadBuffer (bs )
350+ err = GridRead (rb , p1 )
351+ require .NoError (t , err )
352+ testf (t , p1 ) // test by testcase func
353+ })
307354 }
308355}
309356
@@ -332,6 +379,34 @@ func TestDecodeRequired(t *testing.T) {
332379 require .Equal (t , true , p1 .V )
333380}
334381
382+ func TestGridReadWriteRequired (t * testing.T ) {
383+ type S0 struct {
384+ V * bool `frugal:"1,optional,bool"`
385+ }
386+ type S1 struct {
387+ V bool `frugal:"1,required,bool"`
388+ }
389+ v := true
390+ p0 := & S0 {}
391+ wb := gridbuf .NewWriteBuffer ()
392+ err := GridWrite (wb , p0 )
393+ require .NoError (t , err )
394+ p1 := & S1 {}
395+ rb := gridbuf .NewReadBuffer (wb .Bytes ())
396+ err = GridRead (rb , p1 )
397+ require .Equal (t , newRequiredFieldNotSetException ("V" ), err )
398+
399+ p0 .V = & v
400+ wb = gridbuf .NewWriteBuffer ()
401+ err = GridWrite (wb , p0 )
402+ require .NoError (t , err )
403+
404+ rb = gridbuf .NewReadBuffer (wb .Bytes ())
405+ err = GridRead (rb , p1 )
406+ require .NoError (t , err )
407+ require .Equal (t , true , p1 .V )
408+ }
409+
335410func TestDecodeUnknownFields (t * testing.T ) {
336411 type Msg0 struct {
337412 I0 int32 `thrift:"i0,2" frugal:"2,default,i32"`
@@ -360,6 +435,36 @@ func TestDecodeUnknownFields(t *testing.T) {
360435 assert .Equal (t , testb , p ._unknownFields )
361436}
362437
438+ func TestGridReadWriteUnknownFields (t * testing.T ) {
439+ type Msg0 struct {
440+ I0 int32 `thrift:"i0,2" frugal:"2,default,i32"`
441+ S0 string `thrift:"s0,3" frugal:"3,default,string"`
442+ }
443+
444+ type Msg1 struct { // without S0
445+ I0 int32 `thrift:"i0,2" frugal:"2,default,i32"`
446+
447+ _unknownFields []byte
448+ }
449+
450+ msg := Msg0 {I0 : 1 , S0 : "s0" }
451+ wb := gridbuf .NewWriteBuffer ()
452+ _ = GridWrite (wb , msg )
453+ bs := wb .Bytes ()
454+
455+ p := & Msg1 {}
456+ rb := gridbuf .NewReadBuffer (bs )
457+ _ = GridRead (rb , p )
458+
459+ assert .Equal (t , msg .I0 , p .I0 )
460+
461+ sz := fieldHeaderLen + strHeaderLen + len (msg .S0 )
462+ testb := make ([]byte , sz )
463+ testb = appendStringField (testb [:0 ], 3 , msg .S0 )
464+ assert .Equal (t , sz , len (testb ))
465+ assert .Equal (t , testb , p ._unknownFields )
466+ }
467+
363468func TestDecodeNoCopy (t * testing.T ) {
364469 type Msg struct {
365470 A string `frugal:"1,default,string,nocopy"`
0 commit comments