Skip to content

Commit 1d6c453

Browse files
nic-gibsonofekshenawa
authored andcommitted
fix conflicts
1 parent b7838dc commit 1d6c453

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

json.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ func (cmd *JSONCmd) readReply(rd *proto.Reader) error {
118118
return Nil
119119
}
120120

121+
// Handle other base command errors
122+
if cmd.baseCmd.Err() != nil {
123+
return cmd.baseCmd.Err()
124+
}
125+
121126
if readType, err := rd.PeekReplyType(); err != nil {
122127
return err
123128
} else if readType == proto.RespArray {
@@ -127,6 +132,12 @@ func (cmd *JSONCmd) readReply(rd *proto.Reader) error {
127132
return err
128133
}
129134

135+
// Empty array could indicate no results found for JSON path
136+
if size == 0 {
137+
cmd.val = ""
138+
return Nil
139+
}
140+
130141
expanded := make([]interface{}, size)
131142

132143
for i := 0; i < size; i++ {

json_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,10 @@ var _ = Describe("JSON Commands", Label("json"), func() {
263263
Expect(err).NotTo(HaveOccurred())
264264
Expect(res).To(Equal("OK"))
265265

266+
resArr, err := client.JSONArrIndex(ctx, "doc1", "$.store.book[?(@.price<10)].size", 20).Result()
267+
Expect(err).NotTo(HaveOccurred())
268+
Expect(resArr).To(Equal([]int64{1, 2}))
269+
266270
res, err = client.JSONGetWithArgs(ctx, "get3", &redis.JSONGetArgs{Indent: "-"}).Result()
267271
Expect(err).NotTo(HaveOccurred())
268272
Expect(res).To(Equal(`{-"a":1,-"b":2}`))
@@ -673,6 +677,54 @@ var _ = Describe("JSON Commands", Label("json"), func() {
673677
Expect(cmd2.Val()[0]).To(Or(Equal([]interface{}{"boolean"}), Equal("boolean")))
674678
})
675679
})
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 redis.Nil 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+
_, err = client.JSONGet(ctx, "test-key", "$.nonexistent").Result()
692+
Expect(err).To(Equal(redis.Nil))
693+
})
694+
695+
It("should distinguish empty array from nil", func() {
696+
err := client.JSONSet(ctx, "test-key", "$", `{"arr": [], "obj": {}}`).Err()
697+
Expect(err).NotTo(HaveOccurred())
698+
699+
// Empty array should return the array, not nil
700+
val, err := client.JSONGet(ctx, "test-key", "$.arr").Result()
701+
Expect(err).NotTo(HaveOccurred())
702+
Expect(val).To(Equal("[[]]"))
703+
704+
// Non-existent field should return nil
705+
_, err = client.JSONGet(ctx, "test-key", "$.missing").Result()
706+
Expect(err).To(Equal(redis.Nil))
707+
})
708+
709+
It("should handle multiple paths with mixed results", func() {
710+
err := client.JSONSet(ctx, "test-key", "$", `{"a": 1, "b": 2}`).Err()
711+
Expect(err).NotTo(HaveOccurred())
712+
713+
// Path that exists
714+
val, err := client.JSONGet(ctx, "test-key", "$.a").Result()
715+
Expect(err).NotTo(HaveOccurred())
716+
Expect(val).To(Equal("[1]"))
717+
718+
// Path that doesn't exist
719+
_, err = client.JSONGet(ctx, "test-key", "$.c").Result()
720+
Expect(err).To(Equal(redis.Nil))
721+
})
722+
723+
AfterEach(func() {
724+
// Clean up test keys
725+
client.Del(ctx, "test-key", "non-existent-key")
726+
})
727+
})
676728
}
677729
})
678730

0 commit comments

Comments
 (0)