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

     1  package reporef
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/qri-io/dataset"
     7  	"github.com/qri-io/qri/profile"
     8  )
     9  
    10  // DatasetRef encapsulates a reference to a dataset. This needs to exist to bind
    11  // ways of referring to a dataset to a dataset itself, as datasets can't easily
    12  // contain their own hash information, and names are unique on a per-repository
    13  // basis.
    14  //
    15  // Deprecated: DatasetRef will be removed in future versions of Qri, use
    16  // dsref.Ref and this package's Info struct instead
    17  type DatasetRef struct {
    18  	// Peername of dataset owner
    19  	Peername string `json:"peername,omitempty"`
    20  	// ProfileID of dataset owner
    21  	ProfileID profile.ID `json:"profileID,omitempty"`
    22  	// Unique name reference for this dataset
    23  	Name string `json:"name,omitempty"`
    24  	// Content-addressed path for this dataset
    25  	Path string `json:"path,omitempty"`
    26  	// FSIPath is this dataset's link to the local filesystem if one exists
    27  	FSIPath string `json:"fsiPath,omitempty"`
    28  	// Dataset is a pointer to the dataset being referenced
    29  	Dataset *dataset.Dataset `json:"dataset,omitempty"`
    30  	// Published indicates whether this reference is listed as an available dataset
    31  	Published bool `json:"published"`
    32  	// If true, this reference doesn't exist locally. Only makes sense if path is set, as this
    33  	// flag refers to specific versions, not to entire dataset histories.
    34  	Foreign bool `json:"foreign,omitempty"`
    35  }
    36  
    37  // String implements the Stringer interface for DatasetRef
    38  func (r DatasetRef) String() (s string) {
    39  	s = r.AliasString()
    40  	if r.Path != "" {
    41  		s += "@" + r.Path
    42  	}
    43  	return
    44  }
    45  
    46  // DebugString returns a string that describes every field in the reference, useful for tests
    47  func (r DatasetRef) DebugString() string {
    48  	builder := strings.Builder{}
    49  	builder.WriteString("{peername:")
    50  	builder.WriteString(r.Peername)
    51  	builder.WriteString(",profileID:")
    52  	builder.WriteString(r.ProfileID.Encode())
    53  	builder.WriteString(",name:")
    54  	builder.WriteString(r.Name)
    55  	if r.Path != "" {
    56  		builder.WriteString(",path:")
    57  		builder.WriteString(r.Path)
    58  	}
    59  	if r.FSIPath != "" {
    60  		builder.WriteString(",fsiPath:")
    61  		builder.WriteString(r.FSIPath)
    62  	}
    63  	if r.Published {
    64  		builder.WriteString(",published:true")
    65  	}
    66  	if r.Foreign {
    67  		builder.WriteString(",foreign:true")
    68  	}
    69  	builder.WriteString("}")
    70  	return builder.String()
    71  }
    72  
    73  // Absolute implements the same thing as String(), but append ProfileID if it exist
    74  func (r DatasetRef) Absolute() (s string) {
    75  	s = r.AliasString()
    76  	if r.ProfileID.Encode() != "" || r.Path != "" {
    77  		s += "@"
    78  	}
    79  	if r.ProfileID.Encode() != "" {
    80  		s += r.ProfileID.Encode()
    81  	}
    82  	if r.Path != "" {
    83  		s += r.Path
    84  	}
    85  	return
    86  }
    87  
    88  // AliasString returns the alias components of a DatasetRef as a string
    89  func (r DatasetRef) AliasString() (s string) {
    90  	s = r.Peername
    91  	if r.Name != "" {
    92  		s += "/" + r.Name
    93  	}
    94  	return
    95  }
    96  
    97  // Complete returns true if a dataset has Peername, Name, ProfileID and Path
    98  // properties set
    99  func (r DatasetRef) Complete() bool {
   100  	return r.Peername != "" && r.ProfileID != "" && r.Name != "" && r.Path != ""
   101  }
   102  
   103  // Match checks returns true if Peername and Name are equal,
   104  // and/or path is equal
   105  func (r DatasetRef) Match(b DatasetRef) bool {
   106  	return (r.Path != "" && b.Path != "" && r.Path == b.Path) || (r.ProfileID == b.ProfileID || r.Peername == b.Peername) && r.Name == b.Name
   107  }
   108  
   109  // Equal returns true only if Peername Name and Path are equal
   110  func (r DatasetRef) Equal(b DatasetRef) bool {
   111  	return r.Peername == b.Peername && r.ProfileID == b.ProfileID && r.Name == b.Name && r.Path == b.Path
   112  }
   113  
   114  // IsPeerRef returns true if only Peername is set
   115  func (r DatasetRef) IsPeerRef() bool {
   116  	return (r.Peername != "" || r.ProfileID != "") && r.Name == "" && r.Path == "" && r.Dataset == nil
   117  }
   118  
   119  // IsEmpty returns true if none of it's fields are set
   120  func (r DatasetRef) IsEmpty() bool {
   121  	return r.Equal(DatasetRef{})
   122  }