79653083

Date: 2025-06-04 15:38:17
Score: 0.5
Natty:
Report link

In case someone is still looking for a solution, this appears to be the most elegant way:

type Timestamp time.Time

func (t *Timestamp) RedisScan(x any) error {
    ...
}

// This allows to correct encode the value!
func (t Timestamp) RedisArg() any {
    return time.Time(t).Format(time.RFC3339)
}

type Data struct {
    Timestamp  Timestamp `redis:"timestamp"`
}

func (r *RedisRepo) Write(data Data, key String) error {
    conn := r.pool.Get()
    defer conn.Close()
    conn.Send("HSET", redis.Args{key}.AddFlat(data)...)     
}

func (r *RedisRepo) Read(key string) (*Data, error) {
    var data Data
    conn := r.pool.Get()
    defer conn.Close()
    v, err := redis.Values(conn.Do("HGETALL", key))
    return redis.ScanStruct(v, &data)
}

The RedisArg (reference) is used when encoding here:

https://github.com/gomodule/redigo/blob/4c535aa56d60a1dddd457a8e63caa463bcb5a70b/redis/scan.go#L705-L706

Reasons:
  • Probably link only (1):
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: faersons