11package clients
22
33import (
4+ "context"
45 "fmt"
5- goredis "github.com/go-redis/redis/v7"
6- "github.com/nitishm/go-rejson/rjs"
76 "strings"
7+
8+ goredis "github.com/go-redis/redis/v8"
9+ "github.com/nitishm/go-rejson/rjs"
810)
911
1012// GoRedis implements ReJSON interface for Go-Redis/Redis Redis client
1113// Link: https://github.com/go-redis/redis
1214type GoRedis struct {
13- Conn * goredis.Client // import goredis "github.com/go-redis/redis/v7"
15+ 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+ }
1431}
1532
1633// JSONSet used to set a json object
@@ -35,7 +52,7 @@ func (r *GoRedis) JSONSet(key string, path string, obj interface{}, opts ...rjs.
3552 return nil , err
3653 }
3754 args = append ([]interface {}{name }, args ... )
38- res , err = r .Conn .Do (args ... ).Result ()
55+ res , err = r .Conn .Do (r . ctx , args ... ).Result ()
3956
4057 if err != nil && err .Error () == rjs .ErrGoRedisNil .Error () {
4158 err = nil
@@ -71,7 +88,7 @@ func (r *GoRedis) JSONGet(key, path string, opts ...rjs.GetOption) (res interfac
7188 }
7289
7390 args = append ([]interface {}{name }, args ... )
74- res , err = r .Conn .Do (args ... ).Result ()
91+ res , err = r .Conn .Do (r . ctx , args ... ).Result ()
7592 if err != nil {
7693 return
7794 }
@@ -99,7 +116,7 @@ func (r *GoRedis) JSONMGet(path string, keys ...string) (res interface{}, err er
99116 return nil , err
100117 }
101118 args = append ([]interface {}{name }, args ... )
102- res , err = r .Conn .Do (args ... ).Result ()
119+ res , err = r .Conn .Do (r . ctx , args ... ).Result ()
103120 if err != nil {
104121 return
105122 }
@@ -127,7 +144,7 @@ func (r *GoRedis) JSONDel(key string, path string) (res interface{}, err error)
127144 return nil , err
128145 }
129146 args = append ([]interface {}{name }, args ... )
130- return r .Conn .Do (args ... ).Result ()
147+ return r .Conn .Do (r . ctx , args ... ).Result ()
131148}
132149
133150// JSONType to get the type of key or member at path.
@@ -142,7 +159,7 @@ func (r *GoRedis) JSONType(key, path string) (res interface{}, err error) {
142159 return nil , err
143160 }
144161 args = append ([]interface {}{name }, args ... )
145- res , err = r .Conn .Do (args ... ).Result ()
162+ res , err = r .Conn .Do (r . ctx , args ... ).Result ()
146163
147164 if err != nil && err .Error () == rjs .ErrGoRedisNil .Error () {
148165 err = nil
@@ -162,7 +179,7 @@ func (r *GoRedis) JSONNumIncrBy(key, path string, number int) (res interface{},
162179 return nil , err
163180 }
164181 args = append ([]interface {}{name }, args ... )
165- res , err = r .Conn .Do (args ... ).Result ()
182+ res , err = r .Conn .Do (r . ctx , args ... ).Result ()
166183 if err != nil {
167184 return
168185 }
@@ -181,7 +198,7 @@ func (r *GoRedis) JSONNumMultBy(key, path string, number int) (res interface{},
181198 return nil , err
182199 }
183200 args = append ([]interface {}{name }, args ... )
184- res , err = r .Conn .Do (args ... ).Result ()
201+ res , err = r .Conn .Do (r . ctx , args ... ).Result ()
185202 if err != nil {
186203 return
187204 }
@@ -200,7 +217,7 @@ func (r *GoRedis) JSONStrAppend(key, path, jsonstring string) (res interface{},
200217 return nil , err
201218 }
202219 args = append ([]interface {}{name }, args ... )
203- return r .Conn .Do (args ... ).Result ()
220+ return r .Conn .Do (r . ctx , args ... ).Result ()
204221}
205222
206223// JSONStrLen to return the length of a string member
@@ -215,7 +232,7 @@ func (r *GoRedis) JSONStrLen(key, path string) (res interface{}, err error) {
215232 return nil , err
216233 }
217234 args = append ([]interface {}{name }, args ... )
218- return r .Conn .Do (args ... ).Result ()
235+ return r .Conn .Do (r . ctx , args ... ).Result ()
219236}
220237
221238// JSONArrAppend to append json value into array at path
@@ -237,7 +254,7 @@ func (r *GoRedis) JSONArrAppend(key, path string, values ...interface{}) (res in
237254 return nil , err
238255 }
239256 args = append ([]interface {}{name }, args ... )
240- return r .Conn .Do (args ... ).Result ()
257+ return r .Conn .Do (r . ctx , args ... ).Result ()
241258}
242259
243260// JSONArrLen returns the length of the json array at path
@@ -252,7 +269,7 @@ func (r *GoRedis) JSONArrLen(key, path string) (res interface{}, err error) {
252269 return nil , err
253270 }
254271 args = append ([]interface {}{name }, args ... )
255- return r .Conn .Do (args ... ).Result ()
272+ return r .Conn .Do (r . ctx , args ... ).Result ()
256273}
257274
258275// JSONArrPop removes and returns element from the index in the array
@@ -269,7 +286,7 @@ func (r *GoRedis) JSONArrPop(key, path string, index int) (res interface{}, err
269286 }
270287 args = append ([]interface {}{name }, args ... )
271288
272- res , err = r .Conn .Do (args ... ).Result ()
289+ res , err = r .Conn .Do (r . ctx , args ... ).Result ()
273290 if err != nil {
274291 return
275292 }
@@ -299,7 +316,7 @@ func (r *GoRedis) JSONArrIndex(key, path string, jsonValue interface{}, optional
299316 return nil , err
300317 }
301318 args = append ([]interface {}{name }, args ... )
302- return r .Conn .Do (args ... ).Result ()
319+ return r .Conn .Do (r . ctx , args ... ).Result ()
303320}
304321
305322// JSONArrTrim trims an array so that it contains only the specified inclusive range of elements
@@ -314,7 +331,7 @@ func (r *GoRedis) JSONArrTrim(key, path string, start, end int) (res interface{}
314331 return nil , err
315332 }
316333 args = append ([]interface {}{name }, args ... )
317- return r .Conn .Do (args ... ).Result ()
334+ return r .Conn .Do (r . ctx , args ... ).Result ()
318335}
319336
320337// JSONArrInsert inserts the json value(s) into the array at path before the index (shifts to the right).
@@ -336,7 +353,7 @@ func (r *GoRedis) JSONArrInsert(key, path string, index int, values ...interface
336353 return nil , err
337354 }
338355 args = append ([]interface {}{name }, args ... )
339- return r .Conn .Do (args ... ).Result ()
356+ return r .Conn .Do (r . ctx , args ... ).Result ()
340357}
341358
342359// JSONObjKeys returns the keys in the object that's referenced by path
@@ -351,7 +368,7 @@ func (r *GoRedis) JSONObjKeys(key, path string) (res interface{}, err error) {
351368 return nil , err
352369 }
353370 args = append ([]interface {}{name }, args ... )
354- res , err = r .Conn .Do (args ... ).Result ()
371+ res , err = r .Conn .Do (r . ctx , args ... ).Result ()
355372 if err != nil {
356373 return
357374 }
@@ -376,7 +393,7 @@ func (r *GoRedis) JSONObjLen(key, path string) (res interface{}, err error) {
376393 return nil , err
377394 }
378395 args = append ([]interface {}{name }, args ... )
379- return r .Conn .Do (args ... ).Result ()
396+ return r .Conn .Do (r . ctx , args ... ).Result ()
380397}
381398
382399// JSONDebug reports information
@@ -397,7 +414,7 @@ func (r *GoRedis) JSONDebug(subcommand rjs.DebugSubCommand, key, path string) (r
397414 return nil , err
398415 }
399416 args = append ([]interface {}{name }, args ... )
400- res , err = r .Conn .Do (args ... ).Result ()
417+ res , err = r .Conn .Do (r . ctx , args ... ).Result ()
401418 if err != nil {
402419 return
403420 }
@@ -426,7 +443,7 @@ func (r *GoRedis) JSONForget(key, path string) (res interface{}, err error) {
426443 return nil , err
427444 }
428445 args = append ([]interface {}{name }, args ... )
429- return r .Conn .Do (args ... ).Result ()
446+ return r .Conn .Do (r . ctx , args ... ).Result ()
430447}
431448
432449// JSONResp returns the JSON in key in Redis Serialization Protocol (RESP).
@@ -441,5 +458,5 @@ func (r *GoRedis) JSONResp(key, path string) (res interface{}, err error) {
441458 return nil , err
442459 }
443460 args = append ([]interface {}{name }, args ... )
444- return r .Conn .Do (args ... ).Result ()
461+ return r .Conn .Do (r . ctx , args ... ).Result ()
445462}
0 commit comments