Skip to content
Closed
Show file tree
Hide file tree
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
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -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)

Expand All @@ -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
}
Expand Down
5 changes: 5 additions & 0 deletions json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down