From 76d4450f66595eabd647b924093f83093590188b Mon Sep 17 00:00:00 2001 From: apocelipes Date: Mon, 9 Dec 2024 13:30:44 +0800 Subject: [PATCH 1/2] Fix UUID.Scan --- types.go | 34 ++++++++++++++++++++++++++++++++-- types_test.go | 10 ++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/types.go b/types.go index 8f601714..6faf705b 100644 --- a/types.go +++ b/types.go @@ -7,10 +7,12 @@ import "C" import ( "encoding/binary" + "encoding/hex" "fmt" "math/big" "strings" + "github.com/google/uuid" "github.com/mitchellh/mapstructure" ) @@ -23,12 +25,40 @@ const uuid_length = 16 type UUID [uuid_length]byte func (u *UUID) Scan(v any) error { - if n := copy(u[:], v.([]byte)); n != uuid_length { - return fmt.Errorf("invalid UUID length: %d", n) + switch val := v.(type) { + case []byte: + if len(val) != uuid_length { + return u.Scan(string(val)) + } + copy(u[:], val[:]) + case string: + id, err := uuid.Parse(val) + if err != nil { + return err + } + copy(u[:], id[:]) + default: + return fmt.Errorf("invalid UUID type: %#v", val) } return nil } +func (u *UUID) String() string { + buf := make([]byte, 36) + + hex.Encode(buf, u[:4]) + buf[8] = '-' + hex.Encode(buf[9:13], u[4:6]) + buf[13] = '-' + hex.Encode(buf[14:18], u[6:8]) + buf[18] = '-' + hex.Encode(buf[19:23], u[8:10]) + buf[23] = '-' + hex.Encode(buf[24:], u[10:]) + + return string(buf) +} + // duckdb_hugeint is composed of (lower, upper) components. // The value is computed as: upper * 2^64 + lower diff --git a/types_test.go b/types_test.go index da5d8942..bca361d0 100644 --- a/types_test.go +++ b/types_test.go @@ -643,6 +643,16 @@ func TestUUID(t *testing.T) { require.NoError(t, db.QueryRow(`SELECT ?::uuid`, test).Scan(&val)) require.Equal(t, test, val) + + var u UUID + require.NoError(t, db.QueryRow(`SELECT uuid FROM uuid_test WHERE uuid = ?`, test).Scan(&u)) + require.Equal(t, test.String(), u.String()) + + require.NoError(t, db.QueryRow(`SELECT ?`, test).Scan(&u)) + require.Equal(t, test.String(), u.String()) + + require.NoError(t, db.QueryRow(`SELECT ?::uuid`, test).Scan(&u)) + require.Equal(t, test.String(), u.String()) } require.NoError(t, db.Close()) From c2be05c0b97e212f1ec8fc24e05176bc874ab011 Mon Sep 17 00:00:00 2001 From: apocelipes Date: Tue, 10 Dec 2024 00:10:26 +0800 Subject: [PATCH 2/2] Add test cases to UUID scan errors --- types.go | 2 +- types_test.go | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/types.go b/types.go index 6faf705b..ff6f190e 100644 --- a/types.go +++ b/types.go @@ -38,7 +38,7 @@ func (u *UUID) Scan(v any) error { } copy(u[:], id[:]) default: - return fmt.Errorf("invalid UUID type: %#v", val) + return fmt.Errorf("invalid UUID value type: %T", val) } return nil } diff --git a/types_test.go b/types_test.go index bca361d0..22998de6 100644 --- a/types_test.go +++ b/types_test.go @@ -658,6 +658,19 @@ func TestUUID(t *testing.T) { require.NoError(t, db.Close()) } +func TestUUIDScanError(t *testing.T) { + t.Parallel() + db := openDB(t) + + var u UUID + // invalid value type + require.Error(t, db.QueryRow(`SELECT 12345`).Scan(&u)) + // string value not valid + require.Error(t, db.QueryRow(`SELECT 'I am not a UUID.'`).Scan(&u)) + // blob value not valid + require.Error(t, db.QueryRow(`SELECT '123456789012345678901234567890123456'::BLOB`).Scan(&u)) +} + func TestDate(t *testing.T) { t.Parallel() db := openDB(t)