From ff92bb1a78da8a7168d25a3a8a73b5b6407e3153 Mon Sep 17 00:00:00 2001 From: Sasha Mitchell Date: Sat, 3 Oct 2026 02:04:24 +0700 Subject: [PATCH] fix: Scan(nil) clears a previous ULID A NULL scan returned success and left the previous value in place, so a reused destination still looked like the old id. NULL now stores the zero ULID. --- ulid.go | 1 + ulid_test.go | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/ulid.go b/ulid.go index e07e1a6..d7ebb5d 100644 --- a/ulid.go +++ b/ulid.go @@ -501,6 +501,7 @@ func (id ULID) Compare(other ULID) int { func (id *ULID) Scan(src interface{}) error { switch x := src.(type) { case nil: + *id = Zero return nil case string: return id.UnmarshalText([]byte(x)) diff --git a/ulid_test.go b/ulid_test.go index afa194b..a268eb0 100644 --- a/ulid_test.go +++ b/ulid_test.go @@ -555,6 +555,16 @@ func TestOverflowHandling(t *testing.T) { } } +func TestScanNilClears(t *testing.T) { + id := ulid.MustParse("01ARYZ6S410000000000000000") + if err := id.Scan(nil); err != nil { + t.Fatal(err) + } + if !id.IsZero() { + t.Fatalf("got %s, want zero", id) + } +} + func TestScan(t *testing.T) { id := ulid.MustNew(123, crand.Reader)