From 8b186c25eaa1d71888a3c01603ab3cff045ba33a Mon Sep 17 00:00:00 2001 From: alphagot Date: Tue, 1 Jun 2021 09:36:47 +0800 Subject: [PATCH] add Float64Map --- redis/reply.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/redis/reply.go b/redis/reply.go index dfe6aff7..d0b502e9 100644 --- a/redis/reply.go +++ b/redis/reply.go @@ -449,6 +449,32 @@ func Int64Map(result interface{}, err error) (map[string]int64, error) { return m, nil } +// Float64Map is a helper that converts an array of strings (alternating key, value) +// into a map[string]Float64Map. The HGETALL commands return replies in this format. +// Requires an even number of values in result. +func Float64Map(result interface{}, err error) (map[string]float64, error) { + values, err := Values(result, err) + if err != nil { + return nil, err + } + if len(values)%2 != 0 { + return nil, errors.New("redigo: Float64Map expects even number of values result") + } + m := make(map[string]float64, len(values)/2) + for i := 0; i < len(values); i += 2 { + key, ok := values[i].([]byte) + if !ok { + return nil, errors.New("redigo: Float64Map key not a bulk string value") + } + value, err := Float64(values[i+1], nil) + if err != nil { + return nil, err + } + m[string(key)] = value + } + return m, nil +} + // Positions is a helper that converts an array of positions (lat, long) // into a [][2]float64. The GEOPOS command returns replies in this format. func Positions(result interface{}, err error) ([]*[2]float64, error) {