@@ -142,6 +142,10 @@ var _ = Describe("JSON Commands", Label("json"), func() {
142
142
resArr , err := client .JSONArrIndex (ctx , "doc1" , "$.store.book[?(@.price<10)].size" , 20 ).Result ()
143
143
Expect (err ).NotTo (HaveOccurred ())
144
144
Expect (resArr ).To (Equal ([]int64 {1 , 2 }))
145
+
146
+ _ , err = client .JSONGet (ctx , "this-key-does-not-exist" , "$" ).Result ()
147
+ Expect (err ).To (HaveOccurred ())
148
+ Expect (err ).To (BeIdenticalTo (redis .Nil ))
145
149
})
146
150
147
151
It ("should JSONArrInsert" , Label ("json.arrinsert" , "json" ), func () {
@@ -402,8 +406,8 @@ var _ = Describe("JSON Commands", Label("json"), func() {
402
406
Expect (cmd2 .Val ()).To (Equal (int64 (1 )))
403
407
404
408
cmd3 := client .JSONGet (ctx , "del1" , "$" )
405
- Expect (cmd3 .Err ()).NotTo ( HaveOccurred ( ))
406
- Expect (cmd3 .Val ()).To (HaveLen ( 0 ))
409
+ Expect (cmd3 .Err ()).To ( Equal ( redis . Nil ))
410
+ Expect (cmd3 .Val ()).To (Equal ( "" ))
407
411
})
408
412
409
413
It ("should JSONDel with $" , Label ("json.del" , "json" ), func () {
@@ -673,6 +677,58 @@ var _ = Describe("JSON Commands", Label("json"), func() {
673
677
Expect (cmd2 .Val ()[0 ]).To (Or (Equal ([]interface {}{"boolean" }), Equal ("boolean" )))
674
678
})
675
679
})
680
+
681
+ Describe ("JSON Nil Handling" , func () {
682
+ It ("should return redis.Nil for non-existent key" , func () {
683
+ _ , err := client .JSONGet (ctx , "non-existent-key" , "$" ).Result ()
684
+ Expect (err ).To (Equal (redis .Nil ))
685
+ })
686
+
687
+ It ("should return empty array for non-existent path in existing key" , func () {
688
+ err := client .JSONSet (ctx , "test-key" , "$" , `{"a": 1, "b": "hello"}` ).Err ()
689
+ Expect (err ).NotTo (HaveOccurred ())
690
+
691
+ // Non-existent path should return empty array, not error
692
+ val , err := client .JSONGet (ctx , "test-key" , "$.nonexistent" ).Result ()
693
+ Expect (err ).NotTo (HaveOccurred ())
694
+ Expect (val ).To (Equal ("[]" ))
695
+ })
696
+
697
+ It ("should distinguish empty array from non-existent path" , func () {
698
+ err := client .JSONSet (ctx , "test-key" , "$" , `{"arr": [], "obj": {}}` ).Err ()
699
+ Expect (err ).NotTo (HaveOccurred ())
700
+
701
+ // Empty array should return the array
702
+ val , err := client .JSONGet (ctx , "test-key" , "$.arr" ).Result ()
703
+ Expect (err ).NotTo (HaveOccurred ())
704
+ Expect (val ).To (Equal ("[[]]" ))
705
+
706
+ // Non-existent field should return empty array
707
+ val , err = client .JSONGet (ctx , "test-key" , "$.missing" ).Result ()
708
+ Expect (err ).NotTo (HaveOccurred ())
709
+ Expect (val ).To (Equal ("[]" ))
710
+ })
711
+
712
+ It ("should handle multiple paths with mixed results" , func () {
713
+ err := client .JSONSet (ctx , "test-key" , "$" , `{"a": 1, "b": 2}` ).Err ()
714
+ Expect (err ).NotTo (HaveOccurred ())
715
+
716
+ // Path that exists
717
+ val , err := client .JSONGet (ctx , "test-key" , "$.a" ).Result ()
718
+ Expect (err ).NotTo (HaveOccurred ())
719
+ Expect (val ).To (Equal ("[1]" ))
720
+
721
+ // Path that doesn't exist should return empty array
722
+ val , err = client .JSONGet (ctx , "test-key" , "$.c" ).Result ()
723
+ Expect (err ).NotTo (HaveOccurred ())
724
+ Expect (val ).To (Equal ("[]" ))
725
+ })
726
+
727
+ AfterEach (func () {
728
+ // Clean up test keys
729
+ client .Del (ctx , "test-key" , "non-existent-key" )
730
+ })
731
+ })
676
732
}
677
733
})
678
734
0 commit comments