Skip to content

Commit

Permalink
Rename to NullUniqueIdentifier
Browse files Browse the repository at this point in the history
  • Loading branch information
Norman Gehrsitz committed Nov 2, 2023
1 parent 7ae8aa0 commit eab3da4
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 55 deletions.
37 changes: 37 additions & 0 deletions uniqueidentifier_null.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package mssql

import (
"database/sql/driver"
)

type NullUniqueIdentifier UniqueIdentifier

func (n *NullUniqueIdentifier) Scan(v interface{}) error {
u := UniqueIdentifier(*n)
if v == nil {
*n = [16]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}
return nil
}
err := u.Scan(v)
*n = NullUniqueIdentifier(u)
return err
}

func (n NullUniqueIdentifier) Value() (driver.Value, error) {
return UniqueIdentifier(n).Value()
}

func (n NullUniqueIdentifier) String() string {
return UniqueIdentifier(n).String()
}

func (n NullUniqueIdentifier) MarshalText() (text []byte, err error) {
return UniqueIdentifier(n).MarshalText()
}

func (n *NullUniqueIdentifier) UnmarshalJSON(b []byte) error {
u := UniqueIdentifier(*n)
err := u.UnmarshalJSON(b)
*n = NullUniqueIdentifier(u)
return err
}
36 changes: 18 additions & 18 deletions uniqueidentifier_nullable_test.go → uniqueidentifier_null_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import (

func TestNullableUniqueIdentifierScanNull(t *testing.T) {
t.Parallel()
nullUUID := NullableUniqueIdentifier{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}
nullUUID := NullUniqueIdentifier{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}

sut := NullableUniqueIdentifier{0x01}
sut := NullUniqueIdentifier{0x01}
scanErr := sut.Scan(nil) // NULL in the DB
if scanErr != nil {
t.Fatal("NullableUniqueIdentifier should not error out on Scan(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)
Expand All @@ -25,14 +25,14 @@ func TestNullableUniqueIdentifierScanNull(t *testing.T) {

func TestNullableUniqueIdentifierScanBytes(t *testing.T) {
t.Parallel()
dbUUID := NullableUniqueIdentifier{0x67, 0x45, 0x23, 0x01,
dbUUID := NullUniqueIdentifier{0x67, 0x45, 0x23, 0x01,
0xAB, 0x89,
0xEF, 0xCD,
0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
}
uuid := NullableUniqueIdentifier{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}
uuid := NullUniqueIdentifier{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}

var sut NullableUniqueIdentifier
var sut NullUniqueIdentifier
scanErr := sut.Scan(dbUUID[:])
if scanErr != nil {
t.Fatal(scanErr)
Expand All @@ -44,9 +44,9 @@ func TestNullableUniqueIdentifierScanBytes(t *testing.T) {

func TestNullableUniqueIdentifierScanString(t *testing.T) {
t.Parallel()
uuid := NullableUniqueIdentifier{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}
uuid := NullUniqueIdentifier{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}

var sut NullableUniqueIdentifier
var sut NullUniqueIdentifier
scanErr := sut.Scan(uuid.String())
if scanErr != nil {
t.Fatal(scanErr)
Expand All @@ -58,7 +58,7 @@ func TestNullableUniqueIdentifierScanString(t *testing.T) {

func TestNullableUniqueIdentifierScanUnexpectedType(t *testing.T) {
t.Parallel()
var sut NullableUniqueIdentifier
var sut NullUniqueIdentifier
scanErr := sut.Scan(int(1))
if scanErr == nil {
t.Fatal(scanErr)
Expand All @@ -67,13 +67,13 @@ func TestNullableUniqueIdentifierScanUnexpectedType(t *testing.T) {

func TestNullableUniqueIdentifierValue(t *testing.T) {
t.Parallel()
dbUUID := NullableUniqueIdentifier{0x67, 0x45, 0x23, 0x01,
dbUUID := NullUniqueIdentifier{0x67, 0x45, 0x23, 0x01,
0xAB, 0x89,
0xEF, 0xCD,
0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
}

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

sut := uuid
v, valueErr := sut.Value()
Expand All @@ -93,7 +93,7 @@ func TestNullableUniqueIdentifierValue(t *testing.T) {

func TestNullableUniqueIdentifierString(t *testing.T) {
t.Parallel()
sut := NullableUniqueIdentifier{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}
sut := NullUniqueIdentifier{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}
expected := "01234567-89AB-CDEF-0123-456789ABCDEF"
if actual := sut.String(); actual != expected {
t.Errorf("sut.String() = %s; want %s", sut, expected)
Expand All @@ -102,7 +102,7 @@ func TestNullableUniqueIdentifierString(t *testing.T) {

func TestNullableUniqueIdentifierMarshalText(t *testing.T) {
t.Parallel()
sut := NullableUniqueIdentifier{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}
sut := NullUniqueIdentifier{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}
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, _ := sut.MarshalText()
if actual := text; !reflect.DeepEqual(actual, expected) {
Expand All @@ -113,18 +113,18 @@ func TestNullableUniqueIdentifierMarshalText(t *testing.T) {
func TestNullableUniqueIdentifierUnmarshalJSON(t *testing.T) {
t.Parallel()
input := []byte("01234567-89AB-CDEF-0123-456789ABCDEF")
var u NullableUniqueIdentifier
var u NullUniqueIdentifier

err := u.UnmarshalJSON(input)
if err != nil {
t.Fatal(err)
}
expected := NullableUniqueIdentifier{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}
expected := NullUniqueIdentifier{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}
if u != expected {
t.Errorf("u.UnmarshalJSON() = %v; want %v", u, expected)
}
}

var _ fmt.Stringer = NullableUniqueIdentifier{}
var _ sql.Scanner = &NullableUniqueIdentifier{}
var _ driver.Valuer = NullableUniqueIdentifier{}
var _ fmt.Stringer = NullUniqueIdentifier{}
var _ sql.Scanner = &NullUniqueIdentifier{}
var _ driver.Valuer = NullUniqueIdentifier{}
37 changes: 0 additions & 37 deletions uniqueidentifier_nullable.go

This file was deleted.

0 comments on commit eab3da4

Please sign in to comment.