From a828bba7680746c7890b358ab13efad91cef688f Mon Sep 17 00:00:00 2001 From: LinYuan201712 <1254580337@qq.com> Date: Wed, 9 Jul 2025 14:51:23 +0800 Subject: [PATCH] feat(proto): support uuid.UUID in Scan function --- internal/proto/scan.go | 11 ++++++++++- internal/proto/test_scanuuid.go | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 internal/proto/test_scanuuid.go 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) + } +}