Skip to content

Commit 0f5ba3d

Browse files
authored
Remove direct dependency of clients for obtaining connection (#62)
* Change go-redis client to universal client interface #53 * Abstracts clients to support different types of redis connections * Adding support for different types of connections provided by libraries #63
1 parent 16e335f commit 0f5ba3d

File tree

5 files changed

+27
-17
lines changed

5 files changed

+27
-17
lines changed

clients/goredis.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,23 @@ import (
99
"github.com/nitishm/go-rejson/v4/rjs"
1010
)
1111

12+
// GoRedisClientConn - an abstracted interface for goredis.Client, goredis.ClusterClient, goredis.Ring,
13+
// or goredis.UniversalClient
14+
type GoRedisClientConn interface {
15+
Do(ctx context.Context, args ...interface{}) *goredis.Cmd
16+
}
17+
1218
// GoRedis implements ReJSON interface for Go-Redis/Redis Redis client
1319
// Link: https://github.com/go-redis/redis
1420
type GoRedis struct {
15-
Conn *goredis.Client // import goredis "github.com/go-redis/redis/v8"
16-
21+
Conn GoRedisClientConn
1722
// ctx defines context for the provided connection
1823
ctx context.Context
1924
}
2025

2126
// NewGoRedisClient returns a new GoRedis ReJSON client with the provided context
2227
// and connection, if ctx is nil default context.Background will be used
23-
func NewGoRedisClient(ctx context.Context, conn *goredis.Client) *GoRedis {
28+
func NewGoRedisClient(ctx context.Context, conn GoRedisClientConn) *GoRedis {
2429
if ctx == nil {
2530
ctx = context.Background()
2631
}

clients/redigo.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@ package clients
22

33
import (
44
"fmt"
5-
redigo "github.com/gomodule/redigo/redis"
65
"github.com/nitishm/go-rejson/v4/rjs"
76
"strings"
87
)
98

9+
// RedigoClientConn - an abstracted interface for redigo.Conn and redigo.ConnWithTimeout
10+
type RedigoClientConn interface {
11+
Do(commandName string, args ...interface{}) (reply interface{}, err error)
12+
}
13+
1014
// Redigo implements ReJSON interface for GoModule/Redigo Redis client
1115
// Link: https://github.com/gomodule/redigo
1216
type Redigo struct {
13-
Conn redigo.Conn // import redigo "github.com/gomodule/redigo"
17+
Conn RedigoClientConn
1418
}
1519

1620
// JSONSet used to set a json object

rejson_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package rejson
33
import (
44
"context"
55
"encoding/json"
6+
"github.com/nitishm/go-rejson/v4/clients"
67
"reflect"
78
"testing"
89

@@ -45,7 +46,9 @@ func (t *TestClient) init() []helper {
4546
}
4647

4748
// GoRedis Test Client
48-
goredisCli := goredis.NewClient(&goredis.Options{Addr: "localhost:6379"})
49+
goredisCli := goredis.NewUniversalClient(&goredis.UniversalOptions{
50+
Addrs: []string{"localhost:6379"},
51+
})
4952

5053
return []helper{
5154
{cli: redigoCli, name: "Redigo ", closeFunc: func() {
@@ -73,10 +76,10 @@ func (t *TestClient) SetTestingClient(conn interface{}) {
7376
t.conn = conn
7477

7578
switch conn := conn.(type) {
76-
case redigo.Conn:
79+
case clients.RedigoClientConn:
7780
t.name = "Redigo-"
7881
t.rh.SetRedigoClient(conn)
79-
case *goredis.Client:
82+
case clients.GoRedisClientConn:
8083
t.name = "GoRedis-"
8184
t.rh.SetGoRedisClient(conn)
8285
default:

rjs/options.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (g GetOption) Value() []interface{} {
3939
}
4040

4141
// SetValue will set the values in the options
42-
func (g GetOption) SetValue(arg string) {
42+
func (g *GetOption) SetValue(arg string) {
4343
g.Arg = arg
4444
}
4545

set_client.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@ package rejson
22

33
import (
44
"context"
5-
goredis "github.com/go-redis/redis/v8"
6-
redigo "github.com/gomodule/redigo/redis"
75
"github.com/nitishm/go-rejson/v4/clients"
86
"github.com/nitishm/go-rejson/v4/rjs"
97
)
108

119
// RedisClient provides interface for Client handling in the ReJSON Handler
1210
type RedisClient interface {
1311
SetClientInactive()
14-
SetRedigoClient(redigo.Conn)
15-
SetGoRedisClient(conn *goredis.Client)
12+
SetRedigoClient(conn clients.RedigoClientConn)
13+
SetGoRedisClient(conn clients.GoRedisClientConn)
1614
}
1715

1816
// SetClientInactive resets the handler and unset any client, set to the handler
@@ -24,20 +22,20 @@ func (r *Handler) SetClientInactive() {
2422

2523
// SetRedigoClient sets Redigo (https://github.com/gomodule/redigo/redis) client
2624
// to the handler
27-
func (r *Handler) SetRedigoClient(conn redigo.Conn) {
25+
func (r *Handler) SetRedigoClient(conn clients.RedigoClientConn) {
2826
r.clientName = "redigo"
2927
r.implementation = &clients.Redigo{Conn: conn}
3028
}
3129

3230
// SetGoRedisClient sets Go-Redis (https://github.com/go-redis/redis) client to
3331
// the handler. It is left for backward compatibility.
34-
func (r *Handler) SetGoRedisClient(conn *goredis.Client) {
35-
r.SetGoRedisClientWithContext(context.TODO(), conn)
32+
func (r *Handler) SetGoRedisClient(conn clients.GoRedisClientConn) {
33+
r.SetGoRedisClientWithContext(context.Background(), conn)
3634
}
3735

3836
// SetGoRedisClientWithContext sets Go-Redis (https://github.com/go-redis/redis) client to
3937
// the handler with a global context for the connection
40-
func (r *Handler) SetGoRedisClientWithContext(ctx context.Context, conn *goredis.Client) {
38+
func (r *Handler) SetGoRedisClientWithContext(ctx context.Context, conn clients.GoRedisClientConn) {
4139
r.clientName = "goredis"
4240
r.implementation = clients.NewGoRedisClient(ctx, conn)
4341
}

0 commit comments

Comments
 (0)