github.com/git-amp/amp-sdk-go@v0.7.5/amp/uid.go (about)

     1  package amp
     2  
     3  import "github.com/google/uuid"
     4  
     5  type UID [16]byte
     6  
     7  var (
     8  	NilUID         = UID{}
     9  	DevicePlanet   = FormUID(0, 0x01)
    10  	HostPlanet     = FormUID(0, 0x02)
    11  	AppHomePlanet  = FormUID(0, 0x03)
    12  	UserHomePlanet = FormUID(0, 0x04)
    13  )
    14  
    15  // Forms an amp.UID explicitly from two uint64 values.
    16  func FormUID(n0, n1 uint64) UID {
    17  	uid := UID{}
    18  	shift := uint(56)
    19  	for i := 0; i < 8; i++ {
    20  		uid[i+0] = byte(n0 >> shift)
    21  		uid[i+8] = byte(n1 >> shift)
    22  		shift -= 8
    23  	}
    24  	return uid
    25  }
    26  
    27  // ParseUID decodes s into a UID or returns an error.  Accepted forms:
    28  //   - xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
    29  //   - urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
    30  //   - {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
    31  //   - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.
    32  func ParseUID(s string) (UID, error) {
    33  	uid, err := uuid.Parse(s)
    34  	return UID(uid), err
    35  }
    36  
    37  // MustParseUID decodes s into a UID or panics -- see ParseUID().
    38  func MustParseUID(s string) UID {
    39  	uid := uuid.MustParse(s)
    40  	return UID(uid)
    41  }
    42  
    43  // String returns the string form of uid: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx or "" if uuid is zero.
    44  func (uid UID) String() string {
    45  	return uuid.UUID(uid).String()
    46  }