forked from denisenkom/go-mssqldb
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Add NullableUniqueIdentifier type
* Refactor UniqueIdentifier tests * Parallelize tests * Add NullableUniqueIdentifier type * Add missing test case for UniqueIdentifier * Improve error message * Rename to NullUniqueIdentifier * Add NullUniqueIdentifier to TestBulkcopy * Add uniqueidentifier parsing to the list of Features * Add Valid bool to NullUniqueIdentifier * Handle null in UnmarshalJSON() * Handle !Valid in Value(),String(),MarshalText() --------- Co-authored-by: Norman Gehrsitz <[email protected]>
- Loading branch information
Showing
5 changed files
with
354 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package mssql | ||
|
||
import ( | ||
"database/sql/driver" | ||
) | ||
|
||
type NullUniqueIdentifier struct { | ||
UUID UniqueIdentifier | ||
Valid bool // Valid is true if UUID is not NULL | ||
} | ||
|
||
func (n *NullUniqueIdentifier) Scan(v interface{}) error { | ||
if v == nil { | ||
*n = NullUniqueIdentifier{ | ||
UUID: [16]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, | ||
Valid: false, | ||
} | ||
return nil | ||
} | ||
u := n.UUID | ||
err := u.Scan(v) | ||
*n = NullUniqueIdentifier{ | ||
UUID: u, | ||
Valid: true, | ||
} | ||
return err | ||
} | ||
|
||
func (n NullUniqueIdentifier) Value() (driver.Value, error) { | ||
if !n.Valid { | ||
return nil, nil | ||
} | ||
return n.UUID.Value() | ||
} | ||
|
||
func (n NullUniqueIdentifier) String() string { | ||
if !n.Valid { | ||
return "NULL" | ||
} | ||
return n.UUID.String() | ||
} | ||
|
||
func (n NullUniqueIdentifier) MarshalText() (text []byte, err error) { | ||
if !n.Valid { | ||
return []byte("null"), nil | ||
} | ||
return n.UUID.MarshalText() | ||
} | ||
|
||
func (n *NullUniqueIdentifier) UnmarshalJSON(b []byte) error { | ||
u := n.UUID | ||
if string(b) == "null" { | ||
*n = NullUniqueIdentifier{ | ||
UUID: [16]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, | ||
Valid: false, | ||
} | ||
return nil | ||
} | ||
err := u.UnmarshalJSON(b) | ||
*n = NullUniqueIdentifier{ | ||
UUID: u, | ||
Valid: true, | ||
} | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,215 @@ | ||
package mssql | ||
|
||
import ( | ||
"bytes" | ||
"database/sql" | ||
"database/sql/driver" | ||
"fmt" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestNullableUniqueIdentifierScanNull(t *testing.T) { | ||
t.Parallel() | ||
nullUUID := NullUniqueIdentifier{ | ||
UUID: [16]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, | ||
Valid: false, | ||
} | ||
|
||
sut := NullUniqueIdentifier{ | ||
UUID: [16]byte{0x1}, | ||
Valid: true, | ||
} | ||
scanErr := sut.Scan(nil) // NULL in the DB | ||
if scanErr != nil { | ||
t.Fatal("NullUniqueIdentifier should not error out on Scan(nil)") | ||
} | ||
if sut != nullUUID { | ||
t.Errorf("bytes not swapped correctly: got %q; want %q", sut, nullUUID) | ||
} | ||
} | ||
|
||
func TestNullableUniqueIdentifierScanBytes(t *testing.T) { | ||
t.Parallel() | ||
dbUUID := [16]byte{0x67, 0x45, 0x23, 0x01, 0xAB, 0x89, 0xEF, 0xCD, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF} | ||
uuid := NullUniqueIdentifier{ | ||
UUID: [16]byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}, | ||
Valid: true, | ||
} | ||
|
||
var sut NullUniqueIdentifier | ||
scanErr := sut.Scan(dbUUID[:]) | ||
if scanErr != nil { | ||
t.Fatal(scanErr) | ||
} | ||
if sut != uuid { | ||
t.Errorf("bytes not swapped correctly: got %q; want %q", sut, uuid) | ||
} | ||
} | ||
|
||
func TestNullableUniqueIdentifierScanString(t *testing.T) { | ||
t.Parallel() | ||
uuid := NullUniqueIdentifier{ | ||
UUID: [16]byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}, | ||
Valid: true, | ||
} | ||
|
||
var sut NullUniqueIdentifier | ||
scanErr := sut.Scan(uuid.String()) | ||
if scanErr != nil { | ||
t.Fatal(scanErr) | ||
} | ||
if sut != uuid { | ||
t.Errorf("string not scanned correctly: got %q; want %q", sut, uuid) | ||
} | ||
} | ||
|
||
func TestNullableUniqueIdentifierScanUnexpectedType(t *testing.T) { | ||
t.Parallel() | ||
var sut NullUniqueIdentifier | ||
scanErr := sut.Scan(int(1)) | ||
if scanErr == nil { | ||
t.Fatal(scanErr) | ||
} | ||
} | ||
|
||
func TestNullableUniqueIdentifierValue(t *testing.T) { | ||
t.Parallel() | ||
dbUUID := [16]byte{0x67, 0x45, 0x23, 0x01, 0xAB, 0x89, 0xEF, 0xCD, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF} | ||
|
||
uuid := NullUniqueIdentifier{ | ||
UUID: [16]byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}, | ||
Valid: true, | ||
} | ||
|
||
sut := uuid | ||
v, valueErr := sut.Value() | ||
if valueErr != nil { | ||
t.Fatal(valueErr) | ||
} | ||
|
||
b, ok := v.([]byte) | ||
if !ok { | ||
t.Fatalf("(%T) is not []byte", v) | ||
} | ||
|
||
if !bytes.Equal(b, dbUUID[:]) { | ||
t.Errorf("got %q; want %q", b, dbUUID) | ||
} | ||
} | ||
|
||
func TestNullableUniqueIdentifierValueNull(t *testing.T) { | ||
t.Parallel() | ||
uuid := NullUniqueIdentifier{ | ||
UUID: [16]byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}, | ||
Valid: false, | ||
} | ||
|
||
sut := uuid | ||
v, valueErr := sut.Value() | ||
if valueErr != nil { | ||
t.Errorf("unexpected error for invalid uuid: %s", valueErr) | ||
} | ||
|
||
if v != nil { | ||
t.Errorf("expected non-nil value for invalid uuid: %s", v) | ||
} | ||
} | ||
|
||
func TestNullableUniqueIdentifierString(t *testing.T) { | ||
t.Parallel() | ||
sut := NullUniqueIdentifier{ | ||
UUID: [16]byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}, | ||
Valid: true, | ||
} | ||
expected := "01234567-89AB-CDEF-0123-456789ABCDEF" | ||
if actual := sut.String(); actual != expected { | ||
t.Errorf("sut.String() = %s; want %s", sut, expected) | ||
} | ||
} | ||
|
||
func TestNullableUniqueIdentifierStringNull(t *testing.T) { | ||
t.Parallel() | ||
sut := NullUniqueIdentifier{ | ||
UUID: [16]byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}, | ||
Valid: false, | ||
} | ||
expected := "NULL" | ||
if actual := sut.String(); actual != expected { | ||
t.Errorf("sut.String() = %s; want %s", sut, expected) | ||
} | ||
} | ||
|
||
func TestNullableUniqueIdentifierMarshalText(t *testing.T) { | ||
t.Parallel() | ||
sut := NullUniqueIdentifier{ | ||
UUID: [16]byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}, | ||
Valid: true, | ||
} | ||
expected := []byte{48, 49, 50, 51, 52, 53, 54, 55, 45, 56, 57, 65, 66, 45, 67, 68, 69, 70, 45, 48, 49, 50, 51, 45, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70} | ||
text, marshalErr := sut.MarshalText() | ||
if marshalErr != nil { | ||
t.Errorf("unexpected error while marshalling: %s", marshalErr) | ||
} | ||
if actual := text; !reflect.DeepEqual(actual, expected) { | ||
t.Errorf("sut.MarshalText() = %v; want %v", actual, expected) | ||
} | ||
} | ||
|
||
func TestNullableUniqueIdentifierMarshalTextNull(t *testing.T) { | ||
t.Parallel() | ||
sut := NullUniqueIdentifier{ | ||
UUID: [16]byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}, | ||
Valid: false, | ||
} | ||
expected := []byte("null") | ||
text, marshalErr := sut.MarshalText() | ||
if marshalErr != nil { | ||
t.Errorf("unexpected error while marshalling: %s", marshalErr) | ||
} | ||
if actual := text; !reflect.DeepEqual(actual, expected) { | ||
t.Errorf("sut.MarshalText() = %v; want %v", actual, expected) | ||
} | ||
} | ||
|
||
func TestNullableUniqueIdentifierUnmarshalJSON(t *testing.T) { | ||
t.Parallel() | ||
input := []byte("01234567-89AB-CDEF-0123-456789ABCDEF") | ||
var u NullUniqueIdentifier | ||
|
||
err := u.UnmarshalJSON(input) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
expected := NullUniqueIdentifier{ | ||
UUID: [16]byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}, | ||
Valid: true, | ||
} | ||
if u != expected { | ||
t.Errorf("u.UnmarshalJSON() = %v; want %v", u, expected) | ||
} | ||
} | ||
|
||
func TestNullableUniqueIdentifierUnmarshalJSONNull(t *testing.T) { | ||
t.Parallel() | ||
u := NullUniqueIdentifier{ | ||
UUID: [16]byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}, | ||
Valid: true, | ||
} | ||
|
||
err := u.UnmarshalJSON([]byte("null")) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
expected := NullUniqueIdentifier{ | ||
UUID: [16]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, | ||
Valid: false, | ||
} | ||
if u != expected { | ||
t.Errorf("u.UnmarshalJSON() = %v; want %v", u, expected) | ||
} | ||
} | ||
|
||
var _ fmt.Stringer = NullUniqueIdentifier{} | ||
var _ sql.Scanner = &NullUniqueIdentifier{} | ||
var _ driver.Valuer = NullUniqueIdentifier{} |
Oops, something went wrong.