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: