vid.go 619 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package core
  2. import (
  3. "encoding/hex"
  4. "fmt"
  5. )
  6. // The ID of en entity, in the form of an UUID.
  7. type VID [16]byte
  8. var byteGroups = []int{8, 4, 4, 4, 12}
  9. // TODO: leverage a full functional UUID library
  10. func UUIDToVID(uuid string) (v VID, err error) {
  11. text := []byte(uuid)
  12. if len(text) < 32 {
  13. err = fmt.Errorf("uuid: invalid UUID string: %s", text)
  14. return
  15. }
  16. b := v[:]
  17. for _, byteGroup := range byteGroups {
  18. if text[0] == '-' {
  19. text = text[1:]
  20. }
  21. _, err = hex.Decode(b[:byteGroup/2], text[:byteGroup])
  22. if err != nil {
  23. return
  24. }
  25. text = text[byteGroup:]
  26. b = b[byteGroup/2:]
  27. }
  28. return
  29. }