Skip to content

Ensure that JSON.GET returns Nil response #2987

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions json.go
Original file line number Diff line number Diff line change
@@ -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)
@@ -112,10 +114,10 @@ func (cmd JSONCmd) Expanded() (interface{}, error) {
}

func (cmd *JSONCmd) readReply(rd *proto.Reader) error {
// nil response from JSON.(M)GET (cmd.baseCmd.err will be "redis: nil")
if cmd.baseCmd.Err() == Nil {

if cmd.baseCmd.Err() != nil {
cmd.val = ""
return Nil
return cmd.baseCmd.Err()
}

if readType, err := rd.PeekReplyType(); err != nil {
@@ -126,6 +128,9 @@ func (cmd *JSONCmd) readReply(rd *proto.Reader) error {
if err != nil {
return err
}
if size == 0 {
return Nil
}

expanded := make([]interface{}, size)

@@ -141,6 +146,7 @@ func (cmd *JSONCmd) readReply(rd *proto.Reader) error {
return err
} else if str == "" || err == Nil {
cmd.val = ""
return Nil
} else {
cmd.val = str
}
5 changes: 5 additions & 0 deletions json_test.go
Original file line number Diff line number Diff line change
@@ -123,9 +123,14 @@ var _ = Describe("JSON Commands", Label("json"), func() {
Expect(err).NotTo(HaveOccurred())
Expect(resGet).To(Equal("[[10,20,30,40],[5,10,20,30]]"))

_, err = client.JSONGet(ctx, "this-key-does-not-exist", "$").Result()
Expect(err).To(HaveOccurred())
Expect(err).To(BeIdenticalTo(redis.Nil))

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() {