Skip to content

Commit a828bba

Browse files
committed
feat(proto): support uuid.UUID in Scan function
1 parent 7ac4021 commit a828bba

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

internal/proto/scan.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"net"
77
"reflect"
88
"time"
9-
9+
"github.com/google/uuid"
1010
"github.com/redis/go-redis/v9/internal/util"
1111
)
1212

@@ -23,6 +23,14 @@ func Scan(b []byte, v interface{}) error {
2323
case *[]byte:
2424
*v = b
2525
return nil
26+
case *uuid.UUID:
27+
str := util.BytesToString(b)
28+
u, err := uuid.Parse(str)
29+
if err != nil {
30+
return fmt.Errorf("redis: failed to parse uuid: %w", err)
31+
}
32+
*v = u
33+
return nil
2634
case *int:
2735
var err error
2836
*v, err = util.Atoi(b)
@@ -120,6 +128,7 @@ func Scan(b []byte, v interface{}) error {
120128
case *net.IP:
121129
*v = b
122130
return nil
131+
123132
default:
124133
return fmt.Errorf(
125134
"redis: can't unmarshal %T (consider implementing BinaryUnmarshaler)", v)

internal/proto/test_scanuuid.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package proto
2+
3+
import (
4+
"github.com/google/uuid"
5+
"testing"
6+
)
7+
8+
func TestScanUUID(t *testing.T) {
9+
u1 := uuid.New()
10+
var u2 uuid.UUID
11+
12+
err := Scan([]byte(u1.String()), &u2)
13+
if err != nil {
14+
t.Fatalf("Scan failed: %v", err)
15+
}
16+
17+
if u1 != u2 {
18+
t.Errorf("UUID mismatch, got %v, want %v", u2, u1)
19+
}
20+
}

0 commit comments

Comments
 (0)