id.go 898 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package wire
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strings"
  6. )
  7. type WireIDJSON struct {
  8. Name string `json:"name,omitempty"`
  9. Domain string `json:"domain,omitempty"`
  10. ClientID string `json:"client-id,omitempty"`
  11. Handle string `json:"handle,omitempty"`
  12. }
  13. func ParseID(data []byte) (wireID WireIDJSON, err error) {
  14. err = json.Unmarshal(data, &wireID)
  15. return
  16. }
  17. type ClientID struct {
  18. Username string
  19. DeviceID string
  20. Domain string
  21. }
  22. func ParseClientID(clientID string) (ClientID, error) {
  23. at := strings.SplitN(clientID, "@", 2)
  24. if len(at) != 2 {
  25. return ClientID{}, fmt.Errorf("could not match client ID format: %s", clientID)
  26. }
  27. comp := at[0]
  28. slash := strings.SplitN(comp, "/", 2)
  29. if len(slash) != 2 {
  30. return ClientID{}, fmt.Errorf("could not match client ID format: %s", clientID)
  31. }
  32. return ClientID{
  33. Username: slash[0],
  34. DeviceID: slash[1],
  35. Domain: at[1],
  36. }, nil
  37. }