github.com/lulzWill/go-agent@v2.1.2+incompatible/internal/cat/id.go (about)

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