1
0
mirror of https://github.com/google/uuid.git synced 2025-11-06 08:59:16 +02:00

Add support for driver.Valuer.

It just returns a string, but that should be recognizable by any database that
supports UUIDs, since it's the standard format.
This commit is contained in:
David E. Wheeler
2015-11-24 14:39:54 -08:00
parent 5bc73798cc
commit 0384d88412
2 changed files with 17 additions and 0 deletions

8
sql.go
View File

@@ -5,6 +5,7 @@
package uuid package uuid
import ( import (
"database/sql/driver"
"errors" "errors"
"fmt" "fmt"
) )
@@ -56,3 +57,10 @@ func (uuid *UUID) Scan(src interface{}) error {
return nil return nil
} }
// Value implements sql.Valuer so that UUIDs can be written to databases
// transparently. Currently, UUIDs map map to strings. Please consult
// database-specific driver documentation for matching types.
func (uuid *UUID) Value() (driver.Value, error) {
return uuid.String(), nil
}

View File

@@ -56,3 +56,12 @@ func TestScan(t *testing.T) {
t.Error("attempting to parse an invalid byte UUID returned an incorrect error message") t.Error("attempting to parse an invalid byte UUID returned an incorrect error message")
} }
} }
func TestValue(t *testing.T) {
stringTest := "f47ac10b-58cc-0372-8567-0e02b2c3d479"
uuid := Parse(stringTest)
val, _ := uuid.Value()
if val != stringTest {
t.Error("Value() did not return expected string")
}
}