From 572c2fc5a96f052826a1c767b1ea3d85aba774a0 Mon Sep 17 00:00:00 2001 From: Bastrykov Evgeniy Date: Fri, 9 Sep 2016 10:35:22 +0400 Subject: [PATCH] Fix Scan postgres uuid --- types.go | 6 +++++- types_test.go | 8 +++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/types.go b/types.go index 308c43a..578d2e6 100644 --- a/types.go +++ b/types.go @@ -128,7 +128,11 @@ func (o *Uuid) Scan(pSrc interface{}) error { return o.UnmarshalText([]byte(src)) case []byte: - return o.UnmarshalBinary(src) + if len(src) == length { + return o.UnmarshalBinary(src) + } else { + return o.UnmarshalText(src) + } default: return fmt.Errorf("uuid.Uuid.Scan: cannot scan type %T into Uuid", pSrc) diff --git a/types_test.go b/types_test.go index 963e78f..0f4052f 100644 --- a/types_test.go +++ b/types_test.go @@ -2,8 +2,9 @@ package uuid import ( "errors" - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestUuid_Bytes(t *testing.T) { @@ -198,6 +199,11 @@ func TestUuid_Scan(t *testing.T) { assert.NoError(t, err, "When nil there should be no error") assert.Equal(t, NameSpaceDNS.String(), v.String(), "Values should be the same") + var v3 Uuid + err = v3.Scan([]byte(NameSpaceDNS.String())) + assert.NoError(t, err, "When []byte represents string should be no error") + assert.Equal(t, NameSpaceDNS.String(), v3.String(), "Values should be the same") + err = v.Scan(22) assert.Error(t, err, "When wrong type should error") }