Skip to content

Commit 9d03b1d

Browse files
committed
feat: add SetContext support for go-redis without losing redigo
1 parent f8e7002 commit 9d03b1d

File tree

5 files changed

+182
-57
lines changed

5 files changed

+182
-57
lines changed

clients/goredis.go

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,25 @@ import (
99
"github.com/nitishm/go-rejson/rjs"
1010
)
1111

12-
var ctx = context.Background()
13-
1412
// GoRedis implements ReJSON interface for Go-Redis/Redis Redis client
1513
// Link: https://github.com/go-redis/redis
1614
type GoRedis struct {
1715
Conn *goredis.Client // import goredis "github.com/go-redis/redis/v8"
16+
17+
// ctx defines context for the provided connection
18+
ctx context.Context
19+
}
20+
21+
// NewGoRedisClient returns a new GoRedis ReJSON client with the provided context
22+
// and connection, if ctx is nil default context.Background will be used
23+
func NewGoRedisClient(ctx context.Context, conn *goredis.Client) *GoRedis {
24+
if ctx == nil {
25+
ctx = context.Background()
26+
}
27+
return &GoRedis{
28+
ctx: ctx,
29+
Conn: conn,
30+
}
1831
}
1932

2033
// JSONSet used to set a json object
@@ -39,7 +52,7 @@ func (r *GoRedis) JSONSet(key string, path string, obj interface{}, opts ...rjs.
3952
return nil, err
4053
}
4154
args = append([]interface{}{name}, args...)
42-
res, err = r.Conn.Do(ctx, args...).Result()
55+
res, err = r.Conn.Do(r.ctx, args...).Result()
4356

4457
if err != nil && err.Error() == rjs.ErrGoRedisNil.Error() {
4558
err = nil
@@ -75,7 +88,7 @@ func (r *GoRedis) JSONGet(key, path string, opts ...rjs.GetOption) (res interfac
7588
}
7689

7790
args = append([]interface{}{name}, args...)
78-
res, err = r.Conn.Do(ctx, args...).Result()
91+
res, err = r.Conn.Do(r.ctx, args...).Result()
7992
if err != nil {
8093
return
8194
}
@@ -103,7 +116,7 @@ func (r *GoRedis) JSONMGet(path string, keys ...string) (res interface{}, err er
103116
return nil, err
104117
}
105118
args = append([]interface{}{name}, args...)
106-
res, err = r.Conn.Do(ctx, args...).Result()
119+
res, err = r.Conn.Do(r.ctx, args...).Result()
107120
if err != nil {
108121
return
109122
}
@@ -131,7 +144,7 @@ func (r *GoRedis) JSONDel(key string, path string) (res interface{}, err error)
131144
return nil, err
132145
}
133146
args = append([]interface{}{name}, args...)
134-
return r.Conn.Do(ctx, args...).Result()
147+
return r.Conn.Do(r.ctx, args...).Result()
135148
}
136149

137150
// JSONType to get the type of key or member at path.
@@ -146,7 +159,7 @@ func (r *GoRedis) JSONType(key, path string) (res interface{}, err error) {
146159
return nil, err
147160
}
148161
args = append([]interface{}{name}, args...)
149-
res, err = r.Conn.Do(ctx, args...).Result()
162+
res, err = r.Conn.Do(r.ctx, args...).Result()
150163

151164
if err != nil && err.Error() == rjs.ErrGoRedisNil.Error() {
152165
err = nil
@@ -166,7 +179,7 @@ func (r *GoRedis) JSONNumIncrBy(key, path string, number int) (res interface{},
166179
return nil, err
167180
}
168181
args = append([]interface{}{name}, args...)
169-
res, err = r.Conn.Do(ctx, args...).Result()
182+
res, err = r.Conn.Do(r.ctx, args...).Result()
170183
if err != nil {
171184
return
172185
}
@@ -185,7 +198,7 @@ func (r *GoRedis) JSONNumMultBy(key, path string, number int) (res interface{},
185198
return nil, err
186199
}
187200
args = append([]interface{}{name}, args...)
188-
res, err = r.Conn.Do(ctx, args...).Result()
201+
res, err = r.Conn.Do(r.ctx, args...).Result()
189202
if err != nil {
190203
return
191204
}
@@ -204,7 +217,7 @@ func (r *GoRedis) JSONStrAppend(key, path, jsonstring string) (res interface{},
204217
return nil, err
205218
}
206219
args = append([]interface{}{name}, args...)
207-
return r.Conn.Do(ctx, args...).Result()
220+
return r.Conn.Do(r.ctx, args...).Result()
208221
}
209222

210223
// JSONStrLen to return the length of a string member
@@ -219,7 +232,7 @@ func (r *GoRedis) JSONStrLen(key, path string) (res interface{}, err error) {
219232
return nil, err
220233
}
221234
args = append([]interface{}{name}, args...)
222-
return r.Conn.Do(ctx, args...).Result()
235+
return r.Conn.Do(r.ctx, args...).Result()
223236
}
224237

225238
// JSONArrAppend to append json value into array at path
@@ -241,7 +254,7 @@ func (r *GoRedis) JSONArrAppend(key, path string, values ...interface{}) (res in
241254
return nil, err
242255
}
243256
args = append([]interface{}{name}, args...)
244-
return r.Conn.Do(ctx, args...).Result()
257+
return r.Conn.Do(r.ctx, args...).Result()
245258
}
246259

