1
0
mirror of https://github.com/google/uuid.git synced 2025-02-20 12:33:21 +02:00

Merge pull request #15 from jboverfelt/sql-byte-scan

Attempt to scan []byte uuids larger than 16 bytes. Fixes #12
This commit is contained in:
pborman 2015-12-15 07:05:54 -08:00
commit dee7705ef7
2 changed files with 18 additions and 5 deletions

18
sql.go
View File

@ -24,14 +24,22 @@ func (uuid *UUID) Scan(src interface{}) error {
*uuid = parsed *uuid = parsed
case []byte: case []byte:
// assumes a simple slice of bytes, just check validity and store b := src.([]byte)
u := UUID(src.([]byte))
if u.Variant() == Invalid { // assumes a simple slice of bytes if 16 bytes
return errors.New("Scan: invalid UUID format") // otherwise attempts to parse
if len(b) == 16 {
*uuid = UUID(b)
} else {
u := Parse(string(b))
if u == nil {
return errors.New("Scan: invalid UUID format")
}
*uuid = u
} }
*uuid = u
default: default:
return fmt.Errorf("Scan: unable to scan type %T into UUID", src) return fmt.Errorf("Scan: unable to scan type %T into UUID", src)
} }

View File

@ -22,6 +22,11 @@ func TestScan(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
err = (&uuid).Scan([]byte(stringTest))
if err != nil {
t.Fatal(err)
}
err = (&uuid).Scan(byteTest) err = (&uuid).Scan(byteTest)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)