From 58b185606ce70e76dd19d5700c68c8fb99cf1ee7 Mon Sep 17 00:00:00 2001 From: it512 Date: Sun, 29 Sep 2024 22:11:05 +0800 Subject: [PATCH] feat add IsZero --- null.go | 22 +++++++++++++--------- null_test.go | 17 +++++++++++++++++ uuid.go | 17 ++++++++++++----- uuid_test.go | 12 ++++++++++++ 4 files changed, 54 insertions(+), 14 deletions(-) diff --git a/null.go b/null.go index d7fcbf2..293779c 100644 --- a/null.go +++ b/null.go @@ -17,15 +17,14 @@ var jsonNull = []byte("null") // NullUUID implements the SQL driver.Scanner interface so // it can be used as a scan destination: // -// var u uuid.NullUUID -// err := db.QueryRow("SELECT name FROM foo WHERE id=?", id).Scan(&u) -// ... -// if u.Valid { -// // use u.UUID -// } else { -// // NULL value -// } -// +// var u uuid.NullUUID +// err := db.QueryRow("SELECT name FROM foo WHERE id=?", id).Scan(&u) +// ... +// if u.Valid { +// // use u.UUID +// } else { +// // NULL value +// } type NullUUID struct { UUID UUID Valid bool // Valid is true if UUID is not NULL @@ -116,3 +115,8 @@ func (nu *NullUUID) UnmarshalJSON(data []byte) error { nu.Valid = err == nil return err } + +// IsZero determine whether the value is zero +func (nu NullUUID) IsZero() bool { + return nu.Valid && nu.UUID == Nil +} diff --git a/null_test.go b/null_test.go index fe0fe8d..a8a5fa1 100644 --- a/null_test.go +++ b/null_test.go @@ -212,3 +212,20 @@ func TestNullUUIDUnmarshalJSON(t *testing.T) { t.Errorf("expected nil when unmarshalling null, got %s", err) } } + +func TestNullUUIDIsZero(t *testing.T) { + var nu NullUUID + if nu.IsZero() { + t.Error("expected false got true") + } + + nu.Valid = true + if !nu.IsZero() { + t.Error("expected true got false") + } + + nu.UUID = Max + if nu.IsZero() { + t.Error("expected false got true") + } +} diff --git a/uuid.go b/uuid.go index dc75cee..ef7e356 100644 --- a/uuid.go +++ b/uuid.go @@ -52,7 +52,7 @@ var ( ErrInvalidBracketedFormat = errors.New("invalid bracketed UUID format") ) -type URNPrefixError struct { prefix string } +type URNPrefixError struct{ prefix string } func (e URNPrefixError) Error() string { return fmt.Sprintf("invalid urn prefix: %q", e.prefix) @@ -215,10 +215,12 @@ func Must(uuid UUID, err error) UUID { } // Validate returns an error if s is not a properly formatted UUID in one of the following formats: -// xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx -// urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx -// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx -// {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} +// +// xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx +// urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx +// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx +// {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} +// // It returns an error if the format is invalid, otherwise nil. func Validate(s string) error { switch len(s) { @@ -315,6 +317,11 @@ func (uuid UUID) Version() Version { return Version(uuid[6] >> 4) } +// IsZero determine whether the value is zero +func (uuid UUID) IsZero() bool { + return uuid == Nil +} + func (v Version) String() string { if v > 15 { return fmt.Sprintf("BAD_VERSION_%d", v) diff --git a/uuid_test.go b/uuid_test.go index 906ecbe..f091a09 100644 --- a/uuid_test.go +++ b/uuid_test.go @@ -928,3 +928,15 @@ func TestVersion7MonotonicityStrict(t *testing.T) { u1 = u2 } } + +func TestUUIDIsZero(t *testing.T) { + var u UUID + if !u.IsZero() { + t.Error("expected true got false") + } + + u = New() + if u.IsZero() { + t.Error("expected false got true") + } +}