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

Added NullUniqueIdentifier struct to work with GUIDs that can be null #635

Open
wants to merge 2 commits 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
39 changes: 39 additions & 0 deletions nulluniqueidentifier.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package mssql

// NullUniqueIdentifier represents a GUID that may be null.
// NullUniqueIdentifier implements the Scanner interface so it can be used as a scan destination.
type NullUniqueIdentifier struct {
UniqueIdentifier UniqueIdentifier
Valid bool
}

// Scan implements the Scanner interface.
func (nui *NullUniqueIdentifier) Scan(v interface{}) error {

if v == nil {

nui.Valid = false
return nil
}

err := nui.UniqueIdentifier.Scan(v)
if err != nil {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Converting any possible scan error into a Null value doesn't seem right to me, error checking should be more specific


nui.Valid = false
return nil
}

nui.Valid = true
return nil
}

// String returns the UniqueIdentifier value
func (nui NullUniqueIdentifier) String() string {

if !nui.Valid {

return ""
}

return nui.UniqueIdentifier.String()
}
88 changes: 88 additions & 0 deletions nulluniqueidentifier_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package mssql

import "testing"

func TestNullUniqueIdentifier(t *testing.T) {
dbUUID := UniqueIdentifier{0x67, 0x45, 0x23, 0x01,
0xAB, 0x89,
0xEF, 0xCD,
0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
}

uuid := UniqueIdentifier{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}

t.Run("Scan", func(t *testing.T) {

t.Run("[]byte", func(t *testing.T) {

var nui NullUniqueIdentifier
if err := nui.Scan(dbUUID[:]); err != nil {

t.Fatal(err)
}
if nui.UniqueIdentifier != uuid {

t.Errorf("bytes not swapped correctly: got %q; want %q", nui.UniqueIdentifier, uuid)
}
})

t.Run("string", func(t *testing.T) {

var nui NullUniqueIdentifier
if err := nui.Scan(uuid.String()); err != nil {

t.Fatal(err)
}
if nui.UniqueIdentifier != uuid {

t.Errorf("bytes not swapped correctly: got %q; want %q", nui.UniqueIdentifier, uuid)
}
})

t.Run("nil", func(t *testing.T) {

var nui NullUniqueIdentifier
var null interface{}
if err := nui.Scan(null); err != nil {

t.Fatal(err)
}
if nui.Valid {

t.Errorf("Validity not correct: got %t; want false", nui.Valid)
}
})
})

t.Run("String", func(t *testing.T) {

t.Run("Empty string", func(t *testing.T) {

var nui NullUniqueIdentifier
var null interface{}
if err := nui.Scan(null); err != nil {

t.Fatal(err)
}

if str := nui.String(); str != "" {

t.Errorf("String invalid: got %s; want %s", str, `""`)
}
})

t.Run("String", func(t *testing.T) {

var nui NullUniqueIdentifier
if err := nui.Scan(dbUUID[:]); err != nil {

t.Fatal(err)
}

if str := nui.String(); str == "" {

t.Errorf("String invalid: got %s; want %s", "67452301-AB89-EFCD-0123-456789ABCDEF", str)
}
})
})
}