Skip to content

Commit

Permalink
Merge pull request #58 from nanjj/sqlscan
Browse files Browse the repository at this point in the history
Change (*UUID) Scan to accept type UUID argument
  • Loading branch information
acln0 authored Oct 30, 2018
2 parents 48eeef7 + 0ab72f8 commit 7077aa6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ func (u UUID) Value() (driver.Value, error) {
// a longer byte slice or a string will be handled by UnmarshalText.
func (u *UUID) Scan(src interface{}) error {
switch src := src.(type) {
case UUID: // support gorm convert from UUID to NullUUID
*u = src
return nil

case []byte:
if len(src) == Size {
return u.UnmarshalBinary(src)
Expand Down
15 changes: 15 additions & 0 deletions sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func TestNullUUID(t *testing.T) {
t.Run("Scan", func(t *testing.T) {
t.Run("Nil", testNullUUIDScanNil)
t.Run("Valid", testNullUUIDScanValid)
t.Run("UUID", testNullUUIDScanUUID)
})

t.Run("MarshalJSON", func(t *testing.T) {
Expand Down Expand Up @@ -193,6 +194,20 @@ func testNullUUIDScanValid(t *testing.T) {
}
}

func testNullUUIDScanUUID(t *testing.T) {
u := NullUUID{}
err := u.Scan(codecTestUUID)
if err != nil {
t.Fatal(err)
}
if !u.Valid {
t.Errorf("Valid == false after scan(%v)", codecTestUUID)
}
if u.UUID != codecTestUUID {
t.Errorf("UUID == %v after Scan(%v), want %v", u.UUID, codecTestUUID, codecTestUUID)
}
}

func testNullUUIDMarshalJSONNil(t *testing.T) {
u := NullUUID{Valid: true}

Expand Down

0 comments on commit 7077aa6

Please sign in to comment.