From b607336be90d57314e634eefdf9f471b85b35fba Mon Sep 17 00:00:00 2001 From: Musab Nazir Date: Tue, 11 Jul 2023 12:27:42 -0400 Subject: [PATCH] Fixes MarshalText and adds UnmarshalJSON interface for UniqueIdentifier (#126) * Added MarshalText() and UnmarshalJSON interfaces - The existing MarshalText() for the UniqueIdentifier type had a bad signature. It omitted returning an error that the interface expects. - Added UnmarshalJSON() interface to the UniqueIdentifier type with a test as well * use strings.Replace instead of ReplaceAll --- uniqueidentifier.go | 23 +++++++++++++++++++++-- uniqueidentifier_test.go | 17 ++++++++++++++++- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/uniqueidentifier.go b/uniqueidentifier.go index 3ad6f863..da9cc03f 100644 --- a/uniqueidentifier.go +++ b/uniqueidentifier.go @@ -5,6 +5,7 @@ import ( "encoding/hex" "errors" "fmt" + "strings" ) type UniqueIdentifier [16]byte @@ -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 } diff --git a/uniqueidentifier_test.go b/uniqueidentifier_test.go index 47a3d883..23070d03 100644 --- a/uniqueidentifier_test.go +++ b/uniqueidentifier_test.go @@ -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{}