Skip to content
This repository has been archived by the owner on May 9, 2020. It is now read-only.

Commit

Permalink
Fix Scan postgres uuid
Browse files Browse the repository at this point in the history
  • Loading branch information
vporoshok committed Sep 9, 2016
1 parent b505f2c commit 572c2fc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
6 changes: 5 additions & 1 deletion types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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")
}
Expand Down

0 comments on commit 572c2fc

Please sign in to comment.