Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into apoorvdeshmukh/tds8
Browse files Browse the repository at this point in the history
  • Loading branch information
apoorvdeshmukh committed Jul 13, 2023
2 parents 0424103 + b607336 commit 2534789
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
23 changes: 21 additions & 2 deletions uniqueidentifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/hex"
"errors"
"fmt"
"strings"
)

type UniqueIdentifier [16]byte
Expand Down Expand Up @@ -75,6 +76,24 @@ func (u UniqueIdentifier) String() string {

// 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 UniqueIdentifier) MarshalText() []byte {
return []byte(u.String())
func (u UniqueIdentifier) MarshalText() (text []byte, err error) {
text = []byte(u.String())
return
}

// Unmarshals a string representation of a UniqueIndentifier to bytes
// "01234567-89AB-CDEF-0123-456789ABCDEF" -> [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]
func (u *UniqueIdentifier) UnmarshalJSON(b []byte) error {
// remove quotes
input := strings.Trim(string(b), `"`)
// decode
bytes, err := hex.DecodeString(strings.Replace(input, "-", "", -1))

if err != nil {
return err
}
// Copy the bytes to the UniqueIdentifier
copy(u[:], bytes)

return nil
}
17 changes: 16 additions & 1 deletion uniqueidentifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,26 @@ func TestUniqueIdentifierString(t *testing.T) {
func TestUniqueIdentifierMarshalText(t *testing.T) {
sut := UniqueIdentifier{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) {
text, _ := sut.MarshalText()
if actual := text; !reflect.DeepEqual(actual, expected) {
t.Errorf("sut.MarshalText() = %v; want %v", actual, expected)
}
}

func TestUniqueIdentifierUnmarshalJSON(t *testing.T) {
input := []byte("01234567-89AB-CDEF-0123-456789ABCDEF")
var u UniqueIdentifier

err := u.UnmarshalJSON(input)
if err != nil {
t.Fatal(err)
}
expected := UniqueIdentifier{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 = UniqueIdentifier{}
var _ sql.Scanner = &UniqueIdentifier{}
var _ driver.Valuer = UniqueIdentifier{}

0 comments on commit 2534789

Please sign in to comment.