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

Add Nullable version of uniqueidentifier #608

Open
wants to merge 1 commit 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
101 changes: 101 additions & 0 deletions nulluniqueidentifier.go
@@ -0,0 +1,101 @@
package mssql

import (
"database/sql/driver"
"encoding/hex"
"errors"
"fmt"
)

type NullUniqueIdentifier struct {
Valid bool
UUID [16]byte
}

func (u *NullUniqueIdentifier) Scan(v interface{}) error {
if v == nil {
u.UUID = [16]byte{}
u.Valid = false
return nil
}

reverse := func(b []byte) {
for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
b[i], b[j] = b[j], b[i]
}
}

switch vt := v.(type) {
case []byte:
if len(vt) != 16 {
return errors.New("mssql: invalid UniqueIdentifier length")
}

var raw [16]byte

copy(raw[:], vt)

reverse(raw[0:4])
reverse(raw[4:6])
reverse(raw[6:8])
u.UUID = raw
u.Valid = true

return nil
case string:
if len(vt) != 36 {
return errors.New("mssql: invalid UniqueIdentifier string length")
}

b := []byte(vt)
for i, c := range b {
switch c {
case '-':
b = append(b[:i], b[i+1:]...)
}
}

_, err := hex.Decode(u.UUID[:], []byte(b))
if err == nil {
u.Valid = true
}
return err
default:
return fmt.Errorf("mssql: cannot convert %T to UniqueIdentifier", v)
}
}

func (u NullUniqueIdentifier) Value() (driver.Value, error) {
if u.Valid == false {
return nil, nil
}

reverse := func(b []byte) {
for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
b[i], b[j] = b[j], b[i]
}
}

raw := make([]byte, len(u.UUID))
copy(raw, u.UUID[:])

reverse(raw[0:4])
reverse(raw[4:6])
reverse(raw[6:8])

return raw, nil
}

func (u NullUniqueIdentifier) String() string {
if u.Valid == false {
return ""
}

return fmt.Sprintf("%X-%X-%X-%X-%X", u.UUID[0:4], u.UUID[4:6], u.UUID[6:8], u.UUID[8:10], u.UUID[10:])
}

// MarshalText converts Uniqueidentifier to bytes corresponding to the stringified hexadecimal representation of the Uniqueidentifier
// e.g., "AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA" -> [65 65 65 65 65 65 65 65 45 65 65 65 65 45 65 65 65 65 45 65 65 65 65 65 65 65 65 65 65 65 65]
func (u NullUniqueIdentifier) MarshalText() []byte {
return []byte(u.String())
}
79 changes: 79 additions & 0 deletions nulluniqueidentifier_test.go
@@ -0,0 +1,79 @@
package mssql

import (
"bytes"
"database/sql"
"database/sql/driver"
"fmt"
"reflect"
"testing"
)

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

uuid := NullUniqueIdentifier{Valid: true, UUID: [16]byte{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 sut NullUniqueIdentifier
if err := sut.Scan(dbUUID[:]); err != nil {
t.Fatal(err)
}
if sut != uuid {
t.Errorf("bytes not swapped correctly: got %q; want %q", sut, uuid)
}
})

t.Run("string", func(t *testing.T) {
var sut NullUniqueIdentifier
if err := sut.Scan(uuid.String()); err != nil {
t.Fatal(err)
}
if sut != uuid {
t.Errorf("string not scanned correctly: got %q; want %q", sut, uuid)
}
})
})

t.Run("Value", func(t *testing.T) {
sut := uuid
v, err := sut.Value()
if err != nil {
t.Fatal(err)
}

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 TestNullUniqueIdentifierString(t *testing.T) {
sut := NullUniqueIdentifier{Valid: true, UUID: [16]byte{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)
}
}

func TestNullUniqueIdentifierMarshalText(t *testing.T) {
sut := NullUniqueIdentifier{Valid: true, UUID: [16]byte{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}
if actual := sut.MarshalText(); !reflect.DeepEqual(actual, expected) {
t.Errorf("sut.MarshalText() = %v; want %v", actual, expected)
}
}

var _ fmt.Stringer = NullUniqueIdentifier{}
var _ sql.Scanner = &NullUniqueIdentifier{}
var _ driver.Valuer = NullUniqueIdentifier{}