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:
8
sql.go
8
sql.go
@@ -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
|
||||||
|
}
|
||||||
|
|||||||
@@ -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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user