Search code examples
goredisredigo

Error unmarshaling when getting array of struct from redis


I am using redigo to save some structs in redis. The thing is that for the same key I need to append new structs, but when I am trying to recover them I cannot unmarshal to an array.

Ie: (ignoring the errors intentionally)

type ADTO struct {
    Value string
}

func main() {
    pool := redis.Pool{
        Dial: func() (conn redis.Conn, e error) {
            return redis.Dial("tcp", "localhost:6379")
        },
        MaxIdle:   80,
        MaxActive: 12000,
    }

    conn := pool.Get()
    defer conn.Close()
    key := "some-key"
    defer conn.Do("DEL", key)

    a := ADTO{Value: "a"}
    bytes, _ := json.Marshal(a)
    conn.Do("APPEND", key, bytes)

    b := ADTO{Value: "b"}
    bytes, _ = json.Marshal(b)
    conn.Do("APPEND", key, bytes)

    c := ADTO{Value: "c"}
    bytes, _ = json.Marshal(c)
    conn.Do("APPEND", key, bytes)

    bytes, _ = redis.Bytes(conn.Do("GET", key))

    adtos := make([]ADTO, 0)

    // the following does not work
    if err := json.Unmarshal(bytes, &adtos); err != nil {
        return
    }
}

If I only append a single struct and retrieving it, then it is working fine

I have tried with redis.ByteSlices with no luck


Solution

  • APPEND will only append to a string, it will not make a JSON array. After first insert, you'll have

    {"Value":"a"}
    

    Then after the second one, you'll have

    {"Value":"a"}{"Value":"b"}
    

    That's not a JSON array.

    You can try using json.Decoder, and do something like:

    b, _ = redis.Bytes(conn.Do("GET", key))
    dec := json.NewDecoder(bytes.NewReader(b))
    items := []ADTO{}
    var x ADTO
    for dec.Decode(&x) == nil {
      items = append(items, x)
    }