diff --git a/json.go b/json.go index b3cadf4b7..234ee8620 100644 --- a/json.go +++ b/json.go @@ -82,6 +82,7 @@ func (cmd *JSONCmd) SetVal(val string) { cmd.val = val } +// Val returns the result of the JSON.GET command as a string. func (cmd *JSONCmd) Val() string { if len(cmd.val) == 0 && cmd.expanded != nil { val, err := json.Marshal(cmd.expanded) @@ -100,6 +101,7 @@ func (cmd *JSONCmd) Result() (string, error) { return cmd.Val(), cmd.Err() } +// Expanded returns the result of the JSON.GET command as unmarshalled JSON. func (cmd *JSONCmd) Expanded() (interface{}, error) { if len(cmd.val) != 0 && cmd.expanded == nil { err := json.Unmarshal([]byte(cmd.val), &cmd.expanded) @@ -118,6 +120,11 @@ func (cmd *JSONCmd) readReply(rd *proto.Reader) error { return Nil } + // Handle other base command errors + if cmd.baseCmd.Err() != nil { + return cmd.baseCmd.Err() + } + if readType, err := rd.PeekReplyType(); err != nil { return err } else if readType == proto.RespArray { @@ -127,6 +134,12 @@ func (cmd *JSONCmd) readReply(rd *proto.Reader) error { return err } + // Empty array could indicate no results found for JSON path + if size == 0 { + cmd.val = "" + return Nil + } + expanded := make([]interface{}, size) for i := 0; i < size; i++ { @@ -141,6 +154,7 @@ func (cmd *JSONCmd) readReply(rd *proto.Reader) error { return err } else if str == "" || err == Nil { cmd.val = "" + return Nil } else { cmd.val = str } diff --git a/json_test.go b/json_test.go index 9139be3ac..4e696c38d 100644 --- a/json_test.go +++ b/json_test.go @@ -263,6 +263,10 @@ var _ = Describe("JSON Commands", Label("json"), func() { Expect(err).NotTo(HaveOccurred()) Expect(res).To(Equal("OK")) + resArr, err := client.JSONArrIndex(ctx, "doc1", "$.store.book[?(@.price<10)].size", 20).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(resArr).To(Equal([]int64{1, 2})) + res, err = client.JSONGetWithArgs(ctx, "get3", &redis.JSONGetArgs{Indent: "-"}).Result() Expect(err).NotTo(HaveOccurred()) Expect(res).To(Equal(`{-"a":1,-"b":2}`)) @@ -673,6 +677,54 @@ var _ = Describe("JSON Commands", Label("json"), func() { Expect(cmd2.Val()[0]).To(Or(Equal([]interface{}{"boolean"}), Equal("boolean"))) }) }) + + Describe("JSON Nil Handling", func() { + It("should return redis.Nil for non-existent key", func() { + _, err := client.JSONGet(ctx, "non-existent-key", "$").Result() + Expect(err).To(Equal(redis.Nil)) + }) + + It("should return redis.Nil for non-existent path in existing key", func() { + err := client.JSONSet(ctx, "test-key", "$", `{"a": 1, "b": "hello"}`).Err() + Expect(err).NotTo(HaveOccurred()) + + _, err = client.JSONGet(ctx, "test-key", "$.nonexistent").Result() + Expect(err).To(Equal(redis.Nil)) + }) + + It("should distinguish empty array from nil", func() { + err := client.JSONSet(ctx, "test-key", "$", `{"arr": [], "obj": {}}`).Err() + Expect(err).NotTo(HaveOccurred()) + + // Empty array should return the array, not nil + val, err := client.JSONGet(ctx, "test-key", "$.arr").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(val).To(Equal("[[]]")) + + // Non-existent field should return nil + _, err = client.JSONGet(ctx, "test-key", "$.missing").Result() + Expect(err).To(Equal(redis.Nil)) + }) + + It("should handle multiple paths with mixed results", func() { + err := client.JSONSet(ctx, "test-key", "$", `{"a": 1, "b": 2}`).Err() + Expect(err).NotTo(HaveOccurred()) + + // Path that exists + val, err := client.JSONGet(ctx, "test-key", "$.a").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(val).To(Equal("[1]")) + + // Path that doesn't exist + _, err = client.JSONGet(ctx, "test-key", "$.c").Result() + Expect(err).To(Equal(redis.Nil)) + }) + + AfterEach(func() { + // Clean up test keys + client.Del(ctx, "test-key", "non-existent-key") + }) + }) } })