id.go 810 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package cat
  2. import (
  3. "errors"
  4. "strconv"
  5. "strings"
  6. )
  7. // IDHeader represents a decoded cross process ID header (generally encoded as
  8. // a string in the form ACCOUNT#BLOB).
  9. type IDHeader struct {
  10. AccountID int
  11. Blob string
  12. }
  13. var (
  14. errInvalidAccountID = errors.New("invalid account ID")
  15. )
  16. // NewIDHeader parses the given decoded ID header and creates an IDHeader
  17. // representing it.
  18. func NewIDHeader(in []byte) (*IDHeader, error) {
  19. parts := strings.Split(string(in), "#")
  20. if len(parts) != 2 {
  21. return nil, errUnexpectedArraySize{
  22. label: "unexpected number of ID elements",
  23. expected: 2,
  24. actual: len(parts),
  25. }
  26. }
  27. account, err := strconv.Atoi(parts[0])
  28. if err != nil {
  29. return nil, errInvalidAccountID
  30. }
  31. return &IDHeader{
  32. AccountID: account,
  33. Blob: parts[1],
  34. }, nil
  35. }