Skip to content

Commit

Permalink
Merge pull request #1 from ReSTARTR/hotfix/decode-zerobytes
Browse files Browse the repository at this point in the history
EOF error when item.Value is zero value
  • Loading branch information
mosasiru committed Dec 9, 2016
2 parents ecb973b + 0d078fc commit ce8876c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
10 changes: 9 additions & 1 deletion memd.go
@@ -1,11 +1,16 @@
package memd

import (
"errors"
"log"

"github.com/douban/libmc/golibmc"
"github.com/ugorji/go/codec"
"log"
)

// ErrEmptyValue is returned by FromItem when the size of item.Value is zero.
var ErrEmptyValue = errors.New("cannot decode from empty value")

// Client is wrapper of *golibmc.Client.
type Client struct {
*golibmc.Client
Expand Down Expand Up @@ -121,6 +126,9 @@ func (c *Client) ToItem(key string, _val interface{}, exp int64) (*golibmc.Item,

// FromItem ... deserialize item.Value
func (c *Client) FromItem(item *golibmc.Item, val interface{}) error {
if len(item.Value) == 0 {
return ErrEmptyValue
}
return codec.NewDecoderBytes(item.Value, c.serializer).Decode(val)
}

Expand Down
27 changes: 25 additions & 2 deletions memd_test.go
@@ -1,11 +1,12 @@
package memd

import (
"github.com/douban/libmc/golibmc"
"github.com/ugorji/go/codec"
"log"
"testing"
"time"

"github.com/douban/libmc/golibmc"
"github.com/ugorji/go/codec"
)

type Result struct {
Expand Down Expand Up @@ -125,3 +126,25 @@ func ResultSerializer(t *testing.T) {
t.Error("invalid cache")
}
}

func TestResultFromItem(t *testing.T) {
c := New(golibmc.SimpleNew([]string{"localhost:11211"}))

tests := []struct {
b []byte
err error
}{
{[]byte(``), ErrEmptyValue},
{[]byte(`{}`), nil},
{[]byte(`{"foo":"bar"}`), nil},
}

for _, tt := range tests {
item := &golibmc.Item{Value: tt.b}
var v map[string]interface{}
err := c.FromItem(item, &v)
if err != tt.err {
t.Errorf("FromItem(%+v, %s) = %#v", item, v, err)
}
}
}

0 comments on commit ce8876c

Please sign in to comment.