nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: gomodule/redigo
Pull request #603 (2022-03-17 16:15:48)

feat: add RequestContext to PubSubConn
Add a wrapper that goes through the standard receiveInternal
processing to match the API of the existing PubSubConn Receive
methods.

Fixes: #592

pabigot: It doesn't seem that the design intent is to limit the features of PubSubConn, but an alternative approach that's only slightly less ideal would be to expose `pubsub/receiveInternal` as a visible, documented reply helper so we could do:

    m, err := redis.PubSubReply(redis.ReceiveContext(c, ctx))

That might be useful anyway.

Patch:
diff --git a/redis/pubsub.go b/redis/pubsub.go
index cc58575..67b885b 100644
--- a/redis/pubsub.go
+++ b/redis/pubsub.go
@@ -15,6 +15,7 @@
 package redis
 
 import (
+	"context"
 	"errors"
 	"time"
 )
@@ -116,6 +117,13 @@ func (c PubSubConn) ReceiveWithTimeout(timeout time.Duration) interface{} {
 	return c.receiveInternal(ReceiveWithTimeout(c.Conn, timeout))
 }
 
+// ReceiveContext is like Receive, but it allows termination of the receive
+// via a Context. If the call returns due to closure of the context's Done
+// channel the underlying Conn will have been closed.
+func (c PubSubConn) ReceiveContext(ctx context.Context) interface{} {
+	return c.receiveInternal(ReceiveContext(c.Conn, ctx))
+}
+
 func (c PubSubConn) receiveInternal(replyArg interface{}, errArg error) interface{} {
 	reply, err := Values(replyArg, errArg)
 	if err != nil {


Test patch:
diff --git a/redis/pubsub_test.go b/redis/pubsub_test.go
index 83b9158..63e0869 100644
--- a/redis/pubsub_test.go
+++ b/redis/pubsub_test.go
@@ -15,6 +15,8 @@
 package redis_test
 
 import (
+	"context"
+	"errors"
 	"reflect"
 	"testing"
 	"time"
@@ -74,3 +76,25 @@ func TestPushed(t *testing.T) {
 		t.Errorf("recv /w timeout got %v, want %v", got, want)
 	}
 }
+
+func TestPubSubReceiveContext(t *testing.T) {
+	sc, err := redis.DialDefaultServer()
+	if err != nil {
+		t.Fatalf("error connection to database, %v", err)
+	}
+	defer sc.Close()
+
+	c := redis.PubSubConn{Conn: sc}
+
+	require.NoError(t, c.Subscribe("c1"))
+	expectPushed(t, c, "Subscribe(c1)", redis.Subscription{Kind: "subscribe", Channel: "c1", Count: 1})
+
+	ctx, cancel := context.WithCancel(context.Background())
+	cancel()
+	got := c.ReceiveContext(ctx)
+	if err, ok := got.(error); !ok {
+		t.Errorf("recv w/canceled expected Canceled got non-error type %T", got)
+	} else if !errors.Is(err, context.Canceled) {
+		t.Errorf("recv w/canceled expected Canceled got %v", err)
+	}
+}
Repository: gomodule/redigo
Pull request #605 (2022-03-24 06:56:33)

feat: add Float64Map
add Float64Map()

zuoshuwen: Hey @stevenh , thanks for your advice

Patch:
diff --git a/redis/reply.go b/redis/reply.go
index 20232e7..5ece6d5 100644
--- a/redis/reply.go
+++ b/redis/reply.go
@@ -476,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) {


Test patch:
diff --git a/redis/reply_test.go b/redis/reply_test.go
index 8993bad..8f43828 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)),