marshal.go 895 B

12345678910111213141516171819202122232425262728293031323334353637
  1. // Copyright 2016 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 "fmt"
  6. // MarshalText implements encoding.TextMarshaler.
  7. func (uuid UUID) MarshalText() ([]byte, error) {
  8. var js [36]byte
  9. encodeHex(js[:], uuid)
  10. return js[:], nil
  11. }
  12. // UnmarshalText implements encoding.TextUnmarshaler.
  13. func (uuid *UUID) UnmarshalText(data []byte) error {
  14. id, err := ParseBytes(data)
  15. if err == nil {
  16. *uuid = id
  17. }
  18. return err
  19. }
  20. // MarshalBinary implements encoding.BinaryMarshaler.
  21. func (uuid UUID) MarshalBinary() ([]byte, error) {
  22. return uuid[:], nil
  23. }
  24. // UnmarshalBinary implements encoding.BinaryUnmarshaler.
  25. func (uuid *UUID) UnmarshalBinary(data []byte) error {
  26. if len(data) != 16 {
  27. return fmt.Errorf("invalid UUID (got %d bytes)", len(data))
  28. }
  29. copy(uuid[:], data)
  30. return nil
  31. }