Repository: denisenkom/go-mssqldb
Pull request #520 (2019-09-19 17:23:00)

Support MarshalText in Uniqueidentifier
For issue #519 

codecov[bot]: # [Codecov](https://codecov.io/gh/denisenkom/go-mssqldb/pull/520?src=pr&el=h1) Report
> Merging [#520](https://codecov.io/gh/denisenkom/go-mssqldb/pull/520?src=pr&el=desc) into [master](https://codecov.io/gh/denisenkom/go-mssqldb/commit/aa4949efa320f9f6b95d0a7ec75284efb57819e4?src=pr&el=desc) will **increase** coverage by `0.01%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/denisenkom/go-mssqldb/pull/520/graphs/tree.svg?width=650&token=MoMB2u5avp&height=150&src=pr)](https://codecov.io/gh/denisenkom/go-mssqldb/pull/520?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #520      +/-   ##
==========================================
+ Coverage    68.8%   68.82%   +0.01%     
==========================================
  Files          22       22              
  Lines        5056     5058       +2     
==========================================
+ Hits         3479     3481       +2     
  Misses       1371     1371              
  Partials      206      206
```


| [Impacted Files](https://codecov.io/gh/denisenkom/go-mssqldb/pull/520?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [uniqueidentifier.go](https://codecov.io/gh/denisenkom/go-mssqldb/pull/520/diff?src=pr&el=tree#diff-dW5pcXVlaWRlbnRpZmllci5nbw==) | `84% <100%> (+0.66%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/denisenkom/go-mssqldb/pull/520?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/denisenkom/go-mssqldb/pull/520?src=pr&el=footer). Last update [aa4949e...f3c275f](https://codecov.io/gh/denisenkom/go-mssqldb/pull/520?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/uniqueidentifier.go b/uniqueidentifier.go
index c8ef314..3ad6f86 100644
--- a/uniqueidentifier.go
+++ b/uniqueidentifier.go
@@ -72,3 +72,9 @@ func (u UniqueIdentifier) Value() (driver.Value, error) {
 func (u UniqueIdentifier) String() string {
 	return fmt.Sprintf("%X-%X-%X-%X-%X", u[0:4], u[4:6], u[6:8], u[8:10], u[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 UniqueIdentifier) MarshalText() []byte {
+	return []byte(u.String())
+}


Test patch:
diff --git a/uniqueidentifier_test.go b/uniqueidentifier_test.go
index 2a29133..47a3d88 100644
--- a/uniqueidentifier_test.go
+++ b/uniqueidentifier_test.go
@@ -5,6 +5,7 @@ import (
 	"database/sql"
 	"database/sql/driver"
 	"fmt"
+	"reflect"
 	"testing"
 )
 
@@ -65,6 +66,14 @@ 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) {
+		t.Errorf("sut.MarshalText() = %v; want %v", actual, expected)
+	}
+}
+
 var _ fmt.Stringer = UniqueIdentifier{}
 var _ sql.Scanner = &UniqueIdentifier{}
 var _ driver.Valuer = UniqueIdentifier{}