redis / go-redis

Redis Go client
https://redis.uptrace.dev
BSD 2-Clause "Simplified" License
19.81k stars 2.34k forks source link

JSONArrAppend not working as expected in latest release . #2854

Closed f1forhelp closed 7 months ago

f1forhelp commented 8 months ago

Hello! I recently started using go-redis version v9.3.1, mainly because it offers support for RedisJSON. However, I encountered an issue while attempting to add an element to an array within a JSON object. The error message I received is "expected value at line 1 column 1."

Here's the JSON object I'm working with:

{
    "moves": [
        "m1",
        "m2"
    ],
    "match_status": 2
}

To insert a value into the "moves" array, I'm utilizing the following function: db.RedisClientWebSocket.JSONArrAppend(context.Background(), fmt.Sprint("match:", redisMatch.Id.Hex()), "$.moves","m3")

Though I can insert struct after marshaling it. But I can't do the same with string.

My development environment is on go version go1.21.4 for darwin/arm64.

ofekshenawa commented 8 months ago

Hey @f1forhelp , Redis can't parse strings literal - https://github.com/RedisJSON/RedisJSON/issues/975. For a quick solution try to enclosing the quoted string with backticks, like this: `"m3"`:

db.RedisClientWebSocket.JSONArrAppend(context.Background(), fmt.Sprint("match:", redisMatch.Id.Hex()), "$.moves", `"m3"`)

Looking ahead, we plan to implement support for incorporating literal strings in go-redis JSON commands.

f1forhelp commented 7 months ago

Yes man @ofekshenawa its working really tnx .

f1forhelp commented 7 months ago

Hii @ofekshenawa but i think its not possible to add variable data to backTicks(`).

f1forhelp commented 7 months ago

Hii so basically I marshaled the string I got the desired O/P

if v, err := json.Marshal(string(msg)); err != nil {
    log.Println(err)
} else {
    if res := db.RedisClientWebSocket.JSONArrAppend(context.Background(), fmt.Sprint("match:", redisMatch.Id.Hex()), 
         "$.moves", v); res.Err() != nil {
          log.Println("Array Append Operation:", res.Err())
      }
}