247260
// JSONArrLen returns the length of the json array at path
@@ -256,7 +269,7 @@ func (r *GoRedis) JSONArrLen(key, path string) (res interface{}, err error) {
256269
return nil, err
257270
}
258271
args = append([]interface{}{name}, args...)
259-
return r.Conn.Do(ctx, args...).Result()
272+
return r.Conn.Do(r.ctx, args...).Result()
260273
}
261274

262275
// JSONArrPop removes and returns element from the index in the array
@@ -273,7 +286,7 @@ func (r *GoRedis) JSONArrPop(key, path string, index int) (res interface{}, err
273286
}
274287
args = append([]interface{}{name}, args...)
275288

276-
res, err = r.Conn.Do(ctx, args...).Result()
289+
res, err = r.Conn.Do(r.ctx, args...).Result()
277290
if err != nil {
278291
return
279292
}
@@ -303,7 +316,7 @@ func (r *GoRedis) JSONArrIndex(key, path string, jsonValue interface{}, optional
303316
return nil, err
304317
}
305318
args = append([]interface{}{name}, args...)
306-
return r.Conn.Do(ctx, args...).Result()
319+
return r.Conn.Do(r.ctx, args...).Result()
307320
}
308321

309322
// JSONArrTrim trims an array so that it contains only the specified inclusive range of elements
@@ -318,7 +331,7 @@ func (r *GoRedis) JSONArrTrim(key, path string, start, end int) (res interface{}
318331
return nil, err
319332
}
320333
args = append([]interface{}{name}, args...)
321-
return r.Conn.Do(ctx, args...).Result()
334+
return r.Conn.Do(r.ctx, args...).Result()
322335
}
323336

324337
// JSONArrInsert inserts the json value(s) into the array at path before the index (shifts to the right).
@@ -340,7 +353,7 @@ func (r *GoRedis) JSONArrInsert(key, path string, index int, values ...interface
340353
return nil, err
341354
}
342355
args = append([]interface{}{name}, args...)
343-
return r.Conn.Do(ctx, args...).Result()
356+
return r.Conn.Do(r.ctx, args...).Result()
344357
}
345358

346359
// JSONObjKeys returns the keys in the object that's referenced by path
@@ -355,7 +368,7 @@ func (r *GoRedis) JSONObjKeys(key, path string) (res interface{}, err error) {
355368
return nil, err
356369
}
357370
args = append([]interface{}{name}, args...)
358-
res, err = r.Conn.Do(ctx, args...).Result()
371+
res, err = r.Conn.Do(r.ctx, args...).Result()
359372
if err != nil {
360373
return
361374
}
@@ -380,7 +393,7 @@ func (r *GoRedis) JSONObjLen(key, path string) (res interface{}, err error) {
380393
return nil, err
381394
}
382395
args = append([]interface{}{name}, args...)
383-
return r.Conn.Do(ctx, args...).Result()
396+
return r.Conn.Do(r.ctx, args...).Result()
384397
}
385398

386399
// JSONDebug reports information
@@ -401,7 +414,7 @@ func (r *GoRedis) JSONDebug(subcommand rjs.DebugSubCommand, key, path string) (r
401414
return nil, err
402415
}
403416
args = append([]interface{}{name}, args...)
404-
res, err = r.Conn.Do(ctx, args...).Result()
417+
res, err = r.Conn.Do(r.ctx, args...).Result()
405418
if err != nil {
406419
return
407420
}
@@ -430,7 +443,7 @@ func (r *GoRedis) JSONForget(key, path string) (res interface{}, err error) {
430443
return nil, err
431444
}
432445
args = append([]interface{}{name}, args...)
433-
return r.Conn.Do(ctx, args...).Result()
446+
return r.Conn.Do(r.ctx, args...).Result()
434447
}
435448

436449
// JSONResp returns the JSON in key in Redis Serialization Protocol (RESP).
@@ -445,5 +458,5 @@ func (r *GoRedis) JSONResp(key, path string) (res interface{}, err error) {
445458
return nil, err
446459
}
447460
args = append([]interface{}{name}, args...)
448-
return r.Conn.Do(ctx, args...).Result()
461+
return r.Conn.Do(r.ctx, args...).Result()
449462
}

context.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package rejson
2+
3+
import (
4+
"context"
5+
"github.com/nitishm/go-rejson/clients"
6+
"github.com/nitishm/go-rejson/rjs"
7+
)
8+
9+
// SetContext helps redis-clients, provide use of command level context
10+
// in the ReJSON commands.
11+
// Currently, only go-redis@v8 supports command level context, therefore
12+
// a separate method is added to support it, maintaining the support for
13+
// other clients and for backward compatibility. (nitishm/go-rejson#46)
14+
func (r *Handler) SetContext(ctx context.Context) *Handler {
15+
if r == nil {
16+
return r // nil
17+
}
18+
19+
if r.clientName == rjs.ClientGoRedis {
20+
if old, ok := r.implementation.(*clients.GoRedis); ok {
21+
return &Handler{
22+
clientName: r.clientName,
23+
implementation: clients.NewGoRedisClient(ctx, old.Conn),
24+
}
25+
}
26+
}
27+
28+
// for other clients, context is of no use, hence return same
29+
return r
30+
}

0 commit comments

Comments
 (0)