Skip to content

Commit

Permalink
fix: fix buf reader
Browse files Browse the repository at this point in the history
  • Loading branch information
HeyJavaBean committed Dec 3, 2024
1 parent 5fbaf2f commit 4c8bcdd
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions protocol/thrift/apache/adaptor/adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func AdaptRead(p, iprot interface{}) error {
return fmt.Errorf("no codec implementation available")
}

var rd io.Reader
var br bufiox.Reader
// if iprot is from kitex v0.12.0+, use interface assert to get bufiox reader
if bp, ok := iprot.(bufioxReaderWriter); ok {
Expand All @@ -58,29 +59,40 @@ func AdaptRead(p, iprot interface{}) error {
case byteBuffer:
// if reader is from byteBuffer, Read() function is not always available
// so use an adaptor to implement Read() by Next() and ReadableLen()
br = bufiox.NewDefaultReader(byteBuffer2ReadWriter(r))
rd = byteBuffer2ReadWriter(r)
case io.ReadWriter:
// if reader is not byteBuffer but is io.ReadWriter, it suppose to be apache thrift binary protocol
br = bufiox.NewDefaultReader(r)
// if reader is not byteBuffer but is io.ReadWriter, it supposes to be apache thrift binary protocol
rd = r
default:
return fmt.Errorf("reader not ok")
}
break
}
}
}
if br == nil {
if rd == nil && br == nil {
return fmt.Errorf("no available field for reader")
}

// read data from iprot
buf, err := thrift.NewSkipDecoder(br).Next(thrift.STRUCT)
var sd *thrift.SkipDecoder
if br != nil {
sd = thrift.NewSkipDecoder(br)
} else {
// if there's no bufiox.Reader, do not wrap a new bufiox.Reader, or some data will remain in the buffer
// directly read from io.Reader
sd = thrift.NewSkipDecoderWithIOReader(rd)
}

buf, err := sd.Next(thrift.STRUCT)
if err != nil {
return err
}

sd.Release()

// unmarshal the data into struct
_, err = fastStruct.FastRead(buf)

return err
}

Expand Down

0 comments on commit 4c8bcdd

Please sign in to comment.