From fd54f7be5969204b6845c4aa7dd332d31c96c59f Mon Sep 17 00:00:00 2001 From: Shulhan Date: Sat, 4 Apr 2026 02:30:09 +0700 Subject: lib/uuidv7: detect nil value on Scan Column with NULL value will returns no error but zero UUID. --- lib/uuidv7/uuidv7.go | 3 +++ lib/uuidv7/uuidv7_test.go | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/lib/uuidv7/uuidv7.go b/lib/uuidv7/uuidv7.go index e35e684b..21277e55 100644 --- a/lib/uuidv7/uuidv7.go +++ b/lib/uuidv7/uuidv7.go @@ -165,6 +165,7 @@ func (id *UUIDv7) UnmarshalText(data []byte) (err error) { // Scan scans the raw database value into id. // This method implement [database/sql.Scanner] interface. +// Column with NULL value will returns no error but zero UUID. func (id *UUIDv7) Scan(src any) (err error) { switch v := src.(type) { case []byte: @@ -172,6 +173,8 @@ func (id *UUIDv7) Scan(src any) (err error) { if err != nil { return err } + case nil: + return nil default: return fmt.Errorf(`uuidv7: Scan: invalid type %T`, src) } diff --git a/lib/uuidv7/uuidv7_test.go b/lib/uuidv7/uuidv7_test.go index 70ca315c..f9d90226 100644 --- a/lib/uuidv7/uuidv7_test.go +++ b/lib/uuidv7/uuidv7_test.go @@ -14,6 +14,7 @@ func TestUUIDv7_Scan(t *testing.T) { desc string data any expError string + exp UUIDv7 }{{ desc: `With empty data`, data: `017F22E2-79B0-7CC3-98C4-DC0C0C07398F`, @@ -22,6 +23,10 @@ func TestUUIDv7_Scan(t *testing.T) { desc: `With invalid version`, data: []byte(`5c146b14-3c52-4afd-938a-375d0df1fbf6`), expError: `uuidv7: invalid version 2`, + }, { + desc: `With nil`, + data: nil, + expError: ``, }} for _, tc := range listCase { var id UUIDv7 -- cgit v1.3