sql.go 989 B

12345678910111213141516171819202122232425262728293031323334353637383940
  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.
  4. package uuid
  5. import (
  6. "errors"
  7. "fmt"
  8. )
  9. // Scan implements sql.Scanner so UUIDs can be read from databases transparently
  10. // Currently, database types that map to string and []byte are supported. Please
  11. // consult database-specific driver documentation for matching types.
  12. func (uuid *UUID) Scan(src interface{}) error {
  13. switch src.(type) {
  14. case string:
  15. // see uuid.Parse for required string format
  16. parsed := Parse(src.(string))
  17. if parsed == nil {
  18. return errors.New("Scan: invalid UUID format")
  19. }
  20. *uuid = parsed
  21. case []byte:
  22. // assumes a simple slice of bytes, just check validity and store
  23. u := UUID(src.([]byte))
  24. if u.Variant() == Invalid {
  25. return errors.New("Scan: invalid UUID format")
  26. }
  27. *uuid = u
  28. default:
  29. return fmt.Errorf("Scan: unable to scan type %T into UUID", src)
  30. }
  31. return nil
  32. }