1 // Copyright 2015 Google Inc. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
13 // Scan implements sql.Scanner so UUIDs can be read from databases transparently
14 // Currently, database types that map to string and []byte are supported. Please
15 // consult database-specific driver documentation for matching types.
16 func (uuid *UUID) Scan(src interface{}) error {
19 // if an empty UUID comes from a table, we return a null UUID
20 if src.(string) == "" {
24 // see uuid.Parse for required string format
25 parsed := Parse(src.(string))
28 return errors.New("Scan: invalid UUID format")
35 // if an empty UUID comes from a table, we return a null UUID
40 // assumes a simple slice of bytes if 16 bytes
41 // otherwise attempts to parse
43 parsed := make([]byte, 16)
50 return errors.New("Scan: invalid UUID format")
57 return fmt.Errorf("Scan: unable to scan type %T into UUID", src)
63 // Value implements sql.Valuer so that UUIDs can be written to databases
64 // transparently. Currently, UUIDs map to strings. Please consult
65 // database-specific driver documentation for matching types.
66 func (uuid UUID) Value() (driver.Value, error) {
67 return uuid.String(), nil