You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have been trying to figure out for some time if this a missing feature or maybe is a missunderstanding, is it possible to call HSet/HMSet with a map[string]float32 or any other map of numbers?, here is an example:
package main
import (
"context""fmt""github.com/redis/go-redis/v9""github.com/rs/zerolog/log"
)
typeUserModelstruct {
Namestring`redis:"name"`Ageuint64`redis:"age"`
}
funcmain() {
ctx:=context.Background()
client:=redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%d", "localhost", 6379),
Password: "",
DB: 0,
})
user:=UserModel{
Name: "John Doe",
Age: 30,
}
err:=client.HMSet(ctx, "user:1", user).Err()
iferr!=nil {
log.Error().Err(err).Msg("Unable to set user")
}
scores:=make(map[string]float32)
scores["2024-12-26"] =21.5scores["2024-12-27"] =25err=client.HSet(ctx, "user:1:scores", scores).Err()
iferr!=nil {
log.Fatal().Err(err).Msg("Unable to set user") // Fails due to "redis: can't marshal map[string]float32 (implement encoding.BinaryMarshaler)"
}
}
func appendArg(dst []interface{}, arg interface{}) []interface{} {
switch arg := arg.(type) {
case []string:
for _, s := range arg {
dst = append(dst, s)
}
return dst
case []interface{}:
dst = append(dst, arg...)
return dst
case map[string]interface{}:
for k, v := range arg {
dst = append(dst, k, v)
}
return dst
case map[string]string:
for k, v := range arg {
dst = append(dst, k, v)
}
return dst
+ case map[string]float32: // Or any other case+ for k, v := range arg {+ dst = append(dst, k, fmt.Sprintf("%f", v))+ }+ return dst
case time.Time, time.Duration, encoding.BinaryMarshaler, net.IP:
A temporary (I hope) workaround I am currently using is to rebuild the map as a part of my custom redis service:
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi,
I have been trying to figure out for some time if this a missing feature or maybe is a missunderstanding, is it possible to call
HSet
/HMSet
with amap[string]float32
or any other map of numbers?, here is an example:Taking a look at https://github.com/redis/go-redis/blob/master/commands.go#L62, is seems that this particular case is missing, maybe something like this would be ok?
A temporary (I hope) workaround I am currently using is to rebuild the map as a part of my custom redis service:
Beta Was this translation helpful? Give feedback.
All reactions