github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/dsref/ref.go (about)

     1  package dsref
     2  
     3  // Ref is a reference to a dataset
     4  type Ref struct {
     5  	// InitID is the canonical identifer for a dataset history
     6  	InitID string `json:"initID,omitempty"`
     7  	// Username of dataset owner
     8  	Username string `json:"username,omitempty"`
     9  	// ProfileID of dataset owner
    10  	// deprecated - avoid using this field, we're working towards removing it
    11  	// generally profile IDs should come from request scopes, or be fetched from
    12  	// stores of identity info (profile.Store)
    13  	ProfileID string `json:"profileID,omitempty"`
    14  	// Unique name reference for this dataset
    15  	Name string `json:"name,omitempty"`
    16  	// Content-addressed path for this dataset
    17  	Path string `json:"path,omitempty"`
    18  }
    19  
    20  // Alias returns the alias components of a Ref as a string
    21  func (r Ref) Alias() (s string) {
    22  	return r.Human()
    23  }
    24  
    25  // Human returns the human-friendly representation of the reference
    26  // example: some_user/my_dataset
    27  func (r Ref) Human() string {
    28  	s := r.Username
    29  	if r.Name != "" {
    30  		s += "/" + r.Name
    31  	}
    32  	return s
    33  }
    34  
    35  // String implements the Stringer interface for Ref
    36  func (r Ref) String() (s string) {
    37  	s = r.Alias()
    38  	if r.InitID != "" || r.Path != "" {
    39  		s += "@"
    40  	}
    41  	if r.InitID != "" {
    42  		s += r.InitID
    43  	}
    44  	if r.Path != "" {
    45  		s += r.Path
    46  	}
    47  	return s
    48  }
    49  
    50  // LegacyProfileIDString serializes a ref in the form
    51  //   Username/Name@ProfileID/Path
    52  // Deprecated - don't add callers, use String or raw ref fields instead
    53  func (r Ref) LegacyProfileIDString() (s string) {
    54  	s = r.Alias()
    55  	if r.InitID != "" || r.Path != "" {
    56  		s += "@"
    57  	}
    58  	if r.ProfileID != "" {
    59  		s += r.ProfileID
    60  	}
    61  	if r.Path != "" {
    62  		s += r.Path
    63  	}
    64  	return s
    65  }
    66  
    67  // IsEmpty returns whether the reference is empty
    68  func (r Ref) IsEmpty() bool {
    69  	return r.InitID == "" && r.Username == "" && r.ProfileID == "" && r.Name == "" && r.Path == ""
    70  }
    71  
    72  // IsPeerRef returns true if only Peername is set
    73  func (r Ref) IsPeerRef() bool {
    74  	return (r.Username != "" || r.ProfileID != "") && r.Name == "" && r.Path == "" && r.InitID == ""
    75  }
    76  
    77  // Complete returns true if all fields are populated
    78  func (r Ref) Complete() bool {
    79  	return r.InitID != "" && r.Username != "" && r.ProfileID != "" && r.Name != "" && r.Path != ""
    80  }
    81  
    82  // Equals returns whether the reference equals another
    83  func (r Ref) Equals(t Ref) bool {
    84  	return r.InitID == t.InitID &&
    85  		r.Username == t.Username &&
    86  		r.ProfileID == t.ProfileID &&
    87  		r.Name == t.Name &&
    88  		r.Path == t.Path
    89  }
    90  
    91  // Copy duplicates a reference
    92  func (r Ref) Copy() Ref {
    93  	return Ref{
    94  		InitID:    r.InitID,
    95  		Username:  r.Username,
    96  		ProfileID: r.ProfileID,
    97  		Name:      r.Name,
    98  		Path:      r.Path,
    99  	}
   100  }
   101  
   102  // VersionInfo creates a new sparse VersionInfo from a reference
   103  func (r Ref) VersionInfo() VersionInfo {
   104  	return VersionInfo{
   105  		InitID:    r.InitID,
   106  		Username:  r.Username,
   107  		ProfileID: r.ProfileID,
   108  		Name:      r.Name,
   109  		Path:      r.Path,
   110  	}
   111  }