diff --git a/doctests/bf_tutorial_test.go b/doctests/bf_tutorial_test.go new file mode 100644 index 000000000..67545f1d5 --- /dev/null +++ b/doctests/bf_tutorial_test.go @@ -0,0 +1,83 @@ +// EXAMPLE: bf_tutorial +// HIDE_START +package example_commands_test + +import ( + "context" + "fmt" + + "github.com/redis/go-redis/v9" +) + +// HIDE_END + +func ExampleClient_bloom() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "bikes:models") + // REMOVE_END + + // STEP_START bloom + res1, err := rdb.BFReserve(ctx, "bikes:models", 0.01, 1000).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res1) // >>> OK + + res2, err := rdb.BFAdd(ctx, "bikes:models", "Smoky Mountain Striker").Result() + + if err != nil { + panic(err) + } + + fmt.Println(res2) // >>> true + + res3, err := rdb.BFExists(ctx, "bikes:models", "Smoky Mountain Striker").Result() + + if err != nil { + panic(err) + } + + fmt.Println(res3) // >>> true + + res4, err := rdb.BFMAdd(ctx, "bikes:models", + "Rocky Mountain Racer", + "Cloudy City Cruiser", + "Windy City Wippet", + ).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res4) // >>> [true true true] + + res5, err := rdb.BFMExists(ctx, "bikes:models", + "Rocky Mountain Racer", + "Cloudy City Cruiser", + "Windy City Wippet", + ).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res5) // >>> [true true true] + // STEP_END + + // Output: + // OK + // true + // true + // [true true true] + // [true true true] +} diff --git a/doctests/bitfield_tutorial_test.go b/doctests/bitfield_tutorial_test.go new file mode 100644 index 000000000..04fcb35f2 --- /dev/null +++ b/doctests/bitfield_tutorial_test.go @@ -0,0 +1,79 @@ +// EXAMPLE: bitfield_tutorial +// HIDE_START +package example_commands_test + +import ( + "context" + "fmt" + + "github.com/redis/go-redis/v9" +) + +// HIDE_END + +func ExampleClient_bf() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "bike:1:stats") + // REMOVE_END + + // STEP_START bf + res1, err := rdb.BitField(ctx, "bike:1:stats", + "set", "u32", "#0", "1000", + ).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res1) // >>> [0] + + res2, err := rdb.BitField(ctx, + "bike:1:stats", + "incrby", "u32", "#0", "-50", + "incrby", "u32", "#1", "1", + ).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res2) // >>> [950 1] + + res3, err := rdb.BitField(ctx, + "bike:1:stats", + "incrby", "u32", "#0", "500", + "incrby", "u32", "#1", "1", + ).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res3) // >>> [1450 2] + + res4, err := rdb.BitField(ctx, "bike:1:stats", + "get", "u32", "#0", + "get", "u32", "#1", + ).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res4) // >>> [1450 2] + // STEP_END + + // Output: + // [0] + // [950 1] + // [1450 2] + // [1450 2] +} diff --git a/doctests/bitmap_tutorial_test.go b/doctests/bitmap_tutorial_test.go new file mode 100644 index 000000000..dbfc247ac --- /dev/null +++ b/doctests/bitmap_tutorial_test.go @@ -0,0 +1,92 @@ +// EXAMPLE: bitmap_tutorial +// HIDE_START +package example_commands_test + +import ( + "context" + "fmt" + + "github.com/redis/go-redis/v9" +) + +// HIDE_END + +func ExampleClient_ping() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "pings:2024-01-01-00:00") + // REMOVE_END + + // STEP_START ping + res1, err := rdb.SetBit(ctx, "pings:2024-01-01-00:00", 123, 1).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res1) // >>> 0 + + res2, err := rdb.GetBit(ctx, "pings:2024-01-01-00:00", 123).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res2) // >>> 1 + + res3, err := rdb.GetBit(ctx, "pings:2024-01-01-00:00", 456).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res3) // >>> 0 + // STEP_END + + // Output: + // 0 + // 1 + // 0 +} + +func ExampleClient_bitcount() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + _, err := rdb.SetBit(ctx, "pings:2024-01-01-00:00", 123, 1).Result() + + if err != nil { + panic(err) + } + // REMOVE_END + + // STEP_START bitcount + res4, err := rdb.BitCount(ctx, "pings:2024-01-01-00:00", + &redis.BitCount{ + Start: 0, + End: 456, + }).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res4) // >>> 1 + // STEP_END + + // Output: + // 1 +} diff --git a/doctests/cmds_generic_test.go b/doctests/cmds_generic_test.go new file mode 100644 index 000000000..ab8ebdd53 --- /dev/null +++ b/doctests/cmds_generic_test.go @@ -0,0 +1,194 @@ +// EXAMPLE: cmds_generic +// HIDE_START +package example_commands_test + +import ( + "context" + "fmt" + "math" + "time" + + "github.com/redis/go-redis/v9" +) + +// HIDE_END + +func ExampleClient_del_cmd() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "key1", "key2", "key3") + // REMOVE_END + + // STEP_START del + delResult1, err := rdb.Set(ctx, "key1", "Hello", 0).Result() + + if err != nil { + panic(err) + } + + fmt.Println(delResult1) // >>> OK + + delResult2, err := rdb.Set(ctx, "key2", "World", 0).Result() + + if err != nil { + panic(err) + } + + fmt.Println(delResult2) // >>> OK + + delResult3, err := rdb.Del(ctx, "key1", "key2", "key3").Result() + + if err != nil { + panic(err) + } + + fmt.Println(delResult3) // >>> 2 + // STEP_END + + // Output: + // OK + // OK + // 2 +} + +func ExampleClient_expire_cmd() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "mykey") + // REMOVE_END + + // STEP_START expire + expireResult1, err := rdb.Set(ctx, "mykey", "Hello", 0).Result() + + if err != nil { + panic(err) + } + + fmt.Println(expireResult1) // >>> OK + + expireResult2, err := rdb.Expire(ctx, "mykey", 10*time.Second).Result() + + if err != nil { + panic(err) + } + + fmt.Println(expireResult2) // >>> true + + expireResult3, err := rdb.TTL(ctx, "mykey").Result() + + if err != nil { + panic(err) + } + + fmt.Println(math.Round(expireResult3.Seconds())) // >>> 10 + + expireResult4, err := rdb.Set(ctx, "mykey", "Hello World", 0).Result() + + if err != nil { + panic(err) + } + + fmt.Println(expireResult4) // >>> OK + + expireResult5, err := rdb.TTL(ctx, "mykey").Result() + + if err != nil { + panic(err) + } + + fmt.Println(expireResult5) // >>> -1ns + + expireResult6, err := rdb.ExpireXX(ctx, "mykey", 10*time.Second).Result() + + if err != nil { + panic(err) + } + + fmt.Println(expireResult6) // >>> false + + expireResult7, err := rdb.TTL(ctx, "mykey").Result() + + if err != nil { + panic(err) + } + + fmt.Println(expireResult7) // >>> -1ns + + expireResult8, err := rdb.ExpireNX(ctx, "mykey", 10*time.Second).Result() + + if err != nil { + panic(err) + } + + fmt.Println(expireResult8) // >>> true + + expireResult9, err := rdb.TTL(ctx, "mykey").Result() + + if err != nil { + panic(err) + } + + fmt.Println(math.Round(expireResult9.Seconds())) // >>> 10 + // STEP_END + + // Output: + // OK + // true + // 10 + // OK + // -1ns + // false + // -1ns + // true + // 10 +} + +func ExampleClient_ttl_cmd() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "mykey") + // REMOVE_END + + // STEP_START ttl + ttlResult1, err := rdb.Set(ctx, "mykey", "Hello", 10*time.Second).Result() + + if err != nil { + panic(err) + } + + fmt.Println(ttlResult1) // >>> OK + + ttlResult2, err := rdb.TTL(ctx, "mykey").Result() + + if err != nil { + panic(err) + } + + fmt.Println(math.Round(ttlResult2.Seconds())) // >>> 10 + // STEP_END + + // Output: + // OK + // 10 +} diff --git a/doctests/cmds_hash_test.go b/doctests/cmds_hash_test.go new file mode 100644 index 000000000..f9630a9de --- /dev/null +++ b/doctests/cmds_hash_test.go @@ -0,0 +1,133 @@ +// EXAMPLE: cmds_hash +// HIDE_START +package example_commands_test + +import ( + "context" + "fmt" + + "github.com/redis/go-redis/v9" +) + +// HIDE_END + +func ExampleClient_hset() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "myhash") + // REMOVE_END + + // STEP_START hset + res1, err := rdb.HSet(ctx, "myhash", "field1", "Hello").Result() + + if err != nil { + panic(err) + } + + fmt.Println(res1) // >>> 1 + + res2, err := rdb.HGet(ctx, "myhash", "field1").Result() + + if err != nil { + panic(err) + } + + fmt.Println(res2) // >>> Hello + + res3, err := rdb.HSet(ctx, "myhash", + "field2", "Hi", + "field3", "World", + ).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res3) // >>> 2 + + res4, err := rdb.HGet(ctx, "myhash", "field2").Result() + + if err != nil { + panic(err) + } + + fmt.Println(res4) // >>> Hi + + res5, err := rdb.HGet(ctx, "myhash", "field3").Result() + + if err != nil { + panic(err) + } + + fmt.Println(res5) // >>> World + + res6, err := rdb.HGetAll(ctx, "myhash").Result() + + if err != nil { + panic(err) + } + + fmt.Println(res6) + // >>> map[field1:Hello field2:Hi field3:World] + // STEP_END + + // Output: + // 1 + // Hello + // 2 + // Hi + // World + // map[field1:Hello field2:Hi field3:World] +} + +func ExampleClient_hget() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "myhash") + // REMOVE_END + + // STEP_START hget + res7, err := rdb.HSet(ctx, "myhash", "field1", "foo").Result() + + if err != nil { + panic(err) + } + + fmt.Println(res7) // >>> 1 + + res8, err := rdb.HGet(ctx, "myhash", "field1").Result() + + if err != nil { + panic(err) + } + + fmt.Println(res8) // >>> foo + + res9, err := rdb.HGet(ctx, "myhash", "field2").Result() + + if err != nil { + fmt.Println(err) + } + + fmt.Println(res9) // >>> + // STEP_END + + // Output: + // 1 + // foo + // redis: nil +} diff --git a/doctests/cmds_sorted_set_test.go b/doctests/cmds_sorted_set_test.go new file mode 100644 index 000000000..8704fc20d --- /dev/null +++ b/doctests/cmds_sorted_set_test.go @@ -0,0 +1,220 @@ +// EXAMPLE: cmds_sorted_set +// HIDE_START +package example_commands_test + +import ( + "context" + "fmt" + + "github.com/redis/go-redis/v9" +) + +// HIDE_END + +func ExampleClient_zadd_cmd() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "myzset") + // REMOVE_END + + // STEP_START zadd + zAddResult1, err := rdb.ZAdd(ctx, "myzset", + redis.Z{Member: "one", Score: 1}, + ).Result() + + if err != nil { + panic(err) + } + + fmt.Println(zAddResult1) // >>> 1 + + zAddResult2, err := rdb.ZAdd(ctx, "myzset", + redis.Z{Member: "uno", Score: 1}, + ).Result() + + if err != nil { + panic(err) + } + + fmt.Println(zAddResult2) + + zAddResult3, err := rdb.ZAdd(ctx, "myzset", + redis.Z{Member: "two", Score: 2}, + redis.Z{Member: "three", Score: 3}, + ).Result() + + if err != nil { + panic(err) + } + + fmt.Println(zAddResult3) // >>> 2 + + zAddResult4, err := rdb.ZRangeWithScores(ctx, "myzset", 0, -1).Result() + + if err != nil { + panic(err) + } + + fmt.Println(zAddResult4) // >>> [{1 one} {1 uno} {2 two} {3 three}] + // STEP_END + + // Output: + // 1 + // 1 + // 2 + // [{1 one} {1 uno} {2 two} {3 three}] +} + +func ExampleClient_zrange1() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "myzset") + // REMOVE_END + + // STEP_START zrange1 + zrangeResult1, err := rdb.ZAdd(ctx, "myzset", + redis.Z{Member: "one", Score: 1}, + redis.Z{Member: "two", Score: 2}, + redis.Z{Member: "three", Score: 3}, + ).Result() + + if err != nil { + panic(err) + } + + fmt.Println(zrangeResult1) // >>> 3 + + zrangeResult2, err := rdb.ZRange(ctx, "myzset", 0, -1).Result() + + if err != nil { + panic(err) + } + + fmt.Println(zrangeResult2) // >>> [one two three] + + zrangeResult3, err := rdb.ZRange(ctx, "myzset", 2, 3).Result() + + if err != nil { + panic(err) + } + + fmt.Println(zrangeResult3) // >>> [three] + + zrangeResult4, err := rdb.ZRange(ctx, "myzset", -2, -1).Result() + + if err != nil { + panic(err) + } + + fmt.Println(zrangeResult4) // >>> [two three] + // STEP_END + + // Output: + // 3 + // [one two three] + // [three] + // [two three] +} + +func ExampleClient_zrange2() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "myzset") + // REMOVE_END + + // STEP_START zrange2 + zRangeResult5, err := rdb.ZAdd(ctx, "myzset", + redis.Z{Member: "one", Score: 1}, + redis.Z{Member: "two", Score: 2}, + redis.Z{Member: "three", Score: 3}, + ).Result() + + if err != nil { + panic(err) + } + + fmt.Println(zRangeResult5) // >>> 3 + + zRangeResult6, err := rdb.ZRangeWithScores(ctx, "myzset", 0, 1).Result() + + if err != nil { + panic(err) + } + + fmt.Println(zRangeResult6) // >>> [{1 one} {2 two}] + // STEP_END + + // Output: + // 3 + // [{1 one} {2 two}] +} + +func ExampleClient_zrange3() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "myzset") + // REMOVE_END + + // STEP_START zrange3 + zRangeResult7, err := rdb.ZAdd(ctx, "myzset", + redis.Z{Member: "one", Score: 1}, + redis.Z{Member: "two", Score: 2}, + redis.Z{Member: "three", Score: 3}, + ).Result() + + if err != nil { + panic(err) + } + + fmt.Println(zRangeResult7) // >>> 3 + + zRangeResult8, err := rdb.ZRangeArgs(ctx, + redis.ZRangeArgs{ + Key: "myzset", + ByScore: true, + Start: "(1", + Stop: "+inf", + Offset: 1, + Count: 1, + }, + ).Result() + + if err != nil { + panic(err) + } + + fmt.Println(zRangeResult8) // >>> [three] + // STEP_END + + // Output: + // 3 + // [three] +} diff --git a/doctests/cmds_string_test.go b/doctests/cmds_string_test.go new file mode 100644 index 000000000..fb7801a67 --- /dev/null +++ b/doctests/cmds_string_test.go @@ -0,0 +1,57 @@ +// EXAMPLE: cmds_string +// HIDE_START +package example_commands_test + +import ( + "context" + "fmt" + + "github.com/redis/go-redis/v9" +) + +// HIDE_END + +func ExampleClient_cmd_incr() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "mykey") + // REMOVE_END + + // STEP_START incr + incrResult1, err := rdb.Set(ctx, "mykey", "10", 0).Result() + + if err != nil { + panic(err) + } + + fmt.Println(incrResult1) // >>> OK + + incrResult2, err := rdb.Incr(ctx, "mykey").Result() + + if err != nil { + panic(err) + } + + fmt.Println(incrResult2) // >>> 11 + + incrResult3, err := rdb.Get(ctx, "mykey").Result() + + if err != nil { + panic(err) + } + + fmt.Println(incrResult3) // >>> 11 + // STEP_END + + // Output: + // OK + // 11 + // 11 +} diff --git a/doctests/cms_tutorial_test.go b/doctests/cms_tutorial_test.go new file mode 100644 index 000000000..ade1fa93d --- /dev/null +++ b/doctests/cms_tutorial_test.go @@ -0,0 +1,84 @@ +// EXAMPLE: cms_tutorial +// HIDE_START +package example_commands_test + +import ( + "context" + "fmt" + + "github.com/redis/go-redis/v9" +) + +// HIDE_END + +func ExampleClient_cms() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "bikes:profit") + // REMOVE_END + + // STEP_START cms + res1, err := rdb.CMSInitByProb(ctx, "bikes:profit", 0.001, 0.002).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res1) // >>> OK + + res2, err := rdb.CMSIncrBy(ctx, "bikes:profit", + "Smoky Mountain Striker", 100, + ).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res2) // >>> [100] + + res3, err := rdb.CMSIncrBy(ctx, "bikes:profit", + "Rocky Mountain Racer", 200, + "Cloudy City Cruiser", 150, + ).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res3) // >>> [200 150] + + res4, err := rdb.CMSQuery(ctx, "bikes:profit", + "Smoky Mountain Striker", + ).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res4) // >>> [100] + + res5, err := rdb.CMSInfo(ctx, "bikes:profit").Result() + + if err != nil { + panic(err) + } + + fmt.Printf("Width: %v, Depth: %v, Count: %v", + res5.Width, res5.Depth, res5.Count) + // >>> Width: 2000, Depth: 9, Count: 450 + // STEP_END + + // Output: + // OK + // [100] + // [200 150] + // [100] + // Width: 2000, Depth: 9, Count: 450 +} diff --git a/doctests/cuckoo_tutorial_test.go b/doctests/cuckoo_tutorial_test.go new file mode 100644 index 000000000..08a503b10 --- /dev/null +++ b/doctests/cuckoo_tutorial_test.go @@ -0,0 +1,75 @@ +// EXAMPLE: cuckoo_tutorial +// HIDE_START +package example_commands_test + +import ( + "context" + "fmt" + + "github.com/redis/go-redis/v9" +) + +// HIDE_END + +func ExampleClient_cuckoo() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "bikes:models") + // REMOVE_END + + // STEP_START cuckoo + res1, err := rdb.CFReserve(ctx, "bikes:models", 1000000).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res1) // >>> OK + + res2, err := rdb.CFAdd(ctx, "bikes:models", "Smoky Mountain Striker").Result() + + if err != nil { + panic(err) + } + + fmt.Println(res2) // >>> true + + res3, err := rdb.CFExists(ctx, "bikes:models", "Smoky Mountain Striker").Result() + + if err != nil { + panic(err) + } + + fmt.Println(res3) // >>> true + + res4, err := rdb.CFExists(ctx, "bikes:models", "Terrible Bike Name").Result() + + if err != nil { + panic(err) + } + + fmt.Println(res4) // >>> false + + res5, err := rdb.CFDel(ctx, "bikes:models", "Smoky Mountain Striker").Result() + + if err != nil { + panic(err) + } + + fmt.Println(res5) // >>> true + // STEP_END + + // Output: + // OK + // true + // true + // false + // true +} diff --git a/doctests/geo_tutorial_test.go b/doctests/geo_tutorial_test.go new file mode 100644 index 000000000..051db623b --- /dev/null +++ b/doctests/geo_tutorial_test.go @@ -0,0 +1,139 @@ +// EXAMPLE: geo_tutorial +// HIDE_START +package example_commands_test + +import ( + "context" + "fmt" + + "github.com/redis/go-redis/v9" +) + +// HIDE_END + +func ExampleClient_geoadd() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "bikes:rentable") + // REMOVE_END + + // STEP_START geoadd + res1, err := rdb.GeoAdd(ctx, "bikes:rentable", + &redis.GeoLocation{ + Longitude: -122.27652, + Latitude: 37.805186, + Name: "station:1", + }).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res1) // >>> 1 + + res2, err := rdb.GeoAdd(ctx, "bikes:rentable", + &redis.GeoLocation{ + Longitude: -122.2674626, + Latitude: 37.8062344, + Name: "station:2", + }).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res2) // >>> 1 + + res3, err := rdb.GeoAdd(ctx, "bikes:rentable", + &redis.GeoLocation{ + Longitude: -122.2469854, + Latitude: 37.8104049, + Name: "station:3", + }).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res3) // >>> 1 + // STEP_END + + // Output: + // 1 + // 1 + // 1 +} + +func ExampleClient_geosearch() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "bikes:rentable") + + _, err := rdb.GeoAdd(ctx, "bikes:rentable", + &redis.GeoLocation{ + Longitude: -122.27652, + Latitude: 37.805186, + Name: "station:1", + }).Result() + + if err != nil { + panic(err) + } + + _, err = rdb.GeoAdd(ctx, "bikes:rentable", + &redis.GeoLocation{ + Longitude: -122.2674626, + Latitude: 37.8062344, + Name: "station:2", + }).Result() + + if err != nil { + panic(err) + } + + _, err = rdb.GeoAdd(ctx, "bikes:rentable", + &redis.GeoLocation{ + Longitude: -122.2469854, + Latitude: 37.8104049, + Name: "station:3", + }).Result() + + if err != nil { + panic(err) + } + // REMOVE_END + + // STEP_START geosearch + res4, err := rdb.GeoSearch(ctx, "bikes:rentable", + &redis.GeoSearchQuery{ + Longitude: -122.27652, + Latitude: 37.805186, + Radius: 5, + RadiusUnit: "km", + }, + ).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res4) // >>> [station:1 station:2 station:3] + // STEP_END + + // Output: + // [station:1 station:2 station:3] +} diff --git a/doctests/hll_tutorial_test.go b/doctests/hll_tutorial_test.go new file mode 100644 index 000000000..57e78d108 --- /dev/null +++ b/doctests/hll_tutorial_test.go @@ -0,0 +1,75 @@ +// EXAMPLE: hll_tutorial +// HIDE_START +package example_commands_test + +import ( + "context" + "fmt" + + "github.com/redis/go-redis/v9" +) + +// HIDE_END + +func ExampleClient_pfadd() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "bikes", "commuter_bikes", "all_bikes") + // REMOVE_END + + // STEP_START pfadd + res1, err := rdb.PFAdd(ctx, "bikes", "Hyperion", "Deimos", "Phoebe", "Quaoar").Result() + + if err != nil { + panic(err) + } + + fmt.Println(res1) // 1 + + res2, err := rdb.PFCount(ctx, "bikes").Result() + + if err != nil { + panic(err) + } + + fmt.Println(res2) // 4 + + res3, err := rdb.PFAdd(ctx, "commuter_bikes", "Salacia", "Mimas", "Quaoar").Result() + + if err != nil { + panic(err) + } + + fmt.Println(res3) // 1 + + res4, err := rdb.PFMerge(ctx, "all_bikes", "bikes", "commuter_bikes").Result() + + if err != nil { + panic(err) + } + + fmt.Println(res4) // OK + + res5, err := rdb.PFCount(ctx, "all_bikes").Result() + + if err != nil { + panic(err) + } + + fmt.Println(res5) // 6 + // STEP_END + + // Output: + // 1 + // 4 + // 1 + // OK + // 6 +} diff --git a/doctests/json_tutorial_test.go b/doctests/json_tutorial_test.go new file mode 100644 index 000000000..4e9787330 --- /dev/null +++ b/doctests/json_tutorial_test.go @@ -0,0 +1,1149 @@ +// EXAMPLE: json_tutorial +// HIDE_START +package example_commands_test + +import ( + "context" + "fmt" + + "github.com/redis/go-redis/v9" +) + +// HIDE_END +func ExampleClient_setget() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "bike") + // REMOVE_END + + // STEP_START set_get + res1, err := rdb.JSONSet(ctx, "bike", "$", + "\"Hyperion\"", + ).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res1) // >>> OK + + res2, err := rdb.JSONGet(ctx, "bike", "$").Result() + + if err != nil { + panic(err) + } + + fmt.Println(res2) // >>> ["Hyperion"] + + res3, err := rdb.JSONType(ctx, "bike", "$").Result() + + if err != nil { + panic(err) + } + + fmt.Println(res3) // >>> [[string]] + // STEP_END + + // Output: + // OK + // ["Hyperion"] + // [[string]] +} + +func ExampleClient_str() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "bike") + // REMOVE_END + + _, err := rdb.JSONSet(ctx, "bike", "$", + "\"Hyperion\"", + ).Result() + + if err != nil { + panic(err) + } + + // STEP_START str + res4, err := rdb.JSONStrLen(ctx, "bike", "$").Result() + + if err != nil { + panic(err) + } + + fmt.Println(*res4[0]) // >>> 8 + + res5, err := rdb.JSONStrAppend(ctx, "bike", "$", "\" (Enduro bikes)\"").Result() + + if err != nil { + panic(err) + } + + fmt.Println(*res5[0]) // >>> 23 + + res6, err := rdb.JSONGet(ctx, "bike", "$").Result() + + if err != nil { + panic(err) + } + + fmt.Println(res6) // >>> ["Hyperion (Enduro bikes)"] + // STEP_END + + // Output: + // 8 + // 23 + // ["Hyperion (Enduro bikes)"] +} + +func ExampleClient_num() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "crashes") + // REMOVE_END + + // STEP_START num + res7, err := rdb.JSONSet(ctx, "crashes", "$", 0).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res7) // >>> OK + + res8, err := rdb.JSONNumIncrBy(ctx, "crashes", "$", 1).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res8) // >>> [1] + + res9, err := rdb.JSONNumIncrBy(ctx, "crashes", "$", 1.5).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res9) // >>> [2.5] + + res10, err := rdb.JSONNumIncrBy(ctx, "crashes", "$", -0.75).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res10) // >>> [1.75] + // STEP_END + + // Output: + // OK + // [1] + // [2.5] + // [1.75] +} + +func ExampleClient_arr() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "newbike") + // REMOVE_END + + // STEP_START arr + res11, err := rdb.JSONSet(ctx, "newbike", "$", + []interface{}{ + "Deimos", + map[string]interface{}{"crashes": 0}, + nil, + }, + ).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res11) // >>> OK + + res12, err := rdb.JSONGet(ctx, "newbike", "$").Result() + + if err != nil { + panic(err) + } + + fmt.Println(res12) // >>> [["Deimos",{"crashes":0},null]] + + res13, err := rdb.JSONGet(ctx, "newbike", "$[1].crashes").Result() + + if err != nil { + panic(err) + } + + fmt.Println(res13) // >>> [0] + + res14, err := rdb.JSONDel(ctx, "newbike", "$.[-1]").Result() + + if err != nil { + panic(err) + } + + fmt.Println(res14) // >>> 1 + + res15, err := rdb.JSONGet(ctx, "newbike", "$").Result() + + if err != nil { + panic(err) + } + + fmt.Println(res15) // >>> [["Deimos",{"crashes":0}]] + // STEP_END + + // Output: + // OK + // [["Deimos",{"crashes":0},null]] + // [0] + // 1 + // [["Deimos",{"crashes":0}]] +} + +func ExampleClient_arr2() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "riders") + // REMOVE_END + + // STEP_START arr2 + res16, err := rdb.JSONSet(ctx, "riders", "$", []interface{}{}).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res16) // >>> OK + + res17, err := rdb.JSONArrAppend(ctx, "riders", "$", "\"Norem\"").Result() + + if err != nil { + panic(err) + } + + fmt.Println(res17) // >>> [1] + + res18, err := rdb.JSONGet(ctx, "riders", "$").Result() + + if err != nil { + panic(err) + } + + fmt.Println(res18) // >>> [["Norem"]] + + res19, err := rdb.JSONArrInsert(ctx, "riders", "$", 1, + "\"Prickett\"", "\"Royce\"", "\"Castilla\"", + ).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res19) // [3] + + res20, err := rdb.JSONGet(ctx, "riders", "$").Result() + + if err != nil { + panic(err) + } + + fmt.Println(res20) // >>> [["Norem", "Prickett", "Royce", "Castilla"]] + + rangeStop := 1 + + res21, err := rdb.JSONArrTrimWithArgs(ctx, "riders", "$", + &redis.JSONArrTrimArgs{Start: 1, Stop: &rangeStop}, + ).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res21) // >>> [1] + + res22, err := rdb.JSONGet(ctx, "riders", "$").Result() + + if err != nil { + panic(err) + } + + fmt.Println(res22) // >>> [["Prickett"]] + + res23, err := rdb.JSONArrPop(ctx, "riders", "$", -1).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res23) // >>> [["Prickett"]] + + res24, err := rdb.JSONArrPop(ctx, "riders", "$", -1).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res24) // [] + // STEP_END + + // Output: + // OK + // [1] + // [["Norem"]] + // [4] + // [["Norem","Prickett","Royce","Castilla"]] + // [1] + // [["Prickett"]] + // ["Prickett"] + // [] +} + +func ExampleClient_obj() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "bike:1") + // REMOVE_END + + // STEP_START obj + res25, err := rdb.JSONSet(ctx, "bike:1", "$", + map[string]interface{}{ + "model": "Deimos", + "brand": "Ergonom", + "price": 4972, + }, + ).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res25) // >>> OK + + res26, err := rdb.JSONObjLen(ctx, "bike:1", "$").Result() + + if err != nil { + panic(err) + } + + fmt.Println(*res26[0]) // >>> 3 + + res27, err := rdb.JSONObjKeys(ctx, "bike:1", "$").Result() + + if err != nil { + panic(err) + } + + fmt.Println(res27) // >>> [brand model price] + // STEP_END + + // Output: + // OK + // 3 + // [[brand model price]] +} + +var inventory_json = map[string]interface{}{ + "inventory": map[string]interface{}{ + "mountain_bikes": []interface{}{ + map[string]interface{}{ + "id": "bike:1", + "model": "Phoebe", + "description": "This is a mid-travel trail slayer that is a fantastic " + + "daily driver or one bike quiver. The Shimano Claris 8-speed groupset " + + "gives plenty of gear range to tackle hills and there\u2019s room for " + + "mudguards and a rack too. This is the bike for the rider who wants " + + "trail manners with low fuss ownership.", + "price": 1920, + "specs": map[string]interface{}{"material": "carbon", "weight": 13.1}, + "colors": []interface{}{"black", "silver"}, + }, + map[string]interface{}{ + "id": "bike:2", + "model": "Quaoar", + "description": "Redesigned for the 2020 model year, this bike " + + "impressed our testers and is the best all-around trail bike we've " + + "ever tested. The Shimano gear system effectively does away with an " + + "external cassette, so is super low maintenance in terms of wear " + + "and tear. All in all it's an impressive package for the price, " + + "making it very competitive.", + "price": 2072, + "specs": map[string]interface{}{"material": "aluminium", "weight": 7.9}, + "colors": []interface{}{"black", "white"}, + }, + map[string]interface{}{ + "id": "bike:3", + "model": "Weywot", + "description": "This bike gives kids aged six years and older " + + "a durable and uberlight mountain bike for their first experience " + + "on tracks and easy cruising through forests and fields. A set of " + + "powerful Shimano hydraulic disc brakes provide ample stopping " + + "ability. If you're after a budget option, this is one of the best " + + "bikes you could get.", + "price": 3264, + "specs": map[string]interface{}{"material": "alloy", "weight": 13.8}, + }, + }, + "commuter_bikes": []interface{}{ + map[string]interface{}{ + "id": "bike:4", + "model": "Salacia", + "description": "This bike is a great option for anyone who just " + + "wants a bike to get about on With a slick-shifting Claris gears " + + "from Shimano\u2019s, this is a bike which doesn\u2019t break the " + + "bank and delivers craved performance. It\u2019s for the rider " + + "who wants both efficiency and capability.", + "price": 1475, + "specs": map[string]interface{}{"material": "aluminium", "weight": 16.6}, + "colors": []interface{}{"black", "silver"}, + }, + map[string]interface{}{ + "id": "bike:5", + "model": "Mimas", + "description": "A real joy to ride, this bike got very high " + + "scores in last years Bike of the year report. The carefully " + + "crafted 50-34 tooth chainset and 11-32 tooth cassette give an " + + "easy-on-the-legs bottom gear for climbing, and the high-quality " + + "Vittoria Zaffiro tires give balance and grip.It includes " + + "a low-step frame , our memory foam seat, bump-resistant shocks and " + + "conveniently placed thumb throttle. Put it all together and you " + + "get a bike that helps redefine what can be done for this price.", + "price": 3941, + "specs": map[string]interface{}{"material": "alloy", "weight": 11.6}, + }, + }, + }, +} + +func ExampleClient_setbikes() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "bikes:inventory") + // REMOVE_END + + // STEP_START set_bikes + var inventory_json = map[string]interface{}{ + "inventory": map[string]interface{}{ + "mountain_bikes": []interface{}{ + map[string]interface{}{ + "id": "bike:1", + "model": "Phoebe", + "description": "This is a mid-travel trail slayer that is a fantastic " + + "daily driver or one bike quiver. The Shimano Claris 8-speed groupset " + + "gives plenty of gear range to tackle hills and there\u2019s room for " + + "mudguards and a rack too. This is the bike for the rider who wants " + + "trail manners with low fuss ownership.", + "price": 1920, + "specs": map[string]interface{}{"material": "carbon", "weight": 13.1}, + "colors": []interface{}{"black", "silver"}, + }, + map[string]interface{}{ + "id": "bike:2", + "model": "Quaoar", + "description": "Redesigned for the 2020 model year, this bike " + + "impressed our testers and is the best all-around trail bike we've " + + "ever tested. The Shimano gear system effectively does away with an " + + "external cassette, so is super low maintenance in terms of wear " + + "and tear. All in all it's an impressive package for the price, " + + "making it very competitive.", + "price": 2072, + "specs": map[string]interface{}{"material": "aluminium", "weight": 7.9}, + "colors": []interface{}{"black", "white"}, + }, + map[string]interface{}{ + "id": "bike:3", + "model": "Weywot", + "description": "This bike gives kids aged six years and older " + + "a durable and uberlight mountain bike for their first experience " + + "on tracks and easy cruising through forests and fields. A set of " + + "powerful Shimano hydraulic disc brakes provide ample stopping " + + "ability. If you're after a budget option, this is one of the best " + + "bikes you could get.", + "price": 3264, + "specs": map[string]interface{}{"material": "alloy", "weight": 13.8}, + }, + }, + "commuter_bikes": []interface{}{ + map[string]interface{}{ + "id": "bike:4", + "model": "Salacia", + "description": "This bike is a great option for anyone who just " + + "wants a bike to get about on With a slick-shifting Claris gears " + + "from Shimano\u2019s, this is a bike which doesn\u2019t break the " + + "bank and delivers craved performance. It\u2019s for the rider " + + "who wants both efficiency and capability.", + "price": 1475, + "specs": map[string]interface{}{"material": "aluminium", "weight": 16.6}, + "colors": []interface{}{"black", "silver"}, + }, + map[string]interface{}{ + "id": "bike:5", + "model": "Mimas", + "description": "A real joy to ride, this bike got very high " + + "scores in last years Bike of the year report. The carefully " + + "crafted 50-34 tooth chainset and 11-32 tooth cassette give an " + + "easy-on-the-legs bottom gear for climbing, and the high-quality " + + "Vittoria Zaffiro tires give balance and grip.It includes " + + "a low-step frame , our memory foam seat, bump-resistant shocks and " + + "conveniently placed thumb throttle. Put it all together and you " + + "get a bike that helps redefine what can be done for this price.", + "price": 3941, + "specs": map[string]interface{}{"material": "alloy", "weight": 11.6}, + }, + }, + }, + } + + res1, err := rdb.JSONSet(ctx, "bikes:inventory", "$", inventory_json).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res1) // >>> OK + // STEP_END + + // Output: + // OK +} + +func ExampleClient_getbikes() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "bikes:inventory") + // REMOVE_END + + _, err := rdb.JSONSet(ctx, "bikes:inventory", "$", inventory_json).Result() + + if err != nil { + panic(err) + } + + // STEP_START get_bikes + res2, err := rdb.JSONGetWithArgs(ctx, "bikes:inventory", + &redis.JSONGetArgs{Indent: " ", Newline: "\n", Space: " "}, + "$.inventory.*", + ).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res2) + // >>> + // [ + // [ + // { + // "colors": [ + // "black", + // "silver" + // ... + // STEP_END + + // Output: + // [ + // [ + // { + // "colors": [ + // "black", + // "silver" + // ], + // "description": "This bike is a great option for anyone who just wants a bike to get about on With a slick-shifting Claris gears from Shimano’s, this is a bike which doesn’t break the bank and delivers craved performance. It’s for the rider who wants both efficiency and capability.", + // "id": "bike:4", + // "model": "Salacia", + // "price": 1475, + // "specs": { + // "material": "aluminium", + // "weight": 16.6 + // } + // }, + // { + // "description": "A real joy to ride, this bike got very high scores in last years Bike of the year report. The carefully crafted 50-34 tooth chainset and 11-32 tooth cassette give an easy-on-the-legs bottom gear for climbing, and the high-quality Vittoria Zaffiro tires give balance and grip.It includes a low-step frame , our memory foam seat, bump-resistant shocks and conveniently placed thumb throttle. Put it all together and you get a bike that helps redefine what can be done for this price.", + // "id": "bike:5", + // "model": "Mimas", + // "price": 3941, + // "specs": { + // "material": "alloy", + // "weight": 11.6 + // } + // } + // ], + // [ + // { + // "colors": [ + // "black", + // "silver" + // ], + // "description": "This is a mid-travel trail slayer that is a fantastic daily driver or one bike quiver. The Shimano Claris 8-speed groupset gives plenty of gear range to tackle hills and there’s room for mudguards and a rack too. This is the bike for the rider who wants trail manners with low fuss ownership.", + // "id": "bike:1", + // "model": "Phoebe", + // "price": 1920, + // "specs": { + // "material": "carbon", + // "weight": 13.1 + // } + // }, + // { + // "colors": [ + // "black", + // "white" + // ], + // "description": "Redesigned for the 2020 model year, this bike impressed our testers and is the best all-around trail bike we've ever tested. The Shimano gear system effectively does away with an external cassette, so is super low maintenance in terms of wear and tear. All in all it's an impressive package for the price, making it very competitive.", + // "id": "bike:2", + // "model": "Quaoar", + // "price": 2072, + // "specs": { + // "material": "aluminium", + // "weight": 7.9 + // } + // }, + // { + // "description": "This bike gives kids aged six years and older a durable and uberlight mountain bike for their first experience on tracks and easy cruising through forests and fields. A set of powerful Shimano hydraulic disc brakes provide ample stopping ability. If you're after a budget option, this is one of the best bikes you could get.", + // "id": "bike:3", + // "model": "Weywot", + // "price": 3264, + // "specs": { + // "material": "alloy", + // "weight": 13.8 + // } + // } + // ] + // ] +} + +func ExampleClient_getmtnbikes() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "bikes:inventory") + // REMOVE_END + + _, err := rdb.JSONSet(ctx, "bikes:inventory", "$", inventory_json).Result() + + if err != nil { + panic(err) + } + + // STEP_START get_mtnbikes + res3, err := rdb.JSONGet(ctx, "bikes:inventory", + "$.inventory.mountain_bikes[*].model", + ).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res3) + // >>> ["Phoebe","Quaoar","Weywot"] + + res4, err := rdb.JSONGet(ctx, + "bikes:inventory", "$.inventory[\"mountain_bikes\"][*].model", + ).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res4) + // >>> ["Phoebe","Quaoar","Weywot"] + + res5, err := rdb.JSONGet(ctx, + "bikes:inventory", "$..mountain_bikes[*].model", + ).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res5) + // >>> ["Phoebe","Quaoar","Weywot"] + // STEP_END + + // Output: + // ["Phoebe","Quaoar","Weywot"] + // ["Phoebe","Quaoar","Weywot"] + // ["Phoebe","Quaoar","Weywot"] +} + +func ExampleClient_getmodels() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "bikes:inventory") + // REMOVE_END + + _, err := rdb.JSONSet(ctx, "bikes:inventory", "$", inventory_json).Result() + + if err != nil { + panic(err) + } + + // STEP_START get_models + res6, err := rdb.JSONGet(ctx, "bikes:inventory", "$..model").Result() + + if err != nil { + panic(err) + } + + fmt.Println(res6) // >>> ["Salacia","Mimas","Phoebe","Quaoar","Weywot"] + // STEP_END + + // Output: + // ["Salacia","Mimas","Phoebe","Quaoar","Weywot"] +} + +func ExampleClient_get2mtnbikes() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "bikes:inventory") + // REMOVE_END + + _, err := rdb.JSONSet(ctx, "bikes:inventory", "$", inventory_json).Result() + + if err != nil { + panic(err) + } + + // STEP_START get2mtnbikes + res7, err := rdb.JSONGet(ctx, "bikes:inventory", "$..mountain_bikes[0:2].model").Result() + + if err != nil { + panic(err) + } + + fmt.Println(res7) // >>> ["Phoebe","Quaoar"] + // STEP_END + + // Output: + // ["Phoebe","Quaoar"] +} + +func ExampleClient_filter1() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "bikes:inventory") + // REMOVE_END + + _, err := rdb.JSONSet(ctx, "bikes:inventory", "$", inventory_json).Result() + + if err != nil { + panic(err) + } + + // STEP_START filter1 + res8, err := rdb.JSONGetWithArgs(ctx, "bikes:inventory", + &redis.JSONGetArgs{Indent: " ", Newline: "\n", Space: " "}, + "$..mountain_bikes[?(@.price < 3000 && @.specs.weight < 10)]", + ).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res8) + // >>> + // [ + // { + // "colors": [ + // "black", + // "white" + // ], + // "description": "Redesigned for the 2020 model year + // ... + // STEP_END + + // Output: + // [ + // { + // "colors": [ + // "black", + // "white" + // ], + // "description": "Redesigned for the 2020 model year, this bike impressed our testers and is the best all-around trail bike we've ever tested. The Shimano gear system effectively does away with an external cassette, so is super low maintenance in terms of wear and tear. All in all it's an impressive package for the price, making it very competitive.", + // "id": "bike:2", + // "model": "Quaoar", + // "price": 2072, + // "specs": { + // "material": "aluminium", + // "weight": 7.9 + // } + // } + // ] +} + +func ExampleClient_filter2() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "bikes:inventory") + // REMOVE_END + + _, err := rdb.JSONSet(ctx, "bikes:inventory", "$", inventory_json).Result() + + if err != nil { + panic(err) + } + + // STEP_START filter2 + res9, err := rdb.JSONGet(ctx, + "bikes:inventory", + "$..[?(@.specs.material == 'alloy')].model", + ).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res9) // >>> ["Mimas","Weywot"] + // STEP_END + + // Output: + // ["Mimas","Weywot"] +} + +func ExampleClient_filter3() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "bikes:inventory") + // REMOVE_END + + _, err := rdb.JSONSet(ctx, "bikes:inventory", "$", inventory_json).Result() + + if err != nil { + panic(err) + } + + // STEP_START filter3 + res10, err := rdb.JSONGet(ctx, + "bikes:inventory", + "$..[?(@.specs.material =~ '(?i)al')].model", + ).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res10) // >>> ["Salacia","Mimas","Quaoar","Weywot"] + // STEP_END + + // Output: + // ["Salacia","Mimas","Quaoar","Weywot"] +} + +func ExampleClient_filter4() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "bikes:inventory") + // REMOVE_END + + _, err := rdb.JSONSet(ctx, "bikes:inventory", "$", inventory_json).Result() + + if err != nil { + panic(err) + } + + // STEP_START filter4 + res11, err := rdb.JSONSet(ctx, + "bikes:inventory", + "$.inventory.mountain_bikes[0].regex_pat", + "\"(?i)al\"", + ).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res11) // >>> OK + + res12, err := rdb.JSONSet(ctx, + "bikes:inventory", + "$.inventory.mountain_bikes[1].regex_pat", + "\"(?i)al\"", + ).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res12) // >>> OK + + res13, err := rdb.JSONSet(ctx, + "bikes:inventory", + "$.inventory.mountain_bikes[2].regex_pat", + "\"(?i)al\"", + ).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res13) // >>> OK + + res14, err := rdb.JSONGet(ctx, + "bikes:inventory", + "$.inventory.mountain_bikes[?(@.specs.material =~ @.regex_pat)].model", + ).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res14) // >>> ["Quaoar","Weywot"] + // STEP_END + + // Output: + // OK + // OK + // OK + // ["Quaoar","Weywot"] +} + +func ExampleClient_updatebikes() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "bikes:inventory") + // REMOVE_END + + _, err := rdb.JSONSet(ctx, "bikes:inventory", "$", inventory_json).Result() + + if err != nil { + panic(err) + } + + // STEP_START update_bikes + res15, err := rdb.JSONGet(ctx, "bikes:inventory", "$..price").Result() + + if err != nil { + panic(err) + } + + fmt.Println(res15) // >>> [1475,3941,1920,2072,3264] + + res16, err := rdb.JSONNumIncrBy(ctx, "bikes:inventory", "$..price", -100).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res16) // >>> [1375,3841,1820,1972,3164] + + res17, err := rdb.JSONNumIncrBy(ctx, "bikes:inventory", "$..price", 100).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res17) // >>> [1475,3941,1920,2072,3264] + // STEP_END + + // Output: + // [1475,3941,1920,2072,3264] + // [1375,3841,1820,1972,3164] + // [1475,3941,1920,2072,3264] +} + +func ExampleClient_updatefilters1() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "bikes:inventory") + // REMOVE_END + + _, err := rdb.JSONSet(ctx, "bikes:inventory", "$", inventory_json).Result() + + if err != nil { + panic(err) + } + + // STEP_START update_filters1 + res18, err := rdb.JSONSet(ctx, + "bikes:inventory", + "$.inventory.*[?(@.price<2000)].price", + 1500, + ).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res18) // >>> OK + + res19, err := rdb.JSONGet(ctx, "bikes:inventory", "$..price").Result() + + if err != nil { + panic(err) + } + + fmt.Println(res19) // >>> [1500,3941,1500,2072,3264] + // STEP_END + + // Output: + // OK + // [1500,3941,1500,2072,3264] +} + +func ExampleClient_updatefilters2() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "bikes:inventory") + // REMOVE_END + + _, err := rdb.JSONSet(ctx, "bikes:inventory", "$", inventory_json).Result() + + if err != nil { + panic(err) + } + + // STEP_START update_filters2 + res20, err := rdb.JSONArrAppend(ctx, + "bikes:inventory", + "$.inventory.*[?(@.price<2000)].colors", + "\"pink\"", + ).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res20) // >>> [3 3] + + res21, err := rdb.JSONGet(ctx, "bikes:inventory", "$..[*].colors").Result() + + if err != nil { + panic(err) + } + + fmt.Println(res21) + // >>> [["black","silver","pink"],["black","silver","pink"],["black","white"]] + // STEP_END + + // Output: + // [3 3] + // [["black","silver","pink"],["black","silver","pink"],["black","white"]] +} diff --git a/doctests/tdigest_tutorial_test.go b/doctests/tdigest_tutorial_test.go new file mode 100644 index 000000000..7589b0ec8 --- /dev/null +++ b/doctests/tdigest_tutorial_test.go @@ -0,0 +1,251 @@ +// EXAMPLE: tdigest_tutorial +// HIDE_START +package example_commands_test + +import ( + "context" + "fmt" + + "github.com/redis/go-redis/v9" +) + +// HIDE_END + +func ExampleClient_tdigstart() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "racer_ages", "bikes:sales") + // REMOVE_END + + // STEP_START tdig_start + res1, err := rdb.TDigestCreate(ctx, "bikes:sales").Result() + + if err != nil { + panic(err) + } + + fmt.Println(res1) // >>> OK + + res2, err := rdb.TDigestAdd(ctx, "bikes:sales", 21).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res2) // >>> OK + + res3, err := rdb.TDigestAdd(ctx, "bikes:sales", + 150, 95, 75, 34, + ).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res3) // >>> OK + + // STEP_END + + // Output: + // OK + // OK + // OK +} + +func ExampleClient_tdigcdf() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "racer_ages", "bikes:sales") + // REMOVE_END + + // STEP_START tdig_cdf + res4, err := rdb.TDigestCreate(ctx, "racer_ages").Result() + + if err != nil { + panic(err) + } + + fmt.Println(res4) // >>> OK + + res5, err := rdb.TDigestAdd(ctx, "racer_ages", + 45.88, 44.2, 58.03, 19.76, 39.84, 69.28, + 50.97, 25.41, 19.27, 85.71, 42.63, + ).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res5) // >>> OK + + res6, err := rdb.TDigestRank(ctx, "racer_ages", 50).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res6) // >>> [7] + + res7, err := rdb.TDigestRank(ctx, "racer_ages", 50, 40).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res7) // >>> [7 4] + // STEP_END + + // Output: + // OK + // OK + // [7] + // [7 4] +} + +func ExampleClient_tdigquant() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "racer_ages") + // REMOVE_END + + _, err := rdb.TDigestCreate(ctx, "racer_ages").Result() + + if err != nil { + panic(err) + } + + _, err = rdb.TDigestAdd(ctx, "racer_ages", + 45.88, 44.2, 58.03, 19.76, 39.84, 69.28, + 50.97, 25.41, 19.27, 85.71, 42.63, + ).Result() + + if err != nil { + panic(err) + } + + // STEP_START tdig_quant + res8, err := rdb.TDigestQuantile(ctx, "racer_ages", 0.5).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res8) // >>> [44.2] + + res9, err := rdb.TDigestByRank(ctx, "racer_ages", 4).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res9) // >>> [42.63] + // STEP_END + + // Output: + // [44.2] + // [42.63] +} + +func ExampleClient_tdigmin() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "racer_ages") + // REMOVE_END + + _, err := rdb.TDigestCreate(ctx, "racer_ages").Result() + + if err != nil { + panic(err) + } + + _, err = rdb.TDigestAdd(ctx, "racer_ages", + 45.88, 44.2, 58.03, 19.76, 39.84, 69.28, + 50.97, 25.41, 19.27, 85.71, 42.63, + ).Result() + + if err != nil { + panic(err) + } + + // STEP_START tdig_min + res10, err := rdb.TDigestMin(ctx, "racer_ages").Result() + + if err != nil { + panic(err) + } + + fmt.Println(res10) // >>> 19.27 + + res11, err := rdb.TDigestMax(ctx, "racer_ages").Result() + + if err != nil { + panic(err) + } + + fmt.Println(res11) // >>> 85.71 + // STEP_END + + // Output: + // 19.27 + // 85.71 +} + +func ExampleClient_tdigreset() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "racer_ages") + // REMOVE_END + _, err := rdb.TDigestCreate(ctx, "racer_ages").Result() + + if err != nil { + panic(err) + } + + // STEP_START tdig_reset + res12, err := rdb.TDigestReset(ctx, "racer_ages").Result() + + if err != nil { + panic(err) + } + + fmt.Println(res12) // >>> OK + // STEP_END + + // Output: + // OK +} diff --git a/doctests/topk_tutorial_test.go b/doctests/topk_tutorial_test.go new file mode 100644 index 000000000..2d1fe7fc2 --- /dev/null +++ b/doctests/topk_tutorial_test.go @@ -0,0 +1,75 @@ +// EXAMPLE: topk_tutorial +// HIDE_START +package example_commands_test + +import ( + "context" + "fmt" + + "github.com/redis/go-redis/v9" +) + +// HIDE_END + +func ExampleClient_topk() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "bikes:keywords") + // REMOVE_END + + // STEP_START topk + res1, err := rdb.TopKReserve(ctx, "bikes:keywords", 5).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res1) // >>> OK + + res2, err := rdb.TopKAdd(ctx, "bikes:keywords", + "store", + "seat", + "handlebars", + "handles", + "pedals", + "tires", + "store", + "seat", + ).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res2) // >>> [ handlebars ] + + res3, err := rdb.TopKList(ctx, "bikes:keywords").Result() + + if err != nil { + panic(err) + } + + fmt.Println(res3) // [store seat pedals tires handles] + + res4, err := rdb.TopKQuery(ctx, "bikes:keywords", "store", "handlebars").Result() + + if err != nil { + panic(err) + } + + fmt.Println(res4) // [true false] + // STEP_END + + // Output: + // OK + // [ handlebars ] + // [store seat pedals tires handles] + // [true false] +} diff --git a/go.mod b/go.mod index bd13d7453..c1d9037ac 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,6 @@ require ( ) retract ( - v9.5.3 // This version was accidentally released. Please use version 9.6.0 instead. v9.5.4 // This version was accidentally released. Please use version 9.6.0 instead. + v9.5.3 // This version was accidentally released. Please use version 9.6.0 instead. ) diff --git a/internal/pool/conn_check.go b/internal/pool/conn_check.go index 07c261c2b..83190d394 100644 --- a/internal/pool/conn_check.go +++ b/internal/pool/conn_check.go @@ -3,7 +3,6 @@ package pool import ( - "crypto/tls" "errors" "io" "net" @@ -17,10 +16,6 @@ func connCheck(conn net.Conn) error { // Reset previous timeout. _ = conn.SetDeadline(time.Time{}) - // Check if tls.Conn. - if c, ok := conn.(*tls.Conn); ok { - conn = c.NetConn() - } sysConn, ok := conn.(syscall.Conn) if !ok { return nil diff --git a/internal/pool/conn_check_test.go b/internal/pool/conn_check_test.go index 214993339..2ade8a0b9 100644 --- a/internal/pool/conn_check_test.go +++ b/internal/pool/conn_check_test.go @@ -3,7 +3,6 @@ package pool import ( - "crypto/tls" "net" "net/http/httptest" "time" @@ -15,17 +14,12 @@ import ( var _ = Describe("tests conn_check with real conns", func() { var ts *httptest.Server var conn net.Conn - var tlsConn *tls.Conn var err error BeforeEach(func() { ts = httptest.NewServer(nil) conn, err = net.DialTimeout(ts.Listener.Addr().Network(), ts.Listener.Addr().String(), time.Second) Expect(err).NotTo(HaveOccurred()) - tlsTestServer := httptest.NewUnstartedServer(nil) - tlsTestServer.StartTLS() - tlsConn, err = tls.DialWithDialer(&net.Dialer{Timeout: time.Second}, tlsTestServer.Listener.Addr().Network(), tlsTestServer.Listener.Addr().String(), &tls.Config{InsecureSkipVerify: true}) - Expect(err).NotTo(HaveOccurred()) }) AfterEach(func() { @@ -39,23 +33,11 @@ var _ = Describe("tests conn_check with real conns", func() { Expect(connCheck(conn)).To(HaveOccurred()) }) - It("good tls conn check", func() { - Expect(connCheck(tlsConn)).NotTo(HaveOccurred()) - - Expect(tlsConn.Close()).NotTo(HaveOccurred()) - Expect(connCheck(tlsConn)).To(HaveOccurred()) - }) - It("bad conn check", func() { Expect(conn.Close()).NotTo(HaveOccurred()) Expect(connCheck(conn)).To(HaveOccurred()) }) - It("bad tls conn check", func() { - Expect(tlsConn.Close()).NotTo(HaveOccurred()) - Expect(connCheck(tlsConn)).To(HaveOccurred()) - }) - It("check conn deadline", func() { Expect(conn.SetDeadline(time.Now())).NotTo(HaveOccurred()) time.Sleep(time.Millisecond * 10) diff --git a/json.go b/json.go index ca731db3a..b3cadf4b7 100644 --- a/json.go +++ b/json.go @@ -60,7 +60,7 @@ type JSONArrTrimArgs struct { type JSONCmd struct { baseCmd val string - expanded []interface{} + expanded interface{} } var _ Cmder = (*JSONCmd)(nil) @@ -100,11 +100,11 @@ func (cmd *JSONCmd) Result() (string, error) { return cmd.Val(), cmd.Err() } -func (cmd JSONCmd) Expanded() (interface{}, error) { +func (cmd *JSONCmd) Expanded() (interface{}, error) { if len(cmd.val) != 0 && cmd.expanded == nil { err := json.Unmarshal([]byte(cmd.val), &cmd.expanded) if err != nil { - return "", err + return nil, err } } @@ -494,7 +494,7 @@ func (c cmdable) JSONMSet(ctx context.Context, params ...interface{}) *StatusCmd } // JSONNumIncrBy increments the number value stored at the specified path by the provided number. -// For more information, see https://redis.io/commands/json.numincreby +// For more information, see https://redis.io/docs/latest/commands/json.numincrby/ func (c cmdable) JSONNumIncrBy(ctx context.Context, key, path string, value float64) *JSONCmd { args := []interface{}{"JSON.NUMINCRBY", key, path, value} cmd := newJSONCmd(ctx, args...) diff --git a/json_test.go b/json_test.go index d1ea24290..9139be3ac 100644 --- a/json_test.go +++ b/json_test.go @@ -2,6 +2,8 @@ package redis_test import ( "context" + "encoding/json" + "time" . "github.com/bsm/ginkgo/v2" . "github.com/bsm/gomega" @@ -17,644 +19,800 @@ var _ = Describe("JSON Commands", Label("json"), func() { ctx := context.TODO() var client *redis.Client - BeforeEach(func() { - client = redis.NewClient(&redis.Options{Addr: ":6379"}) - Expect(client.FlushAll(ctx).Err()).NotTo(HaveOccurred()) - }) - - AfterEach(func() { - Expect(client.Close()).NotTo(HaveOccurred()) - }) - - Describe("arrays", Label("arrays"), func() { - It("should JSONArrAppend", Label("json.arrappend", "json"), func() { - cmd1 := client.JSONSet(ctx, "append2", "$", `{"a": [10], "b": {"a": [12, 13]}}`) - Expect(cmd1.Err()).NotTo(HaveOccurred()) - Expect(cmd1.Val()).To(Equal("OK")) - - cmd2 := client.JSONArrAppend(ctx, "append2", "$..a", 10) - Expect(cmd2.Err()).NotTo(HaveOccurred()) - Expect(cmd2.Val()).To(Equal([]int64{2, 3})) + setupRedisClient := func(protocolVersion int) *redis.Client { + return redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + DB: 0, + Protocol: protocolVersion, + UnstableResp3: true, }) + } - It("should JSONArrIndex and JSONArrIndexWithArgs", Label("json.arrindex", "json"), func() { - cmd1, err := client.JSONSet(ctx, "index1", "$", `{"a": [10], "b": {"a": [12, 10]}}`).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(cmd1).To(Equal("OK")) - - cmd2, err := client.JSONArrIndex(ctx, "index1", "$.b.a", 10).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(cmd2).To(Equal([]int64{1})) - - cmd3, err := client.JSONSet(ctx, "index2", "$", `[0,1,2,3,4]`).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(cmd3).To(Equal("OK")) - - res, err := client.JSONArrIndex(ctx, "index2", "$", 1).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res[0]).To(Equal(int64(1))) - - res, err = client.JSONArrIndex(ctx, "index2", "$", 1, 2).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res[0]).To(Equal(int64(-1))) - - res, err = client.JSONArrIndex(ctx, "index2", "$", 4).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res[0]).To(Equal(int64(4))) - - res, err = client.JSONArrIndexWithArgs(ctx, "index2", "$", &redis.JSONArrIndexArgs{}, 4).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res[0]).To(Equal(int64(4))) - - stop := 5000 - res, err = client.JSONArrIndexWithArgs(ctx, "index2", "$", &redis.JSONArrIndexArgs{Stop: &stop}, 4).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res[0]).To(Equal(int64(4))) - - stop = -1 - res, err = client.JSONArrIndexWithArgs(ctx, "index2", "$", &redis.JSONArrIndexArgs{Stop: &stop}, 4).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res[0]).To(Equal(int64(-1))) - }) - - It("should JSONArrIndex and JSONArrIndexWithArgs with $", Label("json.arrindex", "json"), func() { - doc := `{ - "store": { - "book": [ - { - "category": "reference", - "author": "Nigel Rees", - "title": "Sayings of the Century", - "price": 8.95, - "size": [10, 20, 30, 40] - }, - { - "category": "fiction", - "author": "Evelyn Waugh", - "title": "Sword of Honour", - "price": 12.99, - "size": [50, 60, 70, 80] - }, - { - "category": "fiction", - "author": "Herman Melville", - "title": "Moby Dick", - "isbn": "0-553-21311-3", - "price": 8.99, - "size": [5, 10, 20, 30] - }, - { - "category": "fiction", - "author": "J. R. R. Tolkien", - "title": "The Lord of the Rings", - "isbn": "0-395-19395-8", - "price": 22.99, - "size": [5, 6, 7, 8] - } - ], - "bicycle": {"color": "red", "price": 19.95} - } - }` - res, err := client.JSONSet(ctx, "doc1", "$", doc).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res).To(Equal("OK")) - - resGet, err := client.JSONGet(ctx, "doc1", "$.store.book[?(@.price<10)].size").Result() - Expect(err).NotTo(HaveOccurred()) - Expect(resGet).To(Equal("[[10,20,30,40],[5,10,20,30]]")) - - resArr, err := client.JSONArrIndex(ctx, "doc1", "$.store.book[?(@.price<10)].size", 20).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(resArr).To(Equal([]int64{1, 2})) - }) - - It("should JSONArrInsert", Label("json.arrinsert", "json"), func() { - cmd1 := client.JSONSet(ctx, "insert2", "$", `[100, 200, 300, 200]`) - Expect(cmd1.Err()).NotTo(HaveOccurred()) - Expect(cmd1.Val()).To(Equal("OK")) - - cmd2 := client.JSONArrInsert(ctx, "insert2", "$", -1, 1, 2) - Expect(cmd2.Err()).NotTo(HaveOccurred()) - Expect(cmd2.Val()).To(Equal([]int64{6})) - - cmd3 := client.JSONGet(ctx, "insert2") - Expect(cmd3.Err()).NotTo(HaveOccurred()) - // RESP2 vs RESP3 - Expect(cmd3.Val()).To(Or( - Equal(`[100,200,300,1,2,200]`), - Equal(`[[100,200,300,1,2,200]]`))) - }) - - It("should JSONArrLen", Label("json.arrlen", "json"), func() { - cmd1 := client.JSONSet(ctx, "length2", "$", `{"a": [10], "b": {"a": [12, 10, 20, 12, 90, 10]}}`) - Expect(cmd1.Err()).NotTo(HaveOccurred()) - Expect(cmd1.Val()).To(Equal("OK")) - - cmd2 := client.JSONArrLen(ctx, "length2", "$..a") - Expect(cmd2.Err()).NotTo(HaveOccurred()) - Expect(cmd2.Val()).To(Equal([]int64{1, 6})) - }) - - It("should JSONArrPop", Label("json.arrpop"), func() { - cmd1 := client.JSONSet(ctx, "pop4", "$", `[100, 200, 300, 200]`) - Expect(cmd1.Err()).NotTo(HaveOccurred()) - Expect(cmd1.Val()).To(Equal("OK")) - - cmd2 := client.JSONArrPop(ctx, "pop4", "$", 2) - Expect(cmd2.Err()).NotTo(HaveOccurred()) - Expect(cmd2.Val()).To(Equal([]string{"300"})) - - cmd3 := client.JSONGet(ctx, "pop4", "$") - Expect(cmd3.Err()).NotTo(HaveOccurred()) - Expect(cmd3.Val()).To(Equal("[[100,200,200]]")) - }) - - It("should JSONArrTrim", Label("json.arrtrim", "json"), func() { - cmd1, err := client.JSONSet(ctx, "trim1", "$", `[0,1,2,3,4]`).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(cmd1).To(Equal("OK")) - - stop := 3 - cmd2, err := client.JSONArrTrimWithArgs(ctx, "trim1", "$", &redis.JSONArrTrimArgs{Start: 1, Stop: &stop}).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(cmd2).To(Equal([]int64{3})) - - res, err := client.JSONGet(ctx, "trim1", "$").Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res).To(Equal(`[[1,2,3]]`)) - - cmd3, err := client.JSONSet(ctx, "trim2", "$", `[0,1,2,3,4]`).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(cmd3).To(Equal("OK")) - - stop = 3 - cmd4, err := client.JSONArrTrimWithArgs(ctx, "trim2", "$", &redis.JSONArrTrimArgs{Start: -1, Stop: &stop}).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(cmd4).To(Equal([]int64{0})) - - cmd5, err := client.JSONSet(ctx, "trim3", "$", `[0,1,2,3,4]`).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(cmd5).To(Equal("OK")) - - stop = 99 - cmd6, err := client.JSONArrTrimWithArgs(ctx, "trim3", "$", &redis.JSONArrTrimArgs{Start: 3, Stop: &stop}).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(cmd6).To(Equal([]int64{2})) - - cmd7, err := client.JSONSet(ctx, "trim4", "$", `[0,1,2,3,4]`).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(cmd7).To(Equal("OK")) - - stop = 1 - cmd8, err := client.JSONArrTrimWithArgs(ctx, "trim4", "$", &redis.JSONArrTrimArgs{Start: 9, Stop: &stop}).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(cmd8).To(Equal([]int64{0})) - - cmd9, err := client.JSONSet(ctx, "trim5", "$", `[0,1,2,3,4]`).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(cmd9).To(Equal("OK")) - - stop = 11 - cmd10, err := client.JSONArrTrimWithArgs(ctx, "trim5", "$", &redis.JSONArrTrimArgs{Start: 9, Stop: &stop}).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(cmd10).To(Equal([]int64{0})) - }) - - It("should JSONArrPop", Label("json.arrpop", "json"), func() { - cmd1 := client.JSONSet(ctx, "pop4", "$", `[100, 200, 300, 200]`) - Expect(cmd1.Err()).NotTo(HaveOccurred()) - Expect(cmd1.Val()).To(Equal("OK")) - - cmd2 := client.JSONArrPop(ctx, "pop4", "$", 2) - Expect(cmd2.Err()).NotTo(HaveOccurred()) - Expect(cmd2.Val()).To(Equal([]string{"300"})) - - cmd3 := client.JSONGet(ctx, "pop4", "$") - Expect(cmd3.Err()).NotTo(HaveOccurred()) - Expect(cmd3.Val()).To(Equal("[[100,200,200]]")) - }) + AfterEach(func() { + if client != nil { + client.FlushDB(ctx) + client.Close() + } }) - Describe("get/set", Label("getset"), func() { - It("should JSONSet", Label("json.set", "json"), func() { - cmd := client.JSONSet(ctx, "set1", "$", `{"a": 1, "b": 2, "hello": "world"}`) - Expect(cmd.Err()).NotTo(HaveOccurred()) - Expect(cmd.Val()).To(Equal("OK")) + protocols := []int{2, 3} + for _, protocol := range protocols { + BeforeEach(func() { + client = setupRedisClient(protocol) + Expect(client.FlushAll(ctx).Err()).NotTo(HaveOccurred()) }) - It("should JSONGet", Label("json.get", "json", "NonRedisEnterprise"), func() { - res, err := client.JSONSet(ctx, "get3", "$", `{"a": 1, "b": 2}`).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res).To(Equal("OK")) - - res, err = client.JSONGetWithArgs(ctx, "get3", &redis.JSONGetArgs{Indent: "-"}).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res).To(Equal(`{-"a":1,-"b":2}`)) - - res, err = client.JSONGetWithArgs(ctx, "get3", &redis.JSONGetArgs{Indent: "-", Newline: `~`, Space: `!`}).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res).To(Equal(`{~-"a":!1,~-"b":!2~}`)) + Describe("arrays", Label("arrays"), func() { + It("should JSONArrAppend", Label("json.arrappend", "json"), func() { + cmd1 := client.JSONSet(ctx, "append2", "$", `{"a": [10], "b": {"a": [12, 13]}}`) + Expect(cmd1.Err()).NotTo(HaveOccurred()) + Expect(cmd1.Val()).To(Equal("OK")) + + cmd2 := client.JSONArrAppend(ctx, "append2", "$..a", 10) + Expect(cmd2.Err()).NotTo(HaveOccurred()) + Expect(cmd2.Val()).To(Equal([]int64{2, 3})) + }) + + It("should JSONArrIndex and JSONArrIndexWithArgs", Label("json.arrindex", "json"), func() { + cmd1, err := client.JSONSet(ctx, "index1", "$", `{"a": [10], "b": {"a": [12, 10]}}`).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(cmd1).To(Equal("OK")) + + cmd2, err := client.JSONArrIndex(ctx, "index1", "$.b.a", 10).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(cmd2).To(Equal([]int64{1})) + + cmd3, err := client.JSONSet(ctx, "index2", "$", `[0,1,2,3,4]`).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(cmd3).To(Equal("OK")) + + res, err := client.JSONArrIndex(ctx, "index2", "$", 1).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res[0]).To(Equal(int64(1))) + + res, err = client.JSONArrIndex(ctx, "index2", "$", 1, 2).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res[0]).To(Equal(int64(-1))) + + res, err = client.JSONArrIndex(ctx, "index2", "$", 4).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res[0]).To(Equal(int64(4))) + + res, err = client.JSONArrIndexWithArgs(ctx, "index2", "$", &redis.JSONArrIndexArgs{}, 4).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res[0]).To(Equal(int64(4))) + + stop := 5000 + res, err = client.JSONArrIndexWithArgs(ctx, "index2", "$", &redis.JSONArrIndexArgs{Stop: &stop}, 4).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res[0]).To(Equal(int64(4))) + + stop = -1 + res, err = client.JSONArrIndexWithArgs(ctx, "index2", "$", &redis.JSONArrIndexArgs{Stop: &stop}, 4).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res[0]).To(Equal(int64(-1))) + }) + + It("should JSONArrIndex and JSONArrIndexWithArgs with $", Label("json.arrindex", "json"), func() { + doc := `{ + "store": { + "book": [ + { + "category": "reference", + "author": "Nigel Rees", + "title": "Sayings of the Century", + "price": 8.95, + "size": [10, 20, 30, 40] + }, + { + "category": "fiction", + "author": "Evelyn Waugh", + "title": "Sword of Honour", + "price": 12.99, + "size": [50, 60, 70, 80] + }, + { + "category": "fiction", + "author": "Herman Melville", + "title": "Moby Dick", + "isbn": "0-553-21311-3", + "price": 8.99, + "size": [5, 10, 20, 30] + }, + { + "category": "fiction", + "author": "J. R. R. Tolkien", + "title": "The Lord of the Rings", + "isbn": "0-395-19395-8", + "price": 22.99, + "size": [5, 6, 7, 8] + } + ], + "bicycle": {"color": "red", "price": 19.95} + } + }` + res, err := client.JSONSet(ctx, "doc1", "$", doc).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal("OK")) + + resGet, err := client.JSONGet(ctx, "doc1", "$.store.book[?(@.price<10)].size").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(resGet).To(Equal("[[10,20,30,40],[5,10,20,30]]")) + + resArr, err := client.JSONArrIndex(ctx, "doc1", "$.store.book[?(@.price<10)].size", 20).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(resArr).To(Equal([]int64{1, 2})) + }) + + It("should JSONArrInsert", Label("json.arrinsert", "json"), func() { + cmd1 := client.JSONSet(ctx, "insert2", "$", `[100, 200, 300, 200]`) + Expect(cmd1.Err()).NotTo(HaveOccurred()) + Expect(cmd1.Val()).To(Equal("OK")) + + cmd2 := client.JSONArrInsert(ctx, "insert2", "$", -1, 1, 2) + Expect(cmd2.Err()).NotTo(HaveOccurred()) + Expect(cmd2.Val()).To(Equal([]int64{6})) + + cmd3 := client.JSONGet(ctx, "insert2") + Expect(cmd3.Err()).NotTo(HaveOccurred()) + // RESP2 vs RESP3 + Expect(cmd3.Val()).To(Or( + Equal(`[100,200,300,1,2,200]`), + Equal(`[[100,200,300,1,2,200]]`))) + }) + + It("should JSONArrLen", Label("json.arrlen", "json"), func() { + cmd1 := client.JSONSet(ctx, "length2", "$", `{"a": [10], "b": {"a": [12, 10, 20, 12, 90, 10]}}`) + Expect(cmd1.Err()).NotTo(HaveOccurred()) + Expect(cmd1.Val()).To(Equal("OK")) + + cmd2 := client.JSONArrLen(ctx, "length2", "$..a") + Expect(cmd2.Err()).NotTo(HaveOccurred()) + Expect(cmd2.Val()).To(Equal([]int64{1, 6})) + }) + + It("should JSONArrPop", Label("json.arrpop"), func() { + cmd1 := client.JSONSet(ctx, "pop4", "$", `[100, 200, 300, 200]`) + Expect(cmd1.Err()).NotTo(HaveOccurred()) + Expect(cmd1.Val()).To(Equal("OK")) + + cmd2 := client.JSONArrPop(ctx, "pop4", "$", 2) + Expect(cmd2.Err()).NotTo(HaveOccurred()) + Expect(cmd2.Val()).To(Equal([]string{"300"})) + + cmd3 := client.JSONGet(ctx, "pop4", "$") + Expect(cmd3.Err()).NotTo(HaveOccurred()) + Expect(cmd3.Val()).To(Equal("[[100,200,200]]")) + }) + + It("should JSONArrTrim", Label("json.arrtrim", "json"), func() { + cmd1, err := client.JSONSet(ctx, "trim1", "$", `[0,1,2,3,4]`).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(cmd1).To(Equal("OK")) + + stop := 3 + cmd2, err := client.JSONArrTrimWithArgs(ctx, "trim1", "$", &redis.JSONArrTrimArgs{Start: 1, Stop: &stop}).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(cmd2).To(Equal([]int64{3})) + + res, err := client.JSONGet(ctx, "trim1", "$").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal(`[[1,2,3]]`)) + + cmd3, err := client.JSONSet(ctx, "trim2", "$", `[0,1,2,3,4]`).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(cmd3).To(Equal("OK")) + + stop = 3 + cmd4, err := client.JSONArrTrimWithArgs(ctx, "trim2", "$", &redis.JSONArrTrimArgs{Start: -1, Stop: &stop}).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(cmd4).To(Equal([]int64{0})) + + cmd5, err := client.JSONSet(ctx, "trim3", "$", `[0,1,2,3,4]`).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(cmd5).To(Equal("OK")) + + stop = 99 + cmd6, err := client.JSONArrTrimWithArgs(ctx, "trim3", "$", &redis.JSONArrTrimArgs{Start: 3, Stop: &stop}).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(cmd6).To(Equal([]int64{2})) + + cmd7, err := client.JSONSet(ctx, "trim4", "$", `[0,1,2,3,4]`).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(cmd7).To(Equal("OK")) + + stop = 1 + cmd8, err := client.JSONArrTrimWithArgs(ctx, "trim4", "$", &redis.JSONArrTrimArgs{Start: 9, Stop: &stop}).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(cmd8).To(Equal([]int64{0})) + + cmd9, err := client.JSONSet(ctx, "trim5", "$", `[0,1,2,3,4]`).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(cmd9).To(Equal("OK")) + + stop = 11 + cmd10, err := client.JSONArrTrimWithArgs(ctx, "trim5", "$", &redis.JSONArrTrimArgs{Start: 9, Stop: &stop}).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(cmd10).To(Equal([]int64{0})) + }) + + It("should JSONArrPop", Label("json.arrpop", "json"), func() { + cmd1 := client.JSONSet(ctx, "pop4", "$", `[100, 200, 300, 200]`) + Expect(cmd1.Err()).NotTo(HaveOccurred()) + Expect(cmd1.Val()).To(Equal("OK")) + + cmd2 := client.JSONArrPop(ctx, "pop4", "$", 2) + Expect(cmd2.Err()).NotTo(HaveOccurred()) + Expect(cmd2.Val()).To(Equal([]string{"300"})) + + cmd3 := client.JSONGet(ctx, "pop4", "$") + Expect(cmd3.Err()).NotTo(HaveOccurred()) + Expect(cmd3.Val()).To(Equal("[[100,200,200]]")) + }) }) - It("should JSONMerge", Label("json.merge", "json"), func() { - res, err := client.JSONSet(ctx, "merge1", "$", `{"a": 1, "b": 2}`).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res).To(Equal("OK")) - - res, err = client.JSONMerge(ctx, "merge1", "$", `{"b": 3, "c": 4}`).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res).To(Equal("OK")) - - res, err = client.JSONGet(ctx, "merge1", "$").Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res).To(Equal(`[{"a":1,"b":3,"c":4}]`)) + Describe("get/set", Label("getset"), func() { + It("should JSONSet", Label("json.set", "json"), func() { + cmd := client.JSONSet(ctx, "set1", "$", `{"a": 1, "b": 2, "hello": "world"}`) + Expect(cmd.Err()).NotTo(HaveOccurred()) + Expect(cmd.Val()).To(Equal("OK")) + }) + + It("should JSONGet", Label("json.get", "json", "NonRedisEnterprise"), func() { + res, err := client.JSONSet(ctx, "get3", "$", `{"a": 1, "b": 2}`).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal("OK")) + + res, err = client.JSONGetWithArgs(ctx, "get3", &redis.JSONGetArgs{Indent: "-"}).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal(`{-"a":1,-"b":2}`)) + + res, err = client.JSONGetWithArgs(ctx, "get3", &redis.JSONGetArgs{Indent: "-", Newline: `~`, Space: `!`}).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal(`{~-"a":!1,~-"b":!2~}`)) + }) + + It("should JSONMerge", Label("json.merge", "json"), func() { + res, err := client.JSONSet(ctx, "merge1", "$", `{"a": 1, "b": 2}`).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal("OK")) + + res, err = client.JSONMerge(ctx, "merge1", "$", `{"b": 3, "c": 4}`).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal("OK")) + + res, err = client.JSONGet(ctx, "merge1", "$").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal(`[{"a":1,"b":3,"c":4}]`)) + }) + + It("should JSONMSet", Label("json.mset", "json", "NonRedisEnterprise"), func() { + doc1 := redis.JSONSetArgs{Key: "mset1", Path: "$", Value: `{"a": 1}`} + doc2 := redis.JSONSetArgs{Key: "mset2", Path: "$", Value: 2} + docs := []redis.JSONSetArgs{doc1, doc2} + + mSetResult, err := client.JSONMSetArgs(ctx, docs).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(mSetResult).To(Equal("OK")) + + res, err := client.JSONMGet(ctx, "$", "mset1").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal([]interface{}{`[{"a":1}]`})) + + res, err = client.JSONMGet(ctx, "$", "mset1", "mset2").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal([]interface{}{`[{"a":1}]`, "[2]"})) + + _, err = client.JSONMSet(ctx, "mset1", "$.a", 2, "mset3", "$", `[1]`).Result() + Expect(err).NotTo(HaveOccurred()) + }) + + It("should JSONMGet", Label("json.mget", "json", "NonRedisEnterprise"), func() { + cmd1 := client.JSONSet(ctx, "mget2a", "$", `{"a": ["aa", "ab", "ac", "ad"], "b": {"a": ["ba", "bb", "bc", "bd"]}}`) + Expect(cmd1.Err()).NotTo(HaveOccurred()) + Expect(cmd1.Val()).To(Equal("OK")) + cmd2 := client.JSONSet(ctx, "mget2b", "$", `{"a": [100, 200, 300, 200], "b": {"a": [100, 200, 300, 200]}}`) + Expect(cmd2.Err()).NotTo(HaveOccurred()) + Expect(cmd2.Val()).To(Equal("OK")) + + cmd3 := client.JSONMGet(ctx, "$..a", "mget2a", "mget2b") + Expect(cmd3.Err()).NotTo(HaveOccurred()) + Expect(cmd3.Val()).To(HaveLen(2)) + Expect(cmd3.Val()[0]).To(Equal(`[["aa","ab","ac","ad"],["ba","bb","bc","bd"]]`)) + Expect(cmd3.Val()[1]).To(Equal(`[[100,200,300,200],[100,200,300,200]]`)) + }) + + It("should JSONMget with $", Label("json.mget", "json", "NonRedisEnterprise"), func() { + res, err := client.JSONSet(ctx, "doc1", "$", `{"a": 1, "b": 2, "nested": {"a": 3}, "c": "", "nested2": {"a": ""}}`).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal("OK")) + + res, err = client.JSONSet(ctx, "doc2", "$", `{"a": 4, "b": 5, "nested": {"a": 6}, "c": "", "nested2": {"a": [""]}}`).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal("OK")) + + iRes, err := client.JSONMGet(ctx, "$..a", "doc1").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(iRes).To(Equal([]interface{}{`[1,3,""]`})) + + iRes, err = client.JSONMGet(ctx, "$..a", "doc1", "doc2").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(iRes).To(Equal([]interface{}{`[1,3,""]`, `[4,6,[""]]`})) + + iRes, err = client.JSONMGet(ctx, "$..a", "non_existing_doc", "non_existing_doc1").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(iRes).To(Equal([]interface{}{nil, nil})) + }) }) - It("should JSONMSet", Label("json.mset", "json", "NonRedisEnterprise"), func() { - doc1 := redis.JSONSetArgs{Key: "mset1", Path: "$", Value: `{"a": 1}`} - doc2 := redis.JSONSetArgs{Key: "mset2", Path: "$", Value: 2} - docs := []redis.JSONSetArgs{doc1, doc2} - - mSetResult, err := client.JSONMSetArgs(ctx, docs).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(mSetResult).To(Equal("OK")) - - res, err := client.JSONMGet(ctx, "$", "mset1").Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res).To(Equal([]interface{}{`[{"a":1}]`})) - - res, err = client.JSONMGet(ctx, "$", "mset1", "mset2").Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res).To(Equal([]interface{}{`[{"a":1}]`, "[2]"})) - - _, err = client.JSONMSet(ctx, "mset1", "$.a", 2, "mset3", "$", `[1]`).Result() - Expect(err).NotTo(HaveOccurred()) + Describe("Misc", Label("misc"), func() { + It("should JSONClear", Label("json.clear", "json"), func() { + cmd1 := client.JSONSet(ctx, "clear1", "$", `[1]`) + Expect(cmd1.Err()).NotTo(HaveOccurred()) + Expect(cmd1.Val()).To(Equal("OK")) + + cmd2 := client.JSONClear(ctx, "clear1", "$") + Expect(cmd2.Err()).NotTo(HaveOccurred()) + Expect(cmd2.Val()).To(Equal(int64(1))) + + cmd3 := client.JSONGet(ctx, "clear1", "$") + Expect(cmd3.Err()).NotTo(HaveOccurred()) + Expect(cmd3.Val()).To(Equal(`[[]]`)) + }) + + It("should JSONClear with $", Label("json.clear", "json"), func() { + doc := `{ + "nested1": {"a": {"foo": 10, "bar": 20}}, + "a": ["foo"], + "nested2": {"a": "claro"}, + "nested3": {"a": {"baz": 50}} + }` + res, err := client.JSONSet(ctx, "doc1", "$", doc).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal("OK")) + + iRes, err := client.JSONClear(ctx, "doc1", "$..a").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(iRes).To(Equal(int64(3))) + + resGet, err := client.JSONGet(ctx, "doc1", `$`).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(resGet).To(Equal(`[{"nested1":{"a":{}},"a":[],"nested2":{"a":"claro"},"nested3":{"a":{}}}]`)) + + res, err = client.JSONSet(ctx, "doc1", "$", doc).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal("OK")) + + iRes, err = client.JSONClear(ctx, "doc1", "$.nested1.a").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(iRes).To(Equal(int64(1))) + + resGet, err = client.JSONGet(ctx, "doc1", `$`).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(resGet).To(Equal(`[{"nested1":{"a":{}},"a":["foo"],"nested2":{"a":"claro"},"nested3":{"a":{"baz":50}}}]`)) + }) + + It("should JSONDel", Label("json.del", "json"), func() { + cmd1 := client.JSONSet(ctx, "del1", "$", `[1]`) + Expect(cmd1.Err()).NotTo(HaveOccurred()) + Expect(cmd1.Val()).To(Equal("OK")) + + cmd2 := client.JSONDel(ctx, "del1", "$") + Expect(cmd2.Err()).NotTo(HaveOccurred()) + Expect(cmd2.Val()).To(Equal(int64(1))) + + cmd3 := client.JSONGet(ctx, "del1", "$") + Expect(cmd3.Err()).NotTo(HaveOccurred()) + Expect(cmd3.Val()).To(HaveLen(0)) + }) + + It("should JSONDel with $", Label("json.del", "json"), func() { + res, err := client.JSONSet(ctx, "del1", "$", `{"a": 1, "nested": {"a": 2, "b": 3}}`).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal("OK")) + + iRes, err := client.JSONDel(ctx, "del1", "$..a").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(iRes).To(Equal(int64(2))) + + resGet, err := client.JSONGet(ctx, "del1", "$").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(resGet).To(Equal(`[{"nested":{"b":3}}]`)) + + res, err = client.JSONSet(ctx, "del2", "$", `{"a": {"a": 2, "b": 3}, "b": ["a", "b"], "nested": {"b": [true, "a", "b"]}}`).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal("OK")) + + iRes, err = client.JSONDel(ctx, "del2", "$..a").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(iRes).To(Equal(int64(1))) + + resGet, err = client.JSONGet(ctx, "del2", "$").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(resGet).To(Equal(`[{"nested":{"b":[true,"a","b"]},"b":["a","b"]}]`)) + + doc := `[ + { + "ciao": ["non ancora"], + "nested": [ + {"ciao": [1, "a"]}, + {"ciao": [2, "a"]}, + {"ciaoc": [3, "non", "ciao"]}, + {"ciao": [4, "a"]}, + {"e": [5, "non", "ciao"]} + ] + } + ]` + res, err = client.JSONSet(ctx, "del3", "$", doc).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal("OK")) + + iRes, err = client.JSONDel(ctx, "del3", `$.[0]["nested"]..ciao`).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(iRes).To(Equal(int64(3))) + + resVal := `[[{"ciao":["non ancora"],"nested":[{},{},{"ciaoc":[3,"non","ciao"]},{},{"e":[5,"non","ciao"]}]}]]` + resGet, err = client.JSONGet(ctx, "del3", "$").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(resGet).To(Equal(resVal)) + }) + + It("should JSONForget", Label("json.forget", "json"), func() { + cmd1 := client.JSONSet(ctx, "forget3", "$", `{"a": [1,2,3], "b": {"a": [1,2,3], "b": "annie"}}`) + Expect(cmd1.Err()).NotTo(HaveOccurred()) + Expect(cmd1.Val()).To(Equal("OK")) + + cmd2 := client.JSONForget(ctx, "forget3", "$..a") + Expect(cmd2.Err()).NotTo(HaveOccurred()) + Expect(cmd2.Val()).To(Equal(int64(2))) + + cmd3 := client.JSONGet(ctx, "forget3", "$") + Expect(cmd3.Err()).NotTo(HaveOccurred()) + Expect(cmd3.Val()).To(Equal(`[{"b":{"b":"annie"}}]`)) + }) + + It("should JSONForget with $", Label("json.forget", "json"), func() { + res, err := client.JSONSet(ctx, "doc1", "$", `{"a": 1, "nested": {"a": 2, "b": 3}}`).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal("OK")) + + iRes, err := client.JSONForget(ctx, "doc1", "$..a").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(iRes).To(Equal(int64(2))) + + resGet, err := client.JSONGet(ctx, "doc1", "$").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(resGet).To(Equal(`[{"nested":{"b":3}}]`)) + + res, err = client.JSONSet(ctx, "doc2", "$", `{"a": {"a": 2, "b": 3}, "b": ["a", "b"], "nested": {"b": [true, "a", "b"]}}`).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal("OK")) + + iRes, err = client.JSONForget(ctx, "doc2", "$..a").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(iRes).To(Equal(int64(1))) + + resGet, err = client.JSONGet(ctx, "doc2", "$").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(resGet).To(Equal(`[{"nested":{"b":[true,"a","b"]},"b":["a","b"]}]`)) + + doc := `[ + { + "ciao": ["non ancora"], + "nested": [ + {"ciao": [1, "a"]}, + {"ciao": [2, "a"]}, + {"ciaoc": [3, "non", "ciao"]}, + {"ciao": [4, "a"]}, + {"e": [5, "non", "ciao"]} + ] + } + ]` + res, err = client.JSONSet(ctx, "doc3", "$", doc).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal("OK")) + + iRes, err = client.JSONForget(ctx, "doc3", `$.[0]["nested"]..ciao`).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(iRes).To(Equal(int64(3))) + + resVal := `[[{"ciao":["non ancora"],"nested":[{},{},{"ciaoc":[3,"non","ciao"]},{},{"e":[5,"non","ciao"]}]}]]` + resGet, err = client.JSONGet(ctx, "doc3", "$").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(resGet).To(Equal(resVal)) + }) + + It("should JSONNumIncrBy", Label("json.numincrby", "json"), func() { + cmd1 := client.JSONSet(ctx, "incr3", "$", `{"a": [1, 2], "b": {"a": [0, -1]}}`) + Expect(cmd1.Err()).NotTo(HaveOccurred()) + Expect(cmd1.Val()).To(Equal("OK")) + + cmd2 := client.JSONNumIncrBy(ctx, "incr3", "$..a[1]", float64(1)) + Expect(cmd2.Err()).NotTo(HaveOccurred()) + Expect(cmd2.Val()).To(Equal(`[3,0]`)) + }) + + It("should JSONNumIncrBy with $", Label("json.numincrby", "json"), func() { + res, err := client.JSONSet(ctx, "doc1", "$", `{"a": "b", "b": [{"a": 2}, {"a": 5.0}, {"a": "c"}]}`).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal("OK")) + + res, err = client.JSONNumIncrBy(ctx, "doc1", "$.b[1].a", 2).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal(`[7]`)) + + res, err = client.JSONNumIncrBy(ctx, "doc1", "$.b[1].a", 3.5).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal(`[10.5]`)) + + res, err = client.JSONSet(ctx, "doc2", "$", `{"a": "b", "b": [{"a": 2}, {"a": 5.0}, {"a": "c"}]}`).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal("OK")) + + res, err = client.JSONNumIncrBy(ctx, "doc2", "$.b[0].a", 3).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal(`[5]`)) + }) + + It("should JSONObjKeys", Label("json.objkeys", "json"), func() { + cmd1 := client.JSONSet(ctx, "objkeys1", "$", `{"a": [1, 2], "b": {"a": [0, -1]}}`) + Expect(cmd1.Err()).NotTo(HaveOccurred()) + Expect(cmd1.Val()).To(Equal("OK")) + + cmd2 := client.JSONObjKeys(ctx, "objkeys1", "$..*") + Expect(cmd2.Err()).NotTo(HaveOccurred()) + Expect(cmd2.Val()).To(HaveLen(7)) + Expect(cmd2.Val()).To(Equal([]interface{}{nil, []interface{}{"a"}, nil, nil, nil, nil, nil})) + }) + + It("should JSONObjKeys with $", Label("json.objkeys", "json"), func() { + doc := `{ + "nested1": {"a": {"foo": 10, "bar": 20}}, + "a": ["foo"], + "nested2": {"a": {"baz": 50}} + }` + cmd1, err := client.JSONSet(ctx, "objkeys1", "$", doc).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(cmd1).To(Equal("OK")) + + cmd2, err := client.JSONObjKeys(ctx, "objkeys1", "$.nested1.a").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(cmd2).To(Equal([]interface{}{[]interface{}{"foo", "bar"}})) + + cmd2, err = client.JSONObjKeys(ctx, "objkeys1", ".*.a").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(cmd2).To(Equal([]interface{}{"foo", "bar"})) + + cmd2, err = client.JSONObjKeys(ctx, "objkeys1", ".nested2.a").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(cmd2).To(Equal([]interface{}{"baz"})) + + _, err = client.JSONObjKeys(ctx, "non_existing_doc", "..a").Result() + Expect(err).To(HaveOccurred()) + }) + + It("should JSONObjLen", Label("json.objlen", "json"), func() { + cmd1 := client.JSONSet(ctx, "objlen2", "$", `{"a": [1, 2], "b": {"a": [0, -1]}}`) + Expect(cmd1.Err()).NotTo(HaveOccurred()) + Expect(cmd1.Val()).To(Equal("OK")) + + cmd2 := client.JSONObjLen(ctx, "objlen2", "$..*") + Expect(cmd2.Err()).NotTo(HaveOccurred()) + Expect(cmd2.Val()).To(HaveLen(7)) + Expect(cmd2.Val()[0]).To(BeNil()) + Expect(*cmd2.Val()[1]).To(Equal(int64(1))) + }) + + It("should JSONStrLen", Label("json.strlen", "json"), func() { + cmd1 := client.JSONSet(ctx, "strlen2", "$", `{"a": "alice", "b": "bob", "c": {"a": "alice", "b": "bob"}}`) + Expect(cmd1.Err()).NotTo(HaveOccurred()) + Expect(cmd1.Val()).To(Equal("OK")) + + cmd2 := client.JSONStrLen(ctx, "strlen2", "$..*") + Expect(cmd2.Err()).NotTo(HaveOccurred()) + Expect(cmd2.Val()).To(HaveLen(5)) + var tmp int64 = 20 + Expect(cmd2.Val()[0]).To(BeAssignableToTypeOf(&tmp)) + Expect(*cmd2.Val()[0]).To(Equal(int64(5))) + Expect(*cmd2.Val()[1]).To(Equal(int64(3))) + Expect(cmd2.Val()[2]).To(BeNil()) + Expect(*cmd2.Val()[3]).To(Equal(int64(5))) + Expect(*cmd2.Val()[4]).To(Equal(int64(3))) + }) + + It("should JSONStrAppend", Label("json.strappend", "json"), func() { + cmd1, err := client.JSONSet(ctx, "strapp1", "$", `"foo"`).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(cmd1).To(Equal("OK")) + cmd2, err := client.JSONStrAppend(ctx, "strapp1", "$", `"bar"`).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(*cmd2[0]).To(Equal(int64(6))) + cmd3, err := client.JSONGet(ctx, "strapp1", "$").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(cmd3).To(Equal(`["foobar"]`)) + }) + + It("should JSONStrAppend and JSONStrLen with $", Label("json.strappend", "json.strlen", "json"), func() { + res, err := client.JSONSet(ctx, "doc1", "$", `{"a": "foo", "nested1": {"a": "hello"}, "nested2": {"a": 31}}`).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal("OK")) + + intArrayResult, err := client.JSONStrAppend(ctx, "doc1", "$.nested1.a", `"baz"`).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(*intArrayResult[0]).To(Equal(int64(8))) + + res, err = client.JSONSet(ctx, "doc2", "$", `{"a": "foo", "nested1": {"a": "hello"}, "nested2": {"a": 31}}`).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal("OK")) + + intResult, err := client.JSONStrLen(ctx, "doc2", "$.nested1.a").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(*intResult[0]).To(Equal(int64(5))) + }) + + It("should JSONToggle", Label("json.toggle", "json"), func() { + cmd1 := client.JSONSet(ctx, "toggle1", "$", `[true]`) + Expect(cmd1.Err()).NotTo(HaveOccurred()) + Expect(cmd1.Val()).To(Equal("OK")) + + cmd2 := client.JSONToggle(ctx, "toggle1", "$[0]") + Expect(cmd2.Err()).NotTo(HaveOccurred()) + Expect(cmd2.Val()).To(HaveLen(1)) + Expect(*cmd2.Val()[0]).To(Equal(int64(0))) + }) + + It("should JSONType", Label("json.type", "json"), func() { + cmd1 := client.JSONSet(ctx, "type1", "$", `[true]`) + Expect(cmd1.Err()).NotTo(HaveOccurred()) + Expect(cmd1.Val()).To(Equal("OK")) + + cmd2 := client.JSONType(ctx, "type1", "$[0]") + Expect(cmd2.Err()).NotTo(HaveOccurred()) + Expect(cmd2.Val()).To(HaveLen(1)) + // RESP2 v RESP3 + Expect(cmd2.Val()[0]).To(Or(Equal([]interface{}{"boolean"}), Equal("boolean"))) + }) }) + } +}) - It("should JSONMGet", Label("json.mget", "json", "NonRedisEnterprise"), func() { - cmd1 := client.JSONSet(ctx, "mget2a", "$", `{"a": ["aa", "ab", "ac", "ad"], "b": {"a": ["ba", "bb", "bc", "bd"]}}`) - Expect(cmd1.Err()).NotTo(HaveOccurred()) - Expect(cmd1.Val()).To(Equal("OK")) - cmd2 := client.JSONSet(ctx, "mget2b", "$", `{"a": [100, 200, 300, 200], "b": {"a": [100, 200, 300, 200]}}`) - Expect(cmd2.Err()).NotTo(HaveOccurred()) - Expect(cmd2.Val()).To(Equal("OK")) - - cmd3 := client.JSONMGet(ctx, "$..a", "mget2a", "mget2b") - Expect(cmd3.Err()).NotTo(HaveOccurred()) - Expect(cmd3.Val()).To(HaveLen(2)) - Expect(cmd3.Val()[0]).To(Equal(`[["aa","ab","ac","ad"],["ba","bb","bc","bd"]]`)) - Expect(cmd3.Val()[1]).To(Equal(`[[100,200,300,200],[100,200,300,200]]`)) +var _ = Describe("Go-Redis Advanced JSON and RediSearch Tests", func() { + var client *redis.Client + var ctx = context.Background() + + setupRedisClient := func(protocolVersion int) *redis.Client { + return redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + DB: 0, + Protocol: protocolVersion, // Setting RESP2 or RESP3 protocol + UnstableResp3: true, // Enable RESP3 features }) + } - It("should JSONMget with $", Label("json.mget", "json", "NonRedisEnterprise"), func() { - res, err := client.JSONSet(ctx, "doc1", "$", `{"a": 1, "b": 2, "nested": {"a": 3}, "c": "", "nested2": {"a": ""}}`).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res).To(Equal("OK")) - - res, err = client.JSONSet(ctx, "doc2", "$", `{"a": 4, "b": 5, "nested": {"a": 6}, "c": "", "nested2": {"a": [""]}}`).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res).To(Equal("OK")) - - iRes, err := client.JSONMGet(ctx, "$..a", "doc1").Result() - Expect(err).NotTo(HaveOccurred()) - Expect(iRes).To(Equal([]interface{}{`[1,3,""]`})) - - iRes, err = client.JSONMGet(ctx, "$..a", "doc1", "doc2").Result() - Expect(err).NotTo(HaveOccurred()) - Expect(iRes).To(Equal([]interface{}{`[1,3,""]`, `[4,6,[""]]`})) - - iRes, err = client.JSONMGet(ctx, "$..a", "non_existing_doc", "non_existing_doc1").Result() - Expect(err).NotTo(HaveOccurred()) - Expect(iRes).To(Equal([]interface{}{nil, nil})) - }) + AfterEach(func() { + if client != nil { + client.FlushDB(ctx) + client.Close() + } }) - Describe("Misc", Label("misc"), func() { - It("should JSONClear", Label("json.clear", "json"), func() { - cmd1 := client.JSONSet(ctx, "clear1", "$", `[1]`) - Expect(cmd1.Err()).NotTo(HaveOccurred()) - Expect(cmd1.Val()).To(Equal("OK")) - - cmd2 := client.JSONClear(ctx, "clear1", "$") - Expect(cmd2.Err()).NotTo(HaveOccurred()) - Expect(cmd2.Val()).To(Equal(int64(1))) - - cmd3 := client.JSONGet(ctx, "clear1", "$") - Expect(cmd3.Err()).NotTo(HaveOccurred()) - Expect(cmd3.Val()).To(Equal(`[[]]`)) - }) - - It("should JSONClear with $", Label("json.clear", "json"), func() { - doc := `{ - "nested1": {"a": {"foo": 10, "bar": 20}}, - "a": ["foo"], - "nested2": {"a": "claro"}, - "nested3": {"a": {"baz": 50}} - }` - res, err := client.JSONSet(ctx, "doc1", "$", doc).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res).To(Equal("OK")) - - iRes, err := client.JSONClear(ctx, "doc1", "$..a").Result() - Expect(err).NotTo(HaveOccurred()) - Expect(iRes).To(Equal(int64(3))) - - resGet, err := client.JSONGet(ctx, "doc1", `$`).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(resGet).To(Equal(`[{"nested1":{"a":{}},"a":[],"nested2":{"a":"claro"},"nested3":{"a":{}}}]`)) - - res, err = client.JSONSet(ctx, "doc1", "$", doc).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res).To(Equal("OK")) - - iRes, err = client.JSONClear(ctx, "doc1", "$.nested1.a").Result() - Expect(err).NotTo(HaveOccurred()) - Expect(iRes).To(Equal(int64(1))) - - resGet, err = client.JSONGet(ctx, "doc1", `$`).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(resGet).To(Equal(`[{"nested1":{"a":{}},"a":["foo"],"nested2":{"a":"claro"},"nested3":{"a":{"baz":50}}}]`)) - }) - - It("should JSONDel", Label("json.del", "json"), func() { - cmd1 := client.JSONSet(ctx, "del1", "$", `[1]`) - Expect(cmd1.Err()).NotTo(HaveOccurred()) - Expect(cmd1.Val()).To(Equal("OK")) - - cmd2 := client.JSONDel(ctx, "del1", "$") - Expect(cmd2.Err()).NotTo(HaveOccurred()) - Expect(cmd2.Val()).To(Equal(int64(1))) - - cmd3 := client.JSONGet(ctx, "del1", "$") - Expect(cmd3.Err()).NotTo(HaveOccurred()) - Expect(cmd3.Val()).To(HaveLen(0)) - }) - - It("should JSONDel with $", Label("json.del", "json"), func() { - res, err := client.JSONSet(ctx, "del1", "$", `{"a": 1, "nested": {"a": 2, "b": 3}}`).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res).To(Equal("OK")) - - iRes, err := client.JSONDel(ctx, "del1", "$..a").Result() - Expect(err).NotTo(HaveOccurred()) - Expect(iRes).To(Equal(int64(2))) - - resGet, err := client.JSONGet(ctx, "del1", "$").Result() - Expect(err).NotTo(HaveOccurred()) - Expect(resGet).To(Equal(`[{"nested":{"b":3}}]`)) - - res, err = client.JSONSet(ctx, "del2", "$", `{"a": {"a": 2, "b": 3}, "b": ["a", "b"], "nested": {"b": [true, "a", "b"]}}`).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res).To(Equal("OK")) - - iRes, err = client.JSONDel(ctx, "del2", "$..a").Result() - Expect(err).NotTo(HaveOccurred()) - Expect(iRes).To(Equal(int64(1))) - - resGet, err = client.JSONGet(ctx, "del2", "$").Result() - Expect(err).NotTo(HaveOccurred()) - Expect(resGet).To(Equal(`[{"nested":{"b":[true,"a","b"]},"b":["a","b"]}]`)) - - doc := `[ - { - "ciao": ["non ancora"], - "nested": [ - {"ciao": [1, "a"]}, - {"ciao": [2, "a"]}, - {"ciaoc": [3, "non", "ciao"]}, - {"ciao": [4, "a"]}, - {"e": [5, "non", "ciao"]} - ] - } - ]` - res, err = client.JSONSet(ctx, "del3", "$", doc).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res).To(Equal("OK")) - - iRes, err = client.JSONDel(ctx, "del3", `$.[0]["nested"]..ciao`).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(iRes).To(Equal(int64(3))) - - resVal := `[[{"ciao":["non ancora"],"nested":[{},{},{"ciaoc":[3,"non","ciao"]},{},{"e":[5,"non","ciao"]}]}]]` - resGet, err = client.JSONGet(ctx, "del3", "$").Result() - Expect(err).NotTo(HaveOccurred()) - Expect(resGet).To(Equal(resVal)) - }) - - It("should JSONForget", Label("json.forget", "json"), func() { - cmd1 := client.JSONSet(ctx, "forget3", "$", `{"a": [1,2,3], "b": {"a": [1,2,3], "b": "annie"}}`) - Expect(cmd1.Err()).NotTo(HaveOccurred()) - Expect(cmd1.Val()).To(Equal("OK")) - - cmd2 := client.JSONForget(ctx, "forget3", "$..a") - Expect(cmd2.Err()).NotTo(HaveOccurred()) - Expect(cmd2.Val()).To(Equal(int64(2))) - - cmd3 := client.JSONGet(ctx, "forget3", "$") - Expect(cmd3.Err()).NotTo(HaveOccurred()) - Expect(cmd3.Val()).To(Equal(`[{"b":{"b":"annie"}}]`)) - }) - - It("should JSONForget with $", Label("json.forget", "json"), func() { - res, err := client.JSONSet(ctx, "doc1", "$", `{"a": 1, "nested": {"a": 2, "b": 3}}`).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res).To(Equal("OK")) - - iRes, err := client.JSONForget(ctx, "doc1", "$..a").Result() - Expect(err).NotTo(HaveOccurred()) - Expect(iRes).To(Equal(int64(2))) - - resGet, err := client.JSONGet(ctx, "doc1", "$").Result() - Expect(err).NotTo(HaveOccurred()) - Expect(resGet).To(Equal(`[{"nested":{"b":3}}]`)) - - res, err = client.JSONSet(ctx, "doc2", "$", `{"a": {"a": 2, "b": 3}, "b": ["a", "b"], "nested": {"b": [true, "a", "b"]}}`).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res).To(Equal("OK")) - - iRes, err = client.JSONForget(ctx, "doc2", "$..a").Result() - Expect(err).NotTo(HaveOccurred()) - Expect(iRes).To(Equal(int64(1))) - - resGet, err = client.JSONGet(ctx, "doc2", "$").Result() - Expect(err).NotTo(HaveOccurred()) - Expect(resGet).To(Equal(`[{"nested":{"b":[true,"a","b"]},"b":["a","b"]}]`)) - - doc := `[ - { - "ciao": ["non ancora"], - "nested": [ - {"ciao": [1, "a"]}, - {"ciao": [2, "a"]}, - {"ciaoc": [3, "non", "ciao"]}, - {"ciao": [4, "a"]}, - {"e": [5, "non", "ciao"]} - ] - } - ]` - res, err = client.JSONSet(ctx, "doc3", "$", doc).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res).To(Equal("OK")) - - iRes, err = client.JSONForget(ctx, "doc3", `$.[0]["nested"]..ciao`).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(iRes).To(Equal(int64(3))) - - resVal := `[[{"ciao":["non ancora"],"nested":[{},{},{"ciaoc":[3,"non","ciao"]},{},{"e":[5,"non","ciao"]}]}]]` - resGet, err = client.JSONGet(ctx, "doc3", "$").Result() - Expect(err).NotTo(HaveOccurred()) - Expect(resGet).To(Equal(resVal)) - }) - - It("should JSONNumIncrBy", Label("json.numincrby", "json"), func() { - cmd1 := client.JSONSet(ctx, "incr3", "$", `{"a": [1, 2], "b": {"a": [0, -1]}}`) - Expect(cmd1.Err()).NotTo(HaveOccurred()) - Expect(cmd1.Val()).To(Equal("OK")) - - cmd2 := client.JSONNumIncrBy(ctx, "incr3", "$..a[1]", float64(1)) - Expect(cmd2.Err()).NotTo(HaveOccurred()) - Expect(cmd2.Val()).To(Equal(`[3,0]`)) - }) - - It("should JSONNumIncrBy with $", Label("json.numincrby", "json"), func() { - res, err := client.JSONSet(ctx, "doc1", "$", `{"a": "b", "b": [{"a": 2}, {"a": 5.0}, {"a": "c"}]}`).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res).To(Equal("OK")) - - res, err = client.JSONNumIncrBy(ctx, "doc1", "$.b[1].a", 2).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res).To(Equal(`[7]`)) - - res, err = client.JSONNumIncrBy(ctx, "doc1", "$.b[1].a", 3.5).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res).To(Equal(`[10.5]`)) - - res, err = client.JSONSet(ctx, "doc2", "$", `{"a": "b", "b": [{"a": 2}, {"a": 5.0}, {"a": "c"}]}`).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res).To(Equal("OK")) - - res, err = client.JSONNumIncrBy(ctx, "doc2", "$.b[0].a", 3).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res).To(Equal(`[5]`)) - }) - - It("should JSONObjKeys", Label("json.objkeys", "json"), func() { - cmd1 := client.JSONSet(ctx, "objkeys1", "$", `{"a": [1, 2], "b": {"a": [0, -1]}}`) - Expect(cmd1.Err()).NotTo(HaveOccurred()) - Expect(cmd1.Val()).To(Equal("OK")) - - cmd2 := client.JSONObjKeys(ctx, "objkeys1", "$..*") - Expect(cmd2.Err()).NotTo(HaveOccurred()) - Expect(cmd2.Val()).To(HaveLen(7)) - Expect(cmd2.Val()).To(Equal([]interface{}{nil, []interface{}{"a"}, nil, nil, nil, nil, nil})) - }) - - It("should JSONObjKeys with $", Label("json.objkeys", "json"), func() { - doc := `{ - "nested1": {"a": {"foo": 10, "bar": 20}}, - "a": ["foo"], - "nested2": {"a": {"baz": 50}} - }` - cmd1, err := client.JSONSet(ctx, "objkeys1", "$", doc).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(cmd1).To(Equal("OK")) - - cmd2, err := client.JSONObjKeys(ctx, "objkeys1", "$.nested1.a").Result() - Expect(err).NotTo(HaveOccurred()) - Expect(cmd2).To(Equal([]interface{}{[]interface{}{"foo", "bar"}})) - - cmd2, err = client.JSONObjKeys(ctx, "objkeys1", ".*.a").Result() - Expect(err).NotTo(HaveOccurred()) - Expect(cmd2).To(Equal([]interface{}{"foo", "bar"})) - - cmd2, err = client.JSONObjKeys(ctx, "objkeys1", ".nested2.a").Result() - Expect(err).NotTo(HaveOccurred()) - Expect(cmd2).To(Equal([]interface{}{"baz"})) - - _, err = client.JSONObjKeys(ctx, "non_existing_doc", "..a").Result() - Expect(err).To(HaveOccurred()) - }) - - It("should JSONObjLen", Label("json.objlen", "json"), func() { - cmd1 := client.JSONSet(ctx, "objlen2", "$", `{"a": [1, 2], "b": {"a": [0, -1]}}`) - Expect(cmd1.Err()).NotTo(HaveOccurred()) - Expect(cmd1.Val()).To(Equal("OK")) - - cmd2 := client.JSONObjLen(ctx, "objlen2", "$..*") - Expect(cmd2.Err()).NotTo(HaveOccurred()) - Expect(cmd2.Val()).To(HaveLen(7)) - Expect(cmd2.Val()[0]).To(BeNil()) - Expect(*cmd2.Val()[1]).To(Equal(int64(1))) - }) - - It("should JSONStrLen", Label("json.strlen", "json"), func() { - cmd1 := client.JSONSet(ctx, "strlen2", "$", `{"a": "alice", "b": "bob", "c": {"a": "alice", "b": "bob"}}`) - Expect(cmd1.Err()).NotTo(HaveOccurred()) - Expect(cmd1.Val()).To(Equal("OK")) - - cmd2 := client.JSONStrLen(ctx, "strlen2", "$..*") - Expect(cmd2.Err()).NotTo(HaveOccurred()) - Expect(cmd2.Val()).To(HaveLen(5)) - var tmp int64 = 20 - Expect(cmd2.Val()[0]).To(BeAssignableToTypeOf(&tmp)) - Expect(*cmd2.Val()[0]).To(Equal(int64(5))) - Expect(*cmd2.Val()[1]).To(Equal(int64(3))) - Expect(cmd2.Val()[2]).To(BeNil()) - Expect(*cmd2.Val()[3]).To(Equal(int64(5))) - Expect(*cmd2.Val()[4]).To(Equal(int64(3))) - }) - - It("should JSONStrAppend", Label("json.strappend", "json"), func() { - cmd1, err := client.JSONSet(ctx, "strapp1", "$", `"foo"`).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(cmd1).To(Equal("OK")) - cmd2, err := client.JSONStrAppend(ctx, "strapp1", "$", `"bar"`).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(*cmd2[0]).To(Equal(int64(6))) - cmd3, err := client.JSONGet(ctx, "strapp1", "$").Result() - Expect(err).NotTo(HaveOccurred()) - Expect(cmd3).To(Equal(`["foobar"]`)) - }) - - It("should JSONStrAppend and JSONStrLen with $", Label("json.strappend", "json.strlen", "json"), func() { - res, err := client.JSONSet(ctx, "doc1", "$", `{"a": "foo", "nested1": {"a": "hello"}, "nested2": {"a": 31}}`).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res).To(Equal("OK")) - - intArrayResult, err := client.JSONStrAppend(ctx, "doc1", "$.nested1.a", `"baz"`).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(*intArrayResult[0]).To(Equal(int64(8))) - - res, err = client.JSONSet(ctx, "doc2", "$", `{"a": "foo", "nested1": {"a": "hello"}, "nested2": {"a": 31}}`).Result() - Expect(err).NotTo(HaveOccurred()) - Expect(res).To(Equal("OK")) - - intResult, err := client.JSONStrLen(ctx, "doc2", "$.nested1.a").Result() - Expect(err).NotTo(HaveOccurred()) - Expect(*intResult[0]).To(Equal(int64(5))) - }) - - It("should JSONToggle", Label("json.toggle", "json"), func() { - cmd1 := client.JSONSet(ctx, "toggle1", "$", `[true]`) - Expect(cmd1.Err()).NotTo(HaveOccurred()) - Expect(cmd1.Val()).To(Equal("OK")) - - cmd2 := client.JSONToggle(ctx, "toggle1", "$[0]") - Expect(cmd2.Err()).NotTo(HaveOccurred()) - Expect(cmd2.Val()).To(HaveLen(1)) - Expect(*cmd2.Val()[0]).To(Equal(int64(0))) - }) - - It("should JSONType", Label("json.type", "json"), func() { - cmd1 := client.JSONSet(ctx, "type1", "$", `[true]`) - Expect(cmd1.Err()).NotTo(HaveOccurred()) - Expect(cmd1.Val()).To(Equal("OK")) - - cmd2 := client.JSONType(ctx, "type1", "$[0]") - Expect(cmd2.Err()).NotTo(HaveOccurred()) - Expect(cmd2.Val()).To(HaveLen(1)) - // RESP2 v RESP3 - Expect(cmd2.Val()[0]).To(Or(Equal([]interface{}{"boolean"}), Equal("boolean"))) - }) + Context("when testing with RESP2 and RESP3", func() { + protocols := []int{2, 3} + + for _, protocol := range protocols { + When("using protocol version", func() { + BeforeEach(func() { + client = setupRedisClient(protocol) + }) + + It("should perform complex JSON and RediSearch operations", func() { + jsonDoc := map[string]interface{}{ + "person": map[string]interface{}{ + "name": "Alice", + "age": 30, + "status": true, + "address": map[string]interface{}{ + "city": "Wonderland", + "postcode": "12345", + }, + "contacts": []map[string]interface{}{ + {"type": "email", "value": "alice@example.com"}, + {"type": "phone", "value": "+123456789"}, + {"type": "fax", "value": "+987654321"}, + }, + "friends": []map[string]interface{}{ + {"name": "Bob", "age": 35, "status": true}, + {"name": "Charlie", "age": 28, "status": false}, + }, + }, + "settings": map[string]interface{}{ + "notifications": map[string]interface{}{ + "email": true, + "sms": false, + "alerts": []string{"low battery", "door open"}, + }, + "theme": "dark", + }, + } + + setCmd := client.JSONSet(ctx, "person:1", ".", jsonDoc) + Expect(setCmd.Err()).NotTo(HaveOccurred(), "JSON.SET failed") + + getCmdRaw := client.JSONGet(ctx, "person:1", ".") + rawJSON, err := getCmdRaw.Result() + Expect(err).NotTo(HaveOccurred(), "JSON.GET (raw) failed") + GinkgoWriter.Printf("Raw JSON: %s\n", rawJSON) + + getCmdExpanded := client.JSONGet(ctx, "person:1", ".") + expandedJSON, err := getCmdExpanded.Expanded() + Expect(err).NotTo(HaveOccurred(), "JSON.GET (expanded) failed") + GinkgoWriter.Printf("Expanded JSON: %+v\n", expandedJSON) + + Expect(rawJSON).To(MatchJSON(jsonMustMarshal(expandedJSON))) + + arrAppendCmd := client.JSONArrAppend(ctx, "person:1", "$.person.contacts", `{"type": "social", "value": "@alice_wonder"}`) + Expect(arrAppendCmd.Err()).NotTo(HaveOccurred(), "JSON.ARRAPPEND failed") + arrLenCmd := client.JSONArrLen(ctx, "person:1", "$.person.contacts") + arrLen, err := arrLenCmd.Result() + Expect(err).NotTo(HaveOccurred(), "JSON.ARRLEN failed") + Expect(arrLen).To(Equal([]int64{4}), "Array length mismatch after append") + + arrInsertCmd := client.JSONArrInsert(ctx, "person:1", "$.person.friends", 1, `{"name": "Diana", "age": 25, "status": true}`) + Expect(arrInsertCmd.Err()).NotTo(HaveOccurred(), "JSON.ARRINSERT failed") + + start := 0 + stop := 1 + arrTrimCmd := client.JSONArrTrimWithArgs(ctx, "person:1", "$.person.friends", &redis.JSONArrTrimArgs{Start: start, Stop: &stop}) + Expect(arrTrimCmd.Err()).NotTo(HaveOccurred(), "JSON.ARRTRIM failed") + + mergeData := map[string]interface{}{ + "status": false, + "nickname": "WonderAlice", + "lastLogin": time.Now().Format(time.RFC3339), + } + mergeCmd := client.JSONMerge(ctx, "person:1", "$.person", jsonMustMarshal(mergeData)) + Expect(mergeCmd.Err()).NotTo(HaveOccurred(), "JSON.MERGE failed") + + typeCmd := client.JSONType(ctx, "person:1", "$.person.nickname") + nicknameType, err := typeCmd.Result() + Expect(err).NotTo(HaveOccurred(), "JSON.TYPE failed") + Expect(nicknameType[0]).To(Equal([]interface{}{"string"}), "JSON.TYPE mismatch for nickname") + + createIndexCmd := client.Do(ctx, "FT.CREATE", "person_idx", "ON", "JSON", + "PREFIX", "1", "person:", "SCHEMA", + "$.person.name", "AS", "name", "TEXT", + "$.person.age", "AS", "age", "NUMERIC", + "$.person.address.city", "AS", "city", "TEXT", + "$.person.contacts[*].value", "AS", "contact_value", "TEXT", + ) + Expect(createIndexCmd.Err()).NotTo(HaveOccurred(), "FT.CREATE failed") + + searchCmd := client.FTSearchWithArgs(ctx, "person_idx", "@contact_value:(alice\\@example\\.com alice_wonder)", &redis.FTSearchOptions{Return: []redis.FTSearchReturn{{FieldName: "$.person.name"}, {FieldName: "$.person.age"}, {FieldName: "$.person.address.city"}}}) + searchResult, err := searchCmd.Result() + Expect(err).NotTo(HaveOccurred(), "FT.SEARCH failed") + GinkgoWriter.Printf("Advanced Search result: %+v\n", searchResult) + + incrCmd := client.JSONNumIncrBy(ctx, "person:1", "$.person.age", 5) + incrResult, err := incrCmd.Result() + Expect(err).NotTo(HaveOccurred(), "JSON.NUMINCRBY failed") + Expect(incrResult).To(Equal("[35]"), "Age increment mismatch") + + delCmd := client.JSONDel(ctx, "person:1", "$.settings.notifications.email") + Expect(delCmd.Err()).NotTo(HaveOccurred(), "JSON.DEL failed") + + typeCmd = client.JSONType(ctx, "person:1", "$.settings.notifications.email") + typeResult, err := typeCmd.Result() + Expect(err).ToNot(HaveOccurred()) + Expect(typeResult[0]).To(BeEmpty(), "Expected JSON.TYPE to be empty for deleted field") + }) + }) + } }) }) + +// Helper function to marshal data into JSON for comparisons +func jsonMustMarshal(v interface{}) string { + bytes, err := json.Marshal(v) + Expect(err).NotTo(HaveOccurred()) + return string(bytes) +} diff --git a/search_test.go b/search_test.go index bbbc38ac1..48b9aa39b 100644 --- a/search_test.go +++ b/search_test.go @@ -1446,16 +1446,18 @@ var _ = Describe("RediSearch commands Resp 3", Label("search"), func() { options := &redis.FTAggregateOptions{Apply: []redis.FTAggregateApply{{Field: "@CreatedDateTimeUTC * 10", As: "CreatedDateTimeUTC"}}} res, err := client.FTAggregateWithArgs(ctx, "idx1", "*", options).RawResult() - rawVal := client.FTAggregateWithArgs(ctx, "idx1", "*", options).RawVal() - - Expect(err).NotTo(HaveOccurred()) - Expect(rawVal).To(BeEquivalentTo(res)) results := res.(map[interface{}]interface{})["results"].([]interface{}) Expect(results[0].(map[interface{}]interface{})["extra_attributes"].(map[interface{}]interface{})["CreatedDateTimeUTC"]). To(Or(BeEquivalentTo("6373878785249699840"), BeEquivalentTo("6373878758592700416"))) Expect(results[1].(map[interface{}]interface{})["extra_attributes"].(map[interface{}]interface{})["CreatedDateTimeUTC"]). To(Or(BeEquivalentTo("6373878785249699840"), BeEquivalentTo("6373878758592700416"))) + rawVal := client.FTAggregateWithArgs(ctx, "idx1", "*", options).RawVal() + rawValResults := rawVal.(map[interface{}]interface{})["results"].([]interface{}) + Expect(err).NotTo(HaveOccurred()) + Expect(rawValResults[0]).To(Or(BeEquivalentTo(results[0]), BeEquivalentTo(results[1]))) + Expect(rawValResults[1]).To(Or(BeEquivalentTo(results[0]), BeEquivalentTo(results[1]))) + // Test with UnstableResp3 false Expect(func() { options = &redis.FTAggregateOptions{Apply: []redis.FTAggregateApply{{Field: "@CreatedDateTimeUTC * 10", As: "CreatedDateTimeUTC"}}}