Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add IsZero #167

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions null.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
17 changes: 17 additions & 0 deletions null_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
17 changes: 12 additions & 5 deletions uuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions uuid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}