mirror of
https://github.com/redis/go-redis.git
synced 2025-09-08 19:52:07 +03:00
Ensure that JSON.GET returns Nil response
Updated JSONCmd.readReply to return redis.Nil if no results Added a doc line for Val() and Expanded() methods of JSONCmd Added a test case for non existent keys in json_test.go
This commit is contained in:
12
json.go
12
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)
|
||||
@@ -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
|
||||
}
|
||||
|
@@ -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() {
|
||||
|
Reference in New Issue
Block a user