Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix build without cgo #1182

Merged
merged 1 commit into from
Oct 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"io/ioutil"
"time"

"github.com/DataDog/zstd"
"github.com/eapache/go-xerial-snappy"
"github.com/pierrec/lz4"
)
Expand Down Expand Up @@ -116,7 +115,7 @@ func (m *Message) encode(pe packetEncoder) error {
m.compressedCache = buf.Bytes()
payload = m.compressedCache
case CompressionZSTD:
c, err := zstd.CompressLevel(nil, m.Value, m.CompressionLevel)
c, err := zstdCompressLevel(nil, m.Value, m.CompressionLevel)
if err != nil {
return err
}
Expand Down Expand Up @@ -219,7 +218,7 @@ func (m *Message) decode(pd packetDecoder) (err error) {
if m.Value == nil {
break
}
if m.Value, err = zstd.Decompress(nil, m.Value); err != nil {
if m.Value, err = zstdDecompress(nil, m.Value); err != nil {
return err
}
if err := m.decodeSet(); err != nil {
Expand Down
5 changes: 2 additions & 3 deletions record_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"io/ioutil"
"time"

"github.com/DataDog/zstd"
"github.com/eapache/go-xerial-snappy"
"github.com/pierrec/lz4"
)
Expand Down Expand Up @@ -195,7 +194,7 @@ func (b *RecordBatch) decode(pd packetDecoder) (err error) {
return err
}
case CompressionZSTD:
if recBuffer, err = zstd.Decompress(nil, recBuffer); err != nil {
if recBuffer, err = zstdDecompress(nil, recBuffer); err != nil {
return err
}
default:
Expand Down Expand Up @@ -254,7 +253,7 @@ func (b *RecordBatch) encodeRecords(pe packetEncoder) error {
}
b.compressedRecords = buf.Bytes()
case CompressionZSTD:
c, err := zstd.CompressLevel(nil, raw, b.CompressionLevel)
c, err := zstdCompressLevel(nil, raw, b.CompressionLevel)
if err != nil {
return err
}
Expand Down
13 changes: 13 additions & 0 deletions zstd_cgo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// +build cgo

package sarama

import "github.com/DataDog/zstd"

func zstdDecompress(dst, src []byte) ([]byte, error) {
return zstd.Decompress(dst, src)
}

func zstdCompressLevel(dst, src []byte, level int) ([]byte, error) {
return zstd.CompressLevel(dst, src, level)
}
17 changes: 17 additions & 0 deletions zstd_fallback.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// +build !cgo

package sarama

import (
"errors"
)

var errZstdCgo = errors.New("zstd compression requires building with cgo enabled")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this error be public?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered that, but I didn't think it would be useful (what is the user going to do differently in this case?). If it were public, we would have to maintain it forever to keep backwards compatibility (e.g. even after switching to a pure-go zstd library).


func zstdDecompress(dst, src []byte) ([]byte, error) {
return nil, errZstdCgo
}

func zstdCompressLevel(dst, src []byte, level int) ([]byte, error) {
return nil, errZstdCgo
}