diff --git a/clients/goredis.go b/clients/goredis.go index 6a609fc..d3f454b 100644 --- a/clients/goredis.go +++ b/clients/goredis.go @@ -9,18 +9,23 @@ import ( "github.com/nitishm/go-rejson/v4/rjs" ) +// GoRedisClientConn - an abstracted interface for goredis.Client, goredis.ClusterClient, goredis.Ring, +// or goredis.UniversalClient +type GoRedisClientConn interface { + Do(ctx context.Context, args ...interface{}) *goredis.Cmd +} + // GoRedis implements ReJSON interface for Go-Redis/Redis Redis client // Link: https://github.com/go-redis/redis type GoRedis struct { - Conn *goredis.Client // import goredis "github.com/go-redis/redis/v8" - + Conn GoRedisClientConn // ctx defines context for the provided connection ctx context.Context } // NewGoRedisClient returns a new GoRedis ReJSON client with the provided context // and connection, if ctx is nil default context.Background will be used -func NewGoRedisClient(ctx context.Context, conn *goredis.Client) *GoRedis { +func NewGoRedisClient(ctx context.Context, conn GoRedisClientConn) *GoRedis { if ctx == nil { ctx = context.Background() } diff --git a/clients/redigo.go b/clients/redigo.go index 98eb366..2c3c707 100644 --- a/clients/redigo.go +++ b/clients/redigo.go @@ -2,15 +2,19 @@ package clients import ( "fmt" - redigo "github.com/gomodule/redigo/redis" "github.com/nitishm/go-rejson/v4/rjs" "strings" ) +// RedigoClientConn - an abstracted interface for redigo.Conn and redigo.ConnWithTimeout +type RedigoClientConn interface { + Do(commandName string, args ...interface{}) (reply interface{}, err error) +} + // Redigo implements ReJSON interface for GoModule/Redigo Redis client // Link: https://github.com/gomodule/redigo type Redigo struct { - Conn redigo.Conn // import redigo "github.com/gomodule/redigo" + Conn RedigoClientConn } // JSONSet used to set a json object diff --git a/rejson_test.go b/rejson_test.go index 05bf0b6..3aeebb1 100644 --- a/rejson_test.go +++ b/rejson_test.go @@ -3,6 +3,7 @@ package rejson import ( "context" "encoding/json" + "github.com/nitishm/go-rejson/v4/clients" "reflect" "testing" @@ -45,7 +46,9 @@ func (t *TestClient) init() []helper { } // GoRedis Test Client - goredisCli := goredis.NewClient(&goredis.Options{Addr: "localhost:6379"}) + goredisCli := goredis.NewUniversalClient(&goredis.UniversalOptions{ + Addrs: []string{"localhost:6379"}, + }) return []helper{ {cli: redigoCli, name: "Redigo ", closeFunc: func() { @@ -73,10 +76,10 @@ func (t *TestClient) SetTestingClient(conn interface{}) { t.conn = conn switch conn := conn.(type) { - case redigo.Conn: + case clients.RedigoClientConn: t.name = "Redigo-" t.rh.SetRedigoClient(conn) - case *goredis.Client: + case clients.GoRedisClientConn: t.name = "GoRedis-" t.rh.SetGoRedisClient(conn) default: diff --git a/rjs/options.go b/rjs/options.go index c3b2da4..c6c7526 100644 --- a/rjs/options.go +++ b/rjs/options.go @@ -39,7 +39,7 @@ func (g GetOption) Value() []interface{} { } // SetValue will set the values in the options -func (g GetOption) SetValue(arg string) { +func (g *GetOption) SetValue(arg string) { g.Arg = arg } diff --git a/set_client.go b/set_client.go index 69af199..58f1fcd 100644 --- a/set_client.go +++ b/set_client.go @@ -2,8 +2,6 @@ package rejson import ( "context" - goredis "github.com/go-redis/redis/v8" - redigo "github.com/gomodule/redigo/redis" "github.com/nitishm/go-rejson/v4/clients" "github.com/nitishm/go-rejson/v4/rjs" ) @@ -11,8 +9,8 @@ import ( // RedisClient provides interface for Client handling in the ReJSON Handler type RedisClient interface { SetClientInactive() - SetRedigoClient(redigo.Conn) - SetGoRedisClient(conn *goredis.Client) + SetRedigoClient(conn clients.RedigoClientConn) + SetGoRedisClient(conn clients.GoRedisClientConn) } // SetClientInactive resets the handler and unset any client, set to the handler @@ -24,20 +22,20 @@ func (r *Handler) SetClientInactive() { // SetRedigoClient sets Redigo (https://github.com/gomodule/redigo/redis) client // to the handler -func (r *Handler) SetRedigoClient(conn redigo.Conn) { +func (r *Handler) SetRedigoClient(conn clients.RedigoClientConn) { r.clientName = "redigo" r.implementation = &clients.Redigo{Conn: conn} } // SetGoRedisClient sets Go-Redis (https://github.com/go-redis/redis) client to // the handler. It is left for backward compatibility. -func (r *Handler) SetGoRedisClient(conn *goredis.Client) { - r.SetGoRedisClientWithContext(context.TODO(), conn) +func (r *Handler) SetGoRedisClient(conn clients.GoRedisClientConn) { + r.SetGoRedisClientWithContext(context.Background(), conn) } // SetGoRedisClientWithContext sets Go-Redis (https://github.com/go-redis/redis) client to // the handler with a global context for the connection -func (r *Handler) SetGoRedisClientWithContext(ctx context.Context, conn *goredis.Client) { +func (r *Handler) SetGoRedisClientWithContext(ctx context.Context, conn clients.GoRedisClientConn) { r.clientName = "goredis" r.implementation = clients.NewGoRedisClient(ctx, conn) }