Skip to content

Return an interface instead of StringCmd, StatusCmd, etc. #353

@domodwyer

Description

@domodwyer

First off, fantastic library; thanks very much for your hard work - it's used a lot in our production systems and never lets me down!

If go-redis methods were to return an interface exporting the same methods StringCmd, StatusCmd, IntCmd (etc), then users of the library would be able to mock out the responses from redis relatively easily.

For example:

type redisInterface interface {
    Get(key string) *redis.StringCmd
    Set(key string, value interface{}, expiration time.Duration) *redis.StatusCmd
    Del(keys ...string) *redis.IntCmd
}

The above code requires a return value of *redis.StringCmd etc, which does not export it's fields, making it impossible to write unit tests (i.e. no way to return an error from Err() in a mock).

If it was to return:

type redisInterface interface {
    Get(key string) redis.StringCmdInterface
    Set(key string, value interface{}, expiration time.Duration) redis.StatusCmdInterface
    Del(keys ...string) redis.IntCmdInterface
}

then client code could use the redisInterface in place of the redis.Client type, and in tests a mock implementation of redisInterface could be used returning mock responses (i.e a mockStringCmd could be set up to return a given value when Err() is called).

I've seen issue #332 where writing a wrapper around go-redis, and then mocking that was suggested, and while it works I feel like an extra a level of abstraction is not the best solution when the required code change is relatively straightforward.

I am more than happy to create a PR changing methods to return an interface, but just wanted to check if it's something that would be considered.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions