Skip to content
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
11 changes: 11 additions & 0 deletions null.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ type NullUUID struct {
Valid bool // Valid is true if UUID is not NULL
}

// TryParse parses the given string and returns a NullUUID.
// The UUID is set to the parsed value and Valid is set to true if parsing succeeds.
// If parsing fails, Valid is set to false and UUID is left in its zero state.
func TryParse(s string) NullUUID {
v, err := Parse(s)
if err != nil {
return NullUUID{Valid: false}
}
return NullUUID{UUID: v, Valid: true}
}

// Scan implements the SQL driver.Scanner interface.
func (nu *NullUUID) Scan(value interface{}) error {
if value == nil {
Expand Down
15 changes: 15 additions & 0 deletions null_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@ import (
"testing"
)

func TestTryParse(t *testing.T) {
if got := TryParse(New().String()); !got.Valid {
t.Error("expected output to be valid")
}
if got := TryParse(Nil.String()); !got.Valid {
t.Error("expected output to be valid")
}
if got := TryParse("wrong"); got.Valid {
t.Error("expected output to not be valid")
}
if got := TryParse(""); got.Valid {
t.Error("expected output to not be valid")
}
}

func TestNullUUIDScan(t *testing.T) {
var u UUID
var nu NullUUID
Expand Down