Skip to content

Commit

Permalink
fix: handle buffer encoding correctly for Go 1.16 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Strift committed Feb 18, 2025
1 parent 83a4c9c commit 5cdae38
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,23 @@ func (c *client) sendRequest(
}

if !c.contentEncoding.IsZero() {
body, err = c.encoder.Encode(body)
if err != nil {
return nil, internalError.WithErrCode(ErrCodeMarshalRequest,
fmt.Errorf("failed to marshal with json.Marshal: %w", err))
// Get the data from the buffer before encoding
var bufData []byte
if buf, ok := body.(*bytes.Buffer); ok {
bufData = buf.Bytes()
encodedBuf, err := c.encoder.Encode(bytes.NewReader(bufData))
if err != nil {
if buf, ok := body.(*bytes.Buffer); ok {
c.bufferPool.Put(buf)
}
return nil, internalError.WithErrCode(ErrCodeMarshalRequest,
fmt.Errorf("failed to encode request body: %w", err))

Check warning on line 208 in client.go

View check run for this annotation

Codecov / codecov/patch

client.go#L204-L208

Added lines #L204 - L208 were not covered by tests
}
// Return the original buffer to the pool since we have a new one
if buf, ok := body.(*bytes.Buffer); ok {
c.bufferPool.Put(buf)
}
body = encodedBuf
}
}
}
Expand Down

0 comments on commit 5cdae38

Please sign in to comment.