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)