diff --git a/internal/proto/scan.go b/internal/proto/scan.go index 5223069318..4142c3e870 100644 --- a/internal/proto/scan.go +++ b/internal/proto/scan.go @@ -6,7 +6,7 @@ import ( "net" "reflect" "time" - + "github.com/google/uuid" "github.com/redis/go-redis/v9/internal/util" ) @@ -23,6 +23,14 @@ func Scan(b []byte, v interface{}) error { case *[]byte: *v = b return nil + case *uuid.UUID: + str := util.BytesToString(b) + u, err := uuid.Parse(str) + if err != nil { + return fmt.Errorf("redis: failed to parse uuid: %w", err) + } + *v = u + return nil case *int: var err error *v, err = util.Atoi(b) @@ -120,6 +128,7 @@ func Scan(b []byte, v interface{}) error { case *net.IP: *v = b return nil + default: return fmt.Errorf( "redis: can't unmarshal %T (consider implementing BinaryUnmarshaler)", v) diff --git a/internal/proto/test_scanuuid.go b/internal/proto/test_scanuuid.go new file mode 100644 index 0000000000..452c39e60e --- /dev/null +++ b/internal/proto/test_scanuuid.go @@ -0,0 +1,20 @@ +package proto + +import ( + "github.com/google/uuid" + "testing" +) + +func TestScanUUID(t *testing.T) { + u1 := uuid.New() + var u2 uuid.UUID + + err := Scan([]byte(u1.String()), &u2) + if err != nil { + t.Fatalf("Scan failed: %v", err) + } + + if u1 != u2 { + t.Errorf("UUID mismatch, got %v, want %v", u2, u1) + } +}