diff --git a/uniqueidentifier_null.go b/uniqueidentifier_null.go new file mode 100644 index 00000000..3a9d023b --- /dev/null +++ b/uniqueidentifier_null.go @@ -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 +} diff --git a/uniqueidentifier_nullable_test.go b/uniqueidentifier_null_test.go similarity index 64% rename from uniqueidentifier_nullable_test.go rename to uniqueidentifier_null_test.go index d4be266f..61190ee4 100644 --- a/uniqueidentifier_nullable_test.go +++ b/uniqueidentifier_null_test.go @@ -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) @@ -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) @@ -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) @@ -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) @@ -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() @@ -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) @@ -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) { @@ -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{} diff --git a/uniqueidentifier_nullable.go b/uniqueidentifier_nullable.go deleted file mode 100644 index fb696b2e..00000000 --- a/uniqueidentifier_nullable.go +++ /dev/null @@ -1,37 +0,0 @@ -package mssql - -import ( - "database/sql/driver" -) - -type NullableUniqueIdentifier UniqueIdentifier - -func (n *NullableUniqueIdentifier) 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 = NullableUniqueIdentifier(u) - return err -} - -func (n NullableUniqueIdentifier) Value() (driver.Value, error) { - return UniqueIdentifier(n).Value() -} - -func (n NullableUniqueIdentifier) String() string { - return UniqueIdentifier(n).String() -} - -func (n NullableUniqueIdentifier) MarshalText() (text []byte, err error) { - return UniqueIdentifier(n).MarshalText() -} - -func (n *NullableUniqueIdentifier) UnmarshalJSON(b []byte) error { - u := UniqueIdentifier(*n) - err := u.UnmarshalJSON(b) - *n = NullableUniqueIdentifier(u) - return err -}