Skip to content

Commit

Permalink
Merge pull request #1 from getmiranda/fix/interface_convertion
Browse files Browse the repository at this point in the history
fix(memcachemock): add validation in interface conversion
  • Loading branch information
getmiranda authored Jun 10, 2022
2 parents b167c62 + 06b78b3 commit ebe7375
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
30 changes: 25 additions & 5 deletions memcachemock/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ func (c *clientMock) Get(key string) (*item.Item, error) {
if mock.Error != nil {
return nil, mock.Error
}
return mock.Return.(*item.Item), nil
item, ok := mock.Return.(*item.Item)
if !ok {
return nil, ErrInterfaceConvertion
}
return item, nil
}

func (c *clientMock) Touch(key string, seconds int32) (err error) {
Expand All @@ -54,7 +58,11 @@ func (c *clientMock) GetMulti(keys []string) (map[string]*item.Item, error) {
if mock.Error != nil {
return nil, mock.Error
}
return mock.Return.(map[string]*item.Item), nil
items, ok := mock.Return.(map[string]*item.Item)
if !ok {
return nil, ErrInterfaceConvertion
}
return items, nil
}

func (c *clientMock) Set(item *item.Item) error {
Expand Down Expand Up @@ -156,7 +164,11 @@ func (c *clientMock) Increment(key string, delta uint64) (newValue uint64, err e
if mock.Error != nil {
return 0, mock.Error
}
return mock.Return.(uint64), nil
newValue, ok := mock.Return.(uint64)
if !ok {
return 0, ErrInterfaceConvertion
}
return newValue, nil
}

func (c *clientMock) Decrement(key string, delta uint64) (newValue uint64, err error) {
Expand All @@ -169,7 +181,11 @@ func (c *clientMock) Decrement(key string, delta uint64) (newValue uint64, err e
if mock.Error != nil {
return 0, mock.Error
}
return mock.Return.(uint64), nil
newValue, ok := mock.Return.(uint64)
if !ok {
return 0, ErrInterfaceConvertion
}
return newValue, nil
}

func (c *clientMock) Exists(key string) (bool, error) {
Expand All @@ -182,5 +198,9 @@ func (c *clientMock) Exists(key string) (bool, error) {
if mock.Error != nil {
return false, mock.Error
}
return mock.Return.(bool), nil
exists, ok := mock.Return.(bool)
if !ok {
return false, ErrInterfaceConvertion
}
return exists, nil
}
3 changes: 2 additions & 1 deletion memcachemock/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ var (
memcacheClient: &clientMock{},
}

ErrMockNotFound = errors.New("mock not found")
ErrMockNotFound = errors.New("mock not found")
ErrInterfaceConvertion = errors.New("interface convertion error")
)

type mockServer struct {
Expand Down

0 comments on commit ebe7375

Please sign in to comment.