From b17bc261bd6cbe06cd43f5531f65d9c78a635866 Mon Sep 17 00:00:00 2001 From: sown Date: Thu, 24 Mar 2022 14:39:29 +0800 Subject: [PATCH 1/3] feat: add Float64Map --- redis/reply.go | 30 ++++++++++++++++++++++++++++++ redis/reply_test.go | 5 +++++ 2 files changed, 35 insertions(+) diff --git a/redis/reply.go b/redis/reply.go index 20232e70..7eed55ed 100644 --- a/redis/reply.go +++ b/redis/reply.go @@ -140,6 +140,36 @@ func Float64(reply interface{}, err error) (float64, error) { return 0, fmt.Errorf("redigo: unexpected type for Float64, got type %T", reply) } +// Float64Map is a helper that converts an array of strings (alternating key, value) +// into a map[string]Float64. 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, fmt.Errorf("redigo: Float64Map expects even number of values result, got %d", len(values)) + } + + 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, fmt.Errorf("redigo: Float64Map key[%d] not a bulk string value, got %T", i, values[i]) + } + + value, err := Float64(values[i+1], nil) + if err != nil { + return nil, err + } + + m[string(key)] = value + } + return m, nil +} + // String is a helper that converts a command reply to a string. If err is not // equal to nil, then String returns "", err. Otherwise String converts the // reply to a string as follows: diff --git a/redis/reply_test.go b/redis/reply_test.go index 8993bad1..8f43828c 100644 --- a/redis/reply_test.go +++ b/redis/reply_test.go @@ -123,6 +123,11 @@ var replyTests = []struct { ve(redis.Float64(nil, nil)), ve(float64(0.0), redis.ErrNil), }, + { + "float64Map([[]byte, []byte])", + ve(redis.Float64Map([]interface{}{[]byte("key1"), []byte("1.234"), []byte("key2"), []byte("5.678")}, nil)), + ve(map[string]float64{"key1": 1.234, "key2": 5.678}, nil), + }, { "uint64(1)", ve(redis.Uint64(int64(1), nil)), From 5971fef8adf5944146ca0c90aa01f2d36009910b Mon Sep 17 00:00:00 2001 From: sown Date: Thu, 24 Mar 2022 21:30:22 +0800 Subject: [PATCH 2/3] feat: add Float64Map --- redis/reply.go | 60 +++++++++++++++++++++++++------------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/redis/reply.go b/redis/reply.go index 7eed55ed..632f29c9 100644 --- a/redis/reply.go +++ b/redis/reply.go @@ -140,36 +140,6 @@ func Float64(reply interface{}, err error) (float64, error) { return 0, fmt.Errorf("redigo: unexpected type for Float64, got type %T", reply) } -// Float64Map is a helper that converts an array of strings (alternating key, value) -// into a map[string]Float64. 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, fmt.Errorf("redigo: Float64Map expects even number of values result, got %d", len(values)) - } - - 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, fmt.Errorf("redigo: Float64Map key[%d] not a bulk string value, got %T", i, values[i]) - } - - value, err := Float64(values[i+1], nil) - if err != nil { - return nil, err - } - - m[string(key)] = value - } - return m, nil -} - // String is a helper that converts a command reply to a string. If err is not // equal to nil, then String returns "", err. Otherwise String converts the // reply to a string as follows: @@ -506,6 +476,36 @@ 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]Float64. 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, fmt.Errorf("redigo: Float64Map expects even number of values result, got %d", len(values)) + } + + 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, fmt.Errorf("redigo: Float64Map key[%d] not a bulk string value, got %T", i, values[i]) + } + + 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) { From f3aa2c4ce669760a1384458363f37676d03407b4 Mon Sep 17 00:00:00 2001 From: Sown Date: Thu, 24 Mar 2022 23:03:12 +0800 Subject: [PATCH 3/3] feat: add Float64Map Co-authored-by: Steven Hartland --- redis/reply.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/redis/reply.go b/redis/reply.go index 632f29c9..5ece6d55 100644 --- a/redis/reply.go +++ b/redis/reply.go @@ -477,7 +477,7 @@ func Int64Map(result interface{}, err error) (map[string]int64, error) { } // Float64Map is a helper that converts an array of strings (alternating key, value) -// into a map[string]Float64. The HGETALL commands return replies in this format. +// into a map[string]float64. 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)