Skip to content

Commit

Permalink
Merge pull request #14 from renproject/fix/binary-decoding
Browse files Browse the repository at this point in the history
Fix binary decoding
  • Loading branch information
jazg committed Nov 29, 2019
2 parents 61b8dc8 + 7f0345b commit f8d0743
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 10 deletions.
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ jobs:
golint ./...
CI=true /go/bin/ginkgo -v --race --cover --coverprofile coverprofile.out ./...
/go/bin/covermerge \
codec/coverprofile.out \
cache/lru/coverprofile.out \
cache/ttl/coverprofile.out \
leveldb/coverprofile.out \
Expand Down
19 changes: 9 additions & 10 deletions codec/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,20 @@ func (binaryCodec) Encode(obj interface{}) ([]byte, error) {
return obj.(encoding.BinaryMarshaler).MarshalBinary()
default:
buf := new(bytes.Buffer)
if err := binary.Write(buf, binary.LittleEndian, obj); err != nil {
return buf.Bytes(), err
}
return buf.Bytes(), nil
err := binary.Write(buf, binary.LittleEndian, obj)
return buf.Bytes(), err
}
}

// Decode implements the `db.Codec`
func (binaryCodec) Decode(data []byte, value interface{}) error {
switch value.(type) {
switch v := value.(type) {
case encoding.BinaryUnmarshaler:
return value.(encoding.BinaryUnmarshaler).UnmarshalBinary(data)
case *[]byte:
*v = make([]byte, len(data))
copy(*v, data)
return nil
default:
buf := bytes.NewBuffer(data)
return binary.Read(buf, binary.LittleEndian, value)
Expand Down Expand Up @@ -75,11 +77,8 @@ type gobCodec struct{}
// Encode implements the `db.Codec`
func (gobCodec) Encode(obj interface{}) ([]byte, error) {
buf := new(bytes.Buffer)
if err := gob.NewEncoder(buf).Encode(obj); err != nil {
return nil, err
}

return buf.Bytes(), nil
err := gob.NewEncoder(buf).Encode(obj)
return buf.Bytes(), err
}

// Decode implements the `db.Codec`
Expand Down
13 changes: 13 additions & 0 deletions codec/codec_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package codec_test

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"testing"
)

func TestCodec(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Codec Suite")
}
110 changes: 110 additions & 0 deletions codec/codec_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package codec_test

import (
"bytes"
"reflect"
"testing/quick"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/renproject/kv/codec"
. "github.com/renproject/kv/testutil"
)

var _ = Describe("codec", func() {
Context("binary codec", func() {
It("should return the correct string", func() {
codec := BinaryCodec
Expect(codec.String()).To(Equal("binary"))
})

It("should be able to correctly encode/decode a custom struct", func() {
test := func(obj TestStruct) bool {
codec := BinaryCodec
data, err := codec.Encode(obj)
Expect(err).NotTo(HaveOccurred())

var decoded TestStruct
Expect(codec.Decode(data, &decoded)).NotTo(HaveOccurred())
Expect(reflect.DeepEqual(obj, decoded)).Should(BeTrue())
return true
}

Expect(quick.Check(test, nil)).NotTo(HaveOccurred())
})

It("should be able to correctly encode/decode bytes", func() {
test := func(obj []byte) bool {
codec := BinaryCodec
data, err := codec.Encode(obj)
Expect(err).NotTo(HaveOccurred())

var decoded []byte
Expect(codec.Decode(data, &decoded)).NotTo(HaveOccurred())
Expect(bytes.Equal(obj, decoded)).Should(BeTrue())
return true
}

Expect(quick.Check(test, nil)).NotTo(HaveOccurred())
})

It("should be able to correctly encode/decode int", func() {
test := func(obj int64) bool {
codec := BinaryCodec
data, err := codec.Encode(obj)
Expect(err).NotTo(HaveOccurred())

var decoded int64
Expect(codec.Decode(data, &decoded)).NotTo(HaveOccurred())
Expect(decoded).To(Equal(obj))
return true
}

Expect(quick.Check(test, nil)).NotTo(HaveOccurred())
})
})

Context("JSON codec", func() {
It("should return the correct string", func() {
codec := JSONCodec
Expect(codec.String()).To(Equal("json"))
})

It("should be able to correctly encode/decode a custom struct", func() {
test := func(obj TestStruct) bool {
codec := JSONCodec
data, err := codec.Encode(obj)
Expect(err).NotTo(HaveOccurred())

var decoded TestStruct
Expect(codec.Decode(data, &decoded)).NotTo(HaveOccurred())
Expect(reflect.DeepEqual(obj, decoded)).Should(BeTrue())
return true
}

Expect(quick.Check(test, nil)).NotTo(HaveOccurred())
})
})

Context("Gob codec", func() {
It("should return the correct string", func() {
codec := GobCodec
Expect(codec.String()).To(Equal("gob"))
})

It("should be able to correctly encode/decode a custom struct", func() {
test := func(obj TestStruct) bool {
codec := GobCodec
data, err := codec.Encode(obj)
Expect(err).NotTo(HaveOccurred())

var decoded TestStruct
Expect(codec.Decode(data, &decoded)).NotTo(HaveOccurred())
Expect(reflect.DeepEqual(obj, decoded)).Should(BeTrue())
return true
}

Expect(quick.Check(test, nil)).NotTo(HaveOccurred())
})
})
})

0 comments on commit f8d0743

Please sign in to comment.