From f8e7002b63f87eb05f794784298a4d66ac21a12d Mon Sep 17 00:00:00 2001 From: Sandesh Gade Date: Sun, 20 Dec 2020 06:35:02 -0500 Subject: [PATCH 1/2] bump go-redis version to v8, update tests, deps, examples --- README.md | 14 ++--- clients/goredis.go | 50 +++++++++--------- examples/json_array/json_array.go | 10 ++-- examples/json_obj/json_obj.go | 10 ++-- examples/json_set/json_set.go | 7 ++- go.mod | 4 +- go.sum | 86 ++++++++++++++++++++++--------- rejson_test.go | 10 ++-- set_client.go | 2 +- 9 files changed, 127 insertions(+), 66 deletions(-) diff --git a/README.md b/README.md index b1af83f..165b0ed 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ # Go-ReJSON - a golang client for ReJSON (a JSON data type for Redis) -Go-ReJSON is a [Go](https://golang.org/) client for [ReJSON](https://github.com/RedisLabsModules/rejson) Redis Module. + +Go-ReJSON is a [Go](https://golang.org/) client for [ReJSON](https://github.com/RedisLabsModules/rejson) Redis Module. [![GoDoc](https://godoc.org/github.com/nitishm/go-rejson?status.svg)](https://godoc.org/github.com/nitishm/go-rejson) [![Build Status](https://travis-ci.org/nitishm/go-rejson.svg?branch=master)](https://travis-ci.org/nitishm/go-rejson) @@ -8,15 +9,14 @@ Go-ReJSON is a [Go](https://golang.org/) client for [ReJSON](https://github.com/ > 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). - Primary features of ReJSON Module: - + * Full support of the JSON standard * JSONPath-like syntax for selecting element inside documents * Documents are stored as binary data in a tree structure, allowing fast access to sub-elements * Typed atomic operations for all JSON values types -Each and every feature of ReJSON Module is fully incorporated in the project. +Each and every feature of ReJSON Module is fully incorporated in the project. 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). 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. @@ -24,9 +24,11 @@ Go-ReJSON supports both the clients. Use any of the above two client you want, G Support for `mediocregopher/radix` and other Redis clients is in our RoadMap. Any contributions on the support for other clients is hearty welcome. ## Installation - go get github.com/nitishm/go-rejson + + go get github.com/nitishm/go-rejson ## Example usage + ```golang package main @@ -37,7 +39,7 @@ import ( "log" "github.com/nitishm/go-rejson" - goredis "github.com/go-redis/redis/v7" + goredis "github.com/go-redis/redis/v8" "github.com/gomodule/redigo/redis" ) diff --git a/clients/goredis.go b/clients/goredis.go index e052507..267e443 100644 --- a/clients/goredis.go +++ b/clients/goredis.go @@ -1,16 +1,20 @@ package clients import ( + "context" "fmt" - goredis "github.com/go-redis/redis/v7" - "github.com/nitishm/go-rejson/rjs" "strings" + + goredis "github.com/go-redis/redis/v8" + "github.com/nitishm/go-rejson/rjs" ) +var ctx = context.Background() + // 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/v7" + Conn *goredis.Client // import goredis "github.com/go-redis/redis/v8" } // JSONSet used to set a json object @@ -35,7 +39,7 @@ func (r *GoRedis) JSONSet(key string, path string, obj interface{}, opts ...rjs. return nil, err } args = append([]interface{}{name}, args...) - res, err = r.Conn.Do(args...).Result() + res, err = r.Conn.Do(ctx, args...).Result() if err != nil && err.Error() == rjs.ErrGoRedisNil.Error() { err = nil @@ -71,7 +75,7 @@ func (r *GoRedis) JSONGet(key, path string, opts ...rjs.GetOption) (res interfac } args = append([]interface{}{name}, args...) - res, err = r.Conn.Do(args...).Result() + res, err = r.Conn.Do(ctx, args...).Result() if err != nil { return } @@ -99,7 +103,7 @@ func (r *GoRedis) JSONMGet(path string, keys ...string) (res interface{}, err er return nil, err } args = append([]interface{}{name}, args...) - res, err = r.Conn.Do(args...).Result() + res, err = r.Conn.Do(ctx, args...).Result() if err != nil { return } @@ -127,7 +131,7 @@ func (r *GoRedis) JSONDel(key string, path string) (res interface{}, err error) return nil, err } args = append([]interface{}{name}, args...) - return r.Conn.Do(args...).Result() + return r.Conn.Do(ctx, args...).Result() } // JSONType to get the type of key or member at path. @@ -142,7 +146,7 @@ func (r *GoRedis) JSONType(key, path string) (res interface{}, err error) { return nil, err } args = append([]interface{}{name}, args...) - res, err = r.Conn.Do(args...).Result() + res, err = r.Conn.Do(ctx, args...).Result() if err != nil && err.Error() == rjs.ErrGoRedisNil.Error() { err = nil @@ -162,7 +166,7 @@ func (r *GoRedis) JSONNumIncrBy(key, path string, number int) (res interface{}, return nil, err } args = append([]interface{}{name}, args...) - res, err = r.Conn.Do(args...).Result() + res, err = r.Conn.Do(ctx, args...).Result() if err != nil { return } @@ -181,7 +185,7 @@ func (r *GoRedis) JSONNumMultBy(key, path string, number int) (res interface{}, return nil, err } args = append([]interface{}{name}, args...) - res, err = r.Conn.Do(args...).Result() + res, err = r.Conn.Do(ctx, args...).Result() if err != nil { return } @@ -200,7 +204,7 @@ func (r *GoRedis) JSONStrAppend(key, path, jsonstring string) (res interface{}, return nil, err } args = append([]interface{}{name}, args...) - return r.Conn.Do(args...).Result() + return r.Conn.Do(ctx, args...).Result() } // JSONStrLen to return the length of a string member @@ -215,7 +219,7 @@ func (r *GoRedis) JSONStrLen(key, path string) (res interface{}, err error) { return nil, err } args = append([]interface{}{name}, args...) - return r.Conn.Do(args...).Result() + return r.Conn.Do(ctx, args...).Result() } // JSONArrAppend to append json value into array at path @@ -237,7 +241,7 @@ func (r *GoRedis) JSONArrAppend(key, path string, values ...interface{}) (res in return nil, err } args = append([]interface{}{name}, args...) - return r.Conn.Do(args...).Result() + return r.Conn.Do(ctx, args...).Result() } // JSONArrLen returns the length of the json array at path @@ -252,7 +256,7 @@ func (r *GoRedis) JSONArrLen(key, path string) (res interface{}, err error) { return nil, err } args = append([]interface{}{name}, args...) - return r.Conn.Do(args...).Result() + return r.Conn.Do(ctx, args...).Result() } // JSONArrPop removes and returns element from the index in the array @@ -269,7 +273,7 @@ func (r *GoRedis) JSONArrPop(key, path string, index int) (res interface{}, err } args = append([]interface{}{name}, args...) - res, err = r.Conn.Do(args...).Result() + res, err = r.Conn.Do(ctx, args...).Result() if err != nil { return } @@ -299,7 +303,7 @@ func (r *GoRedis) JSONArrIndex(key, path string, jsonValue interface{}, optional return nil, err } args = append([]interface{}{name}, args...) - return r.Conn.Do(args...).Result() + return r.Conn.Do(ctx, args...).Result() } // JSONArrTrim trims an array so that it contains only the specified inclusive range of elements @@ -314,7 +318,7 @@ func (r *GoRedis) JSONArrTrim(key, path string, start, end int) (res interface{} return nil, err } args = append([]interface{}{name}, args...) - return r.Conn.Do(args...).Result() + return r.Conn.Do(ctx, args...).Result() } // JSONArrInsert inserts the json value(s) into the array at path before the index (shifts to the right). @@ -336,7 +340,7 @@ func (r *GoRedis) JSONArrInsert(key, path string, index int, values ...interface return nil, err } args = append([]interface{}{name}, args...) - return r.Conn.Do(args...).Result() + return r.Conn.Do(ctx, args...).Result() } // JSONObjKeys returns the keys in the object that's referenced by path @@ -351,7 +355,7 @@ func (r *GoRedis) JSONObjKeys(key, path string) (res interface{}, err error) { return nil, err } args = append([]interface{}{name}, args...) - res, err = r.Conn.Do(args...).Result() + res, err = r.Conn.Do(ctx, args...).Result() if err != nil { return } @@ -376,7 +380,7 @@ func (r *GoRedis) JSONObjLen(key, path string) (res interface{}, err error) { return nil, err } args = append([]interface{}{name}, args...) - return r.Conn.Do(args...).Result() + return r.Conn.Do(ctx, args...).Result() } // JSONDebug reports information @@ -397,7 +401,7 @@ func (r *GoRedis) JSONDebug(subcommand rjs.DebugSubCommand, key, path string) (r return nil, err } args = append([]interface{}{name}, args...) - res, err = r.Conn.Do(args...).Result() + res, err = r.Conn.Do(ctx, args...).Result() if err != nil { return } @@ -426,7 +430,7 @@ func (r *GoRedis) JSONForget(key, path string) (res interface{}, err error) { return nil, err } args = append([]interface{}{name}, args...) - return r.Conn.Do(args...).Result() + return r.Conn.Do(ctx, args...).Result() } // JSONResp returns the JSON in key in Redis Serialization Protocol (RESP). @@ -441,5 +445,5 @@ func (r *GoRedis) JSONResp(key, path string) (res interface{}, err error) { return nil, err } args = append([]interface{}{name}, args...) - return r.Conn.Do(args...).Result() + return r.Conn.Do(ctx, args...).Result() } diff --git a/examples/json_array/json_array.go b/examples/json_array/json_array.go index 8b16eb5..6a03b81 100644 --- a/examples/json_array/json_array.go +++ b/examples/json_array/json_array.go @@ -1,17 +1,21 @@ package main import ( + "context" "encoding/json" "flag" "fmt" - "github.com/nitishm/go-rejson/rjs" "log" - goredis "github.com/go-redis/redis/v7" + "github.com/nitishm/go-rejson/rjs" + + goredis "github.com/go-redis/redis/v8" "github.com/gomodule/redigo/redis" "github.com/nitishm/go-rejson" ) +var ctx = context.Background() + func Example_JSONArray(rh *rejson.Handler) { ArrIn := []string{"one", "two", "three", "four", "five"} res, err := rh.JSONSet("arr", ".", ArrIn) @@ -158,7 +162,7 @@ func main() { // GoRedis Client cli := goredis.NewClient(&goredis.Options{Addr: *addr}) defer func() { - if err := cli.FlushAll().Err(); err != nil { + if err := cli.FlushAll(ctx).Err(); err != nil { log.Fatalf("goredis - failed to flush: %v", err) } if err := cli.Close(); err != nil { diff --git a/examples/json_obj/json_obj.go b/examples/json_obj/json_obj.go index 29cec6d..6d76c29 100644 --- a/examples/json_obj/json_obj.go +++ b/examples/json_obj/json_obj.go @@ -1,17 +1,21 @@ package main import ( + "context" "encoding/json" "flag" "fmt" + "log" + "github.com/nitishm/go-rejson" "github.com/nitishm/go-rejson/rjs" - "log" - goredis "github.com/go-redis/redis/v7" + goredis "github.com/go-redis/redis/v8" "github.com/gomodule/redigo/redis" ) +var ctx = context.Background() + func Example_JSONObj(rh *rejson.Handler) { type Object struct { @@ -108,7 +112,7 @@ func main() { // GoRedis Client cli := goredis.NewClient(&goredis.Options{Addr: *addr}) defer func() { - if err := cli.FlushAll().Err(); err != nil { + if err := cli.FlushAll(ctx).Err(); err != nil { log.Fatalf("goredis - failed to flush: %v", err) } if err := cli.Close(); err != nil { diff --git a/examples/json_set/json_set.go b/examples/json_set/json_set.go index 6b3bec5..a81729e 100644 --- a/examples/json_set/json_set.go +++ b/examples/json_set/json_set.go @@ -1,16 +1,19 @@ package main import ( + "context" "encoding/json" "flag" "fmt" "log" - goredis "github.com/go-redis/redis/v7" + goredis "github.com/go-redis/redis/v8" "github.com/gomodule/redigo/redis" "github.com/nitishm/go-rejson" ) +var ctx = context.Background() + // Name - student name type Name struct { First string `json:"first,omitempty"` @@ -87,7 +90,7 @@ func main() { // GoRedis Client cli := goredis.NewClient(&goredis.Options{Addr: *addr}) defer func() { - if err := cli.FlushAll().Err(); err != nil { + if err := cli.FlushAll(ctx).Err(); err != nil { log.Fatalf("goredis - failed to flush: %v", err) } if err := cli.Close(); err != nil { diff --git a/go.mod b/go.mod index 3b05334..41e7194 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,6 @@ module github.com/nitishm/go-rejson go 1.14 require ( - github.com/go-redis/redis/v7 v7.4.0 - github.com/gomodule/redigo v1.8.2 + github.com/go-redis/redis/v8 v8.4.4 + github.com/gomodule/redigo v1.8.3 ) diff --git a/go.sum b/go.sum index e42f965..47660c5 100644 --- a/go.sum +++ b/go.sum @@ -1,59 +1,99 @@ +github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY= +github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= -github.com/go-redis/redis/v7 v7.4.0 h1:7obg6wUoj05T0EpY0o8B59S9w5yeMWql7sw2kwNW1x4= -github.com/go-redis/redis/v7 v7.4.0/go.mod h1:JDNMw23GTyLNC4GZu9njt15ctBQVn7xjRfnwdHj/Dcg= +github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= +github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= +github.com/go-redis/redis/v8 v8.4.4 h1:fGqgxCTR1sydaKI00oQf3OmkU/DIe/I/fYXvGklCIuc= +github.com/go-redis/redis/v8 v8.4.4/go.mod h1:nA0bQuF0i5JFx4Ta9RZxGKXFrQ8cRWntra97f0196iY= github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs= -github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/gomodule/redigo v1.8.2 h1:H5XSIre1MB5NbPYFp+i1NBbb5qN1W8Y8YAQoAYbkm8k= -github.com/gomodule/redigo v1.8.2/go.mod h1:P9dn9mFrCBvWhGE1wpxx6fgq7BAeLBk+UUUzlpkBYO0= +github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= +github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= +github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= +github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= +github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= +github.com/golang/protobuf v1.4.2 h1:+Z5KGCizgyZCbGh1KZqA0fcLLkwbsjIzS4aV2v7wJX0= +github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/gomodule/redigo v1.8.3 h1:HR0kYDX2RJZvAup8CsiJwxB4dTCSC0AaUq6S4SiLwUc= +github.com/gomodule/redigo v1.8.3/go.mod h1:P9dn9mFrCBvWhGE1wpxx6fgq7BAeLBk+UUUzlpkBYO0= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.4 h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M= +github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= -github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= -github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= -github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= -github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/nxadm/tail v1.4.4 h1:DQuhQpB1tVlglWS2hLQ5OV6B5r8aGxSrPc5Qo6uTN78= +github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= -github.com/onsi/ginkgo v1.10.1 h1:q/mM8GF/n0shIN8SaAZ0V+jnLPzen6WIVZdiwrRlMlo= -github.com/onsi/ginkgo v1.10.1/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= -github.com/onsi/gomega v1.7.0 h1:XPnZz8VVBHjVsy1vzJmRwIcSwiUO+JFfrv/xGiigmME= -github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= +github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk= +github.com/onsi/ginkgo v1.14.2 h1:8mVmC9kjFFmA8H4pKMUhcblgifdkOIXPvbhN1T36q1M= +github.com/onsi/ginkgo v1.14.2/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9klQyY= +github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= +github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= +github.com/onsi/gomega v1.10.4 h1:NiTx7EEvBzu9sFOD1zORteLSt3o8gnlvZZwSE9TnY9U= +github.com/onsi/gomega v1.10.4/go.mod h1:g/HbgYopi++010VEqkFgJHKC09uJiW9UkXvMUuKHUCQ= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +go.opentelemetry.io/otel v0.15.0 h1:CZFy2lPhxd4HlhZnYK8gRyDotksO3Ip9rBweY1vVYJw= +go.opentelemetry.io/otel v0.15.0/go.mod h1:e4GKElweB8W2gWUqbghw0B8t5MCTccc9212eNHnOHwA= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd h1:nTDtHvHSdCn1m6ITfMRqtOd/9+7a3s8RBNOZ3eYZzJA= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20190923162816-aa69164e4478 h1:l5EDrHhldLYb3ZRHDUhXF7Om7MvYXnkV9/iQNo1lX6g= -golang.org/x/net v0.0.0-20190923162816-aa69164e4478/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= +golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb h1:eBmm0M9fYhWpKZLjQUUKka/LtIxf46G4fxeEz5KJr9U= +golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e h1:o3PsSEY8E4eXWkXrIP9YJALUkVZqzHJT5DOasTyn8Vs= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20191010194322-b09406accb47 h1:/XfQ9z7ib8eEJX2hdgFTZJ/ntt0swNk5oYBziWeTCvY= -golang.org/x/sys v0.0.0-20191010194322-b09406accb47/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f h1:+Nyd8tzPX9R7BWHguqsrbFdRx3WQ/1ib8I44HXV5yTA= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= +google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= +google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= +google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= +google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= +google.golang.org/protobuf v1.23.0 h1:4MY060fB1DLGMB/7MBTLnwQUY6+F09GEiz6SsrNqyzM= +google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= -gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= -gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE= -gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= +gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/rejson_test.go b/rejson_test.go index 0cc82f5..8965cda 100644 --- a/rejson_test.go +++ b/rejson_test.go @@ -1,15 +1,19 @@ package rejson import ( + "context" "encoding/json" - "github.com/nitishm/go-rejson/rjs" "reflect" "testing" - goredis "github.com/go-redis/redis/v7" + "github.com/nitishm/go-rejson/rjs" + + goredis "github.com/go-redis/redis/v8" redigo "github.com/gomodule/redigo/redis" ) +var ctx = context.Background() + func TestUnsupportedCommand(t *testing.T) { _, _, err := rjs.CommandBuilder(1234, nil) if err == nil { @@ -77,7 +81,7 @@ func TestReJSON(t *testing.T) { } }}, {cli: goredisCli, name: "GoRedis ", closeFunc: func() { - if err := goredisCli.FlushAll().Err(); err != nil { + if err := goredisCli.FlushAll(ctx).Err(); err != nil { t.Fatalf("goredis - failed to flush: %v", err) } if err := goredisCli.Close(); err != nil { diff --git a/set_client.go b/set_client.go index 0622481..9edba97 100644 --- a/set_client.go +++ b/set_client.go @@ -1,7 +1,7 @@ package rejson import ( - goredis "github.com/go-redis/redis/v7" + goredis "github.com/go-redis/redis/v8" redigo "github.com/gomodule/redigo/redis" "github.com/nitishm/go-rejson/clients" "github.com/nitishm/go-rejson/rjs" From 9d03b1d8c164b50d653be6f5464e2858f9240b07 Mon Sep 17 00:00:00 2001 From: Shivam Rathore Date: Sat, 26 Dec 2020 22:50:49 +0530 Subject: [PATCH 2/2] feat: add SetContext support for go-redis without losing redigo --- clients/goredis.go | 57 +++++++++++-------- context.go | 30 ++++++++++ rejson_test.go | 135 ++++++++++++++++++++++++++++++++++----------- rjs/constants.go | 6 ++ set_client.go | 11 +++- 5 files changed, 182 insertions(+), 57 deletions(-) create mode 100644 context.go diff --git a/clients/goredis.go b/clients/goredis.go index 267e443..f7cfb63 100644 --- a/clients/goredis.go +++ b/clients/goredis.go @@ -9,12 +9,25 @@ import ( "github.com/nitishm/go-rejson/rjs" ) -var ctx = context.Background() - // 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" + + // 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 { + if ctx == nil { + ctx = context.Background() + } + return &GoRedis{ + ctx: ctx, + Conn: conn, + } } // JSONSet used to set a json object @@ -39,7 +52,7 @@ func (r *GoRedis) JSONSet(key string, path string, obj interface{}, opts ...rjs. return nil, err } args = append([]interface{}{name}, args...) - res, err = r.Conn.Do(ctx, args...).Result() + res, err = r.Conn.Do(r.ctx, args...).Result() if err != nil && err.Error() == rjs.ErrGoRedisNil.Error() { err = nil @@ -75,7 +88,7 @@ func (r *GoRedis) JSONGet(key, path string, opts ...rjs.GetOption) (res interfac } args = append([]interface{}{name}, args...) - res, err = r.Conn.Do(ctx, args...).Result() + res, err = r.Conn.Do(r.ctx, args...).Result() if err != nil { return } @@ -103,7 +116,7 @@ func (r *GoRedis) JSONMGet(path string, keys ...string) (res interface{}, err er return nil, err } args = append([]interface{}{name}, args...) - res, err = r.Conn.Do(ctx, args...).Result() + res, err = r.Conn.Do(r.ctx, args...).Result() if err != nil { return } @@ -131,7 +144,7 @@ func (r *GoRedis) JSONDel(key string, path string) (res interface{}, err error) return nil, err } args = append([]interface{}{name}, args...) - return r.Conn.Do(ctx, args...).Result() + return r.Conn.Do(r.ctx, args...).Result() } // 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) { return nil, err } args = append([]interface{}{name}, args...) - res, err = r.Conn.Do(ctx, args...).Result() + res, err = r.Conn.Do(r.ctx, args...).Result() if err != nil && err.Error() == rjs.ErrGoRedisNil.Error() { err = nil @@ -166,7 +179,7 @@ func (r *GoRedis) JSONNumIncrBy(key, path string, number int) (res interface{}, return nil, err } args = append([]interface{}{name}, args...) - res, err = r.Conn.Do(ctx, args...).Result() + res, err = r.Conn.Do(r.ctx, args...).Result() if err != nil { return } @@ -185,7 +198,7 @@ func (r *GoRedis) JSONNumMultBy(key, path string, number int) (res interface{}, return nil, err } args = append([]interface{}{name}, args...) - res, err = r.Conn.Do(ctx, args...).Result() + res, err = r.Conn.Do(r.ctx, args...).Result() if err != nil { return } @@ -204,7 +217,7 @@ func (r *GoRedis) JSONStrAppend(key, path, jsonstring string) (res interface{}, return nil, err } args = append([]interface{}{name}, args...) - return r.Conn.Do(ctx, args...).Result() + return r.Conn.Do(r.ctx, args...).Result() } // JSONStrLen to return the length of a string member @@ -219,7 +232,7 @@ func (r *GoRedis) JSONStrLen(key, path string) (res interface{}, err error) { return nil, err } args = append([]interface{}{name}, args...) - return r.Conn.Do(ctx, args...).Result() + return r.Conn.Do(r.ctx, args...).Result() } // JSONArrAppend to append json value into array at path @@ -241,7 +254,7 @@ func (r *GoRedis) JSONArrAppend(key, path string, values ...interface{}) (res in return nil, err } args = append([]interface{}{name}, args...) - return r.Conn.Do(ctx, args...).Result() + return r.Conn.Do(r.ctx, args...).Result() } // 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) { return nil, err } args = append([]interface{}{name}, args...) - return r.Conn.Do(ctx, args...).Result() + return r.Conn.Do(r.ctx, args...).Result() } // 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 } args = append([]interface{}{name}, args...) - res, err = r.Conn.Do(ctx, args...).Result() + res, err = r.Conn.Do(r.ctx, args...).Result() if err != nil { return } @@ -303,7 +316,7 @@ func (r *GoRedis) JSONArrIndex(key, path string, jsonValue interface{}, optional return nil, err } args = append([]interface{}{name}, args...) - return r.Conn.Do(ctx, args...).Result() + return r.Conn.Do(r.ctx, args...).Result() } // 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{} return nil, err } args = append([]interface{}{name}, args...) - return r.Conn.Do(ctx, args...).Result() + return r.Conn.Do(r.ctx, args...).Result() } // 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 return nil, err } args = append([]interface{}{name}, args...) - return r.Conn.Do(ctx, args...).Result() + return r.Conn.Do(r.ctx, args...).Result() } // 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) { return nil, err } args = append([]interface{}{name}, args...) - res, err = r.Conn.Do(ctx, args...).Result() + res, err = r.Conn.Do(r.ctx, args...).Result() if err != nil { return } @@ -380,7 +393,7 @@ func (r *GoRedis) JSONObjLen(key, path string) (res interface{}, err error) { return nil, err } args = append([]interface{}{name}, args...) - return r.Conn.Do(ctx, args...).Result() + return r.Conn.Do(r.ctx, args...).Result() } // JSONDebug reports information @@ -401,7 +414,7 @@ func (r *GoRedis) JSONDebug(subcommand rjs.DebugSubCommand, key, path string) (r return nil, err } args = append([]interface{}{name}, args...) - res, err = r.Conn.Do(ctx, args...).Result() + res, err = r.Conn.Do(r.ctx, args...).Result() if err != nil { return } @@ -430,7 +443,7 @@ func (r *GoRedis) JSONForget(key, path string) (res interface{}, err error) { return nil, err } args = append([]interface{}{name}, args...) - return r.Conn.Do(ctx, args...).Result() + return r.Conn.Do(r.ctx, args...).Result() } // 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) { return nil, err } args = append([]interface{}{name}, args...) - return r.Conn.Do(ctx, args...).Result() + return r.Conn.Do(r.ctx, args...).Result() } diff --git a/context.go b/context.go new file mode 100644 index 0000000..8007f40 --- /dev/null +++ b/context.go @@ -0,0 +1,30 @@ +package rejson + +import ( + "context" + "github.com/nitishm/go-rejson/clients" + "github.com/nitishm/go-rejson/rjs" +) + +// SetContext helps redis-clients, provide use of command level context +// in the ReJSON commands. +// Currently, only go-redis@v8 supports command level context, therefore +// a separate method is added to support it, maintaining the support for +// other clients and for backward compatibility. (nitishm/go-rejson#46) +func (r *Handler) SetContext(ctx context.Context) *Handler { + if r == nil { + return r // nil + } + + if r.clientName == rjs.ClientGoRedis { + if old, ok := r.implementation.(*clients.GoRedis); ok { + return &Handler{ + clientName: r.clientName, + implementation: clients.NewGoRedisClient(ctx, old.Conn), + } + } + } + + // for other clients, context is of no use, hence return same + return r +} diff --git a/rejson_test.go b/rejson_test.go index 8965cda..06c4ffa 100644 --- a/rejson_test.go +++ b/rejson_test.go @@ -12,8 +12,6 @@ import ( redigo "github.com/gomodule/redigo/redis" ) -var ctx = context.Background() - func TestUnsupportedCommand(t *testing.T) { _, _, err := rjs.CommandBuilder(1234, nil) if err == nil { @@ -23,53 +21,34 @@ func TestUnsupportedCommand(t *testing.T) { } type TestClient struct { + *testing.T name string conn interface{} rh *Handler } -func (t *TestClient) init() { +type helper struct { + cli interface{} + name string + closeFunc func() +} + +func (t *TestClient) init() []helper { t.name = "-" t.conn = "inactive" t.rh = NewReJSONHandler() -} - -func (t *TestClient) SetTestingClient(conn interface{}) { - t.conn = conn - - switch conn := conn.(type) { - case redigo.Conn: - t.name = "Redigo-" - t.rh.SetRedigoClient(conn) - case *goredis.Client: - t.name = "GoRedis-" - t.rh.SetGoRedisClient(conn) - default: - t.name = "-" - t.conn = "inactive" - t.rh.SetClientInactive() - } -} - -func TestReJSON(t *testing.T) { - test := TestClient{} - test.init() // Redigo Test Client redigoCli, err := redigo.Dial("tcp", ":6379") if err != nil { t.Fatalf("redigo - could not connect to redigo: %v", err) - return + return nil } // GoRedis Test Client goredisCli := goredis.NewClient(&goredis.Options{Addr: "localhost:6379"}) - clientsObj := []struct { - cli interface{} - name string - closeFunc func() - }{ + return []helper{ {cli: redigoCli, name: "Redigo ", closeFunc: func() { _, err = redigoCli.Do("FLUSHALL") if err != nil { @@ -81,7 +60,7 @@ func TestReJSON(t *testing.T) { } }}, {cli: goredisCli, name: "GoRedis ", closeFunc: func() { - if err := goredisCli.FlushAll(ctx).Err(); err != nil { + if err := goredisCli.FlushAll(context.Background()).Err(); err != nil { t.Fatalf("goredis - failed to flush: %v", err) } if err := goredisCli.Close(); err != nil { @@ -89,8 +68,29 @@ func TestReJSON(t *testing.T) { } }}, } +} - for _, obj := range clientsObj { +func (t *TestClient) SetTestingClient(conn interface{}) { + t.conn = conn + + switch conn := conn.(type) { + case redigo.Conn: + t.name = "Redigo-" + t.rh.SetRedigoClient(conn) + case *goredis.Client: + t.name = "GoRedis-" + t.rh.SetGoRedisClient(conn) + default: + t.name = "-" + t.conn = "inactive" + t.rh.SetClientInactive() + } +} + +func TestReJSON(t *testing.T) { + test := TestClient{T: t} + list := test.init() + for _, obj := range list { t.Run(obj.name+"TestJSONSet", func(t *testing.T) { test.SetTestingClient(obj.cli) testJSONSet(test.rh, t) @@ -176,6 +176,75 @@ func TestReJSON(t *testing.T) { } +func TestReJSONWithContext(t *testing.T) { + ctx := context.Background() + ctxCn, cancel := context.WithCancel(ctx) + cancel() + + testObj := TestObject{ + Name: "itemName", + Number: 1, + } + res := []byte("{\"name\":\"itemName\",\"number\":1}") + + test := TestClient{T: t} + list := test.init() + for _, obj := range list { + test.SetTestingClient(obj.cli) + rh := test.rh + + // check with canceled context + ok, err := rh.SetContext(ctxCn).JSONSet("testObj#1", ".", testObj) + if rh.clientName == rjs.ClientGoRedis { + if err == nil || ok == "OK" { + t.Errorf("JSONSet() got = %v %v, want nil, error: context.Canceled", ok, err) + } + got, err := rh.JSONGet("testObj#1", ".") + if err == nil || reflect.DeepEqual(got, res) { + t.Errorf("JSONGet() got = %v %v, want: no key found error", got, err) + } + } else { + if err != nil || ok != "OK" { + t.Errorf("JSONSet() got = %v %v, want OK, nil", ok, err) + } + got, err := rh.JSONGet("testObj#1", ".") + if err != nil || !reflect.DeepEqual(got, res) { + t.Errorf("JSONGet() got = %v %v, want: %v", got, err, res) + } + } + + // check with normal context + ok, err = rh.SetContext(ctx).JSONSet("testObj#2", ".", testObj) + if err != nil || ok != "OK" { + t.Errorf("JSONSet() got = %v %v, want OK, nil", ok, err) + } + got, err := rh.JSONGet("testObj#2", ".") + if err != nil || !reflect.DeepEqual(got, res) { + t.Errorf("JSONGet() got = %v %v, want: %v", got, err, res) + } + got, err = rh.SetContext(ctx).JSONGet("testObj#2", ".") + if err != nil || !reflect.DeepEqual(got, res) { + t.Errorf("JSONGet() got = %v %v, want: %v", got, err, res) + } + + // check without context + ok, err = rh.JSONSet("testObj#3", ".", testObj) + if err != nil || ok != "OK" { + t.Errorf("JSONSet() got = %v %v, want OK, nil", ok, err) + } + got, err = rh.JSONGet("testObj#3", ".") + if err != nil || !reflect.DeepEqual(got, res) { + t.Errorf("JSONGet() got = %v %v, want: %v", got, err, res) + } + got, err = rh.SetContext(ctx).JSONGet("testObj#3", ".") + if err != nil || !reflect.DeepEqual(got, res) { + t.Errorf("JSONGet() got = %v %v, want: %v", got, err, res) + } + + obj.closeFunc() + } +} + type TestObject struct { Name string `json:"name"` Number int `json:"number"` diff --git a/rjs/constants.go b/rjs/constants.go index 1665fcc..f06a3a3 100644 --- a/rjs/constants.go +++ b/rjs/constants.go @@ -17,6 +17,12 @@ const ( // ClientInactive signifies that the client is inactive in Handler ClientInactive = "inactive" + // ClientRedigo signifies that the current client is redigo + ClientRedigo = "redigo" + + // ClientGoRedis signifies that the current client is go-redis + ClientGoRedis = "goredis" + // PopArrLast gives index of the last element for JSONArrPop PopArrLast = -1 diff --git a/set_client.go b/set_client.go index 9edba97..2f04962 100644 --- a/set_client.go +++ b/set_client.go @@ -1,6 +1,7 @@ package rejson import ( + "context" goredis "github.com/go-redis/redis/v8" redigo "github.com/gomodule/redigo/redis" "github.com/nitishm/go-rejson/clients" @@ -29,8 +30,14 @@ func (r *Handler) SetRedigoClient(conn redigo.Conn) { } // SetGoRedisClient sets Go-Redis (https://github.com/go-redis/redis) client to -// the handler +// the handler. It is left for backward compatibility. func (r *Handler) SetGoRedisClient(conn *goredis.Client) { + r.SetGoRedisClientWithContext(nil, 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) { r.clientName = "goredis" - r.implementation = &clients.GoRedis{Conn: conn} + r.implementation = clients.NewGoRedisClient(ctx, conn) }