Skip to content

Commit 37e0315

Browse files
committed
Merge branch 'cyberbeast-master'
2 parents a9d5a7f + 9d03b1d commit 37e0315

File tree

11 files changed

+284
-98
lines changed

11 files changed

+284
-98
lines changed

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Go-ReJSON - a golang client for ReJSON (a JSON data type for Redis)
2-
Go-ReJSON is a [Go](https://golang.org/) client for [ReJSON](https://github.com/RedisLabsModules/rejson) Redis Module.
2+
3+
Go-ReJSON is a [Go](https://golang.org/) client for [ReJSON](https://github.com/RedisLabsModules/rejson) Redis Module.
34

45
[![GoDoc](https://godoc.org/github.com/nitishm/go-rejson?status.svg)](https://godoc.org/github.com/nitishm/go-rejson)
56
[![Build Status](https://travis-ci.org/nitishm/go-rejson.svg?branch=master)](https://travis-ci.org/nitishm/go-rejson)
@@ -8,25 +9,26 @@ Go-ReJSON is a [Go](https://golang.org/) client for [ReJSON](https://github.com/
89

910
> ReJSON is a Redis module that implements ECMA-404 The JSON Data Interchange Standard as a native data type. It allows storing, updating and fetching JSON values from Redis keys (documents).
1011
11-
1212
Primary features of ReJSON Module:
13-
13+
1414
* Full support of the JSON standard
1515
* JSONPath-like syntax for selecting element inside documents
1616
* Documents are stored as binary data in a tree structure, allowing fast access to sub-elements
1717
* Typed atomic operations for all JSON values types
1818

19-
Each and every feature of ReJSON Module is fully incorporated in the project.
19+
Each and every feature of ReJSON Module is fully incorporated in the project.
2020

2121
Enjoy ReJSON with the type-safe Redis client, [`Go-Redis/Redis`](https://github.com/go-redis/redis) or use the print-like Redis-api client [`GoModule/Redigo`](https://github.com/gomodule/redigo).
2222
Go-ReJSON supports both the clients. Use any of the above two client you want, Go-ReJSON helps you out with all its features and functionalities in a more generic and standard way.
2323

2424
Support for `mediocregopher/radix` and other Redis clients is in our RoadMap. Any contributions on the support for other clients is hearty welcome.
2525

2626
## Installation
27-
go get github.com/nitishm/go-rejson
27+
28+
go get github.com/nitishm/go-rejson
2829

2930
## Example usage
31+
3032
```golang
3133
package main
3234

@@ -37,7 +39,7 @@ import (
3739
"log"
3840

3941
"github.com/nitishm/go-rejson"
40-
goredis "github.com/go-redis/redis/v7"
42+
goredis "github.com/go-redis/redis/v8"
4143
"github.com/gomodule/redigo/redis"
4244
)
4345

clients/goredis.go

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,33 @@
11
package clients
22

33
import (
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
1214
type 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
}

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+
}

examples/json_array/json_array.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
package main
22

33
import (
4+
"context"
45
"encoding/json"
56
"flag"
67
"fmt"
7-
"github.com/nitishm/go-rejson/rjs"
88
"log"
99

10-
goredis "github.com/go-redis/redis/v7"
10+
"github.com/nitishm/go-rejson/rjs"
11+
12+
goredis "github.com/go-redis/redis/v8"
1113
"github.com/gomodule/redigo/redis"
1214
"github.com/nitishm/go-rejson"
1315
)
1416

17+
var ctx = context.Background()
18+
1519
func Example_JSONArray(rh *rejson.Handler) {
1620
ArrIn := []string{"one", "two", "three", "four", "five"}
1721
res, err := rh.JSONSet("arr", ".", ArrIn)
@@ -158,7 +162,7 @@ func main() {
158162
// GoRedis Client
159163
cli := goredis.NewClient(&goredis.Options{Addr: *addr})
160164
defer func() {
161-
if err := cli.FlushAll().Err(); err != nil {
165+
if err := cli.FlushAll(ctx).Err(); err != nil {
162166
log.Fatalf("goredis - failed to flush: %v", err)
163167
}
164168
if err := cli.Close(); err != nil {

examples/json_obj/json_obj.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
package main
22

33
import (
4+
"context"
45
"encoding/json"
56
"flag"
67
"fmt"
8+
"log"
9+
710
"github.com/nitishm/go-rejson"
811
"github.com/nitishm/go-rejson/rjs"
9-
"log"
1012

11-
goredis "github.com/go-redis/redis/v7"
13+
goredis "github.com/go-redis/redis/v8"
1214
"github.com/gomodule/redigo/redis"
1315
)
1416

17+
var ctx = context.Background()
18+
1519
func Example_JSONObj(rh *rejson.Handler) {
1620

1721
type Object struct {
@@ -108,7 +112,7 @@ func main() {
108112
// GoRedis Client
109113
cli := goredis.NewClient(&goredis.Options{Addr: *addr})
110114
defer func() {
111-
if err := cli.FlushAll().Err(); err != nil {
115+
if err := cli.FlushAll(ctx).Err(); err != nil {
112116
log.Fatalf("goredis - failed to flush: %v", err)
113117
}
114118
if err := cli.Close(); err != nil {

examples/json_set/json_set.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
package main
22

33
import (
4+
"context"
45
"encoding/json"
56
"flag"
67
"fmt"
78
"log"
89

9-
goredis "github.com/go-redis/redis/v7"
10+
goredis "github.com/go-redis/redis/v8"
1011
"github.com/gomodule/redigo/redis"
1112
"github.com/nitishm/go-rejson"
1213
)
1314

15+
var ctx = context.Background()
16+
1417
// Name - student name
1518
type Name struct {
1619
First string `json:"first,omitempty"`
@@ -87,7 +90,7 @@ func main() {
8790
// GoRedis Client
8891
cli := goredis.NewClient(&goredis.Options{Addr: *addr})
8992
defer func() {
90-
if err := cli.FlushAll().Err(); err != nil {
93+
if err := cli.FlushAll(ctx).Err(); err != nil {
9194
log.Fatalf("goredis - failed to flush: %v", err)
9295
}
9396
if err := cli.Close(); err != nil {

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ module github.com/nitishm/go-rejson
33
go 1.14
44

55
require (
6-
github.com/go-redis/redis/v7 v7.4.0
7-
github.com/gomodule/redigo v1.8.2
6+
github.com/go-redis/redis/v8 v8.4.4
7+
github.com/gomodule/redigo v1.8.3
88
)

0 commit comments

Comments
 (0)