Skip to content

Commit

Permalink
Implemented a nil decoder for kafka (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
michalkurzeja authored May 10, 2019
1 parent 88f485e commit 1e39a76
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
3 changes: 3 additions & 0 deletions kafka/encoder.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package kafka

// NilDecoder is a decoder that always returns a nil, no matter the input.
var NilDecoder DecoderFunc = func([]byte) (interface{}, error) { return nil, nil }

// Decoder represents a Kafka data decoder.
type Decoder interface {
// Decode transforms byte data to the desired type.
Expand Down
20 changes: 20 additions & 0 deletions kafka/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,23 @@ func TestEncoderFunc_Encode(t *testing.T) {
assert.Equal(t, b, result)
assert.True(t, e == err, "Received error is not exactly the same object that was returned by the function.")
}

func TestNilDecoder(t *testing.T) {
tests := []struct {
name string
in []byte
}{
{name: "some bytes", in: []byte("test")},
{name: "empty bytes", in: []byte{}},
{name: "nil", in: nil},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := kafka.NilDecoder.Decode(tt.in)

assert.NoError(t, err)
assert.Nil(t, got)
})
}
}

0 comments on commit 1e39a76

Please sign in to comment.