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

     1  package reporef
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/qri-io/qri/dsref"
     7  	"github.com/qri-io/qri/profile"
     8  )
     9  
    10  // ConvertToVersionInfo converts an old style DatasetRef to the newly preferred dsref.VersionInfo
    11  func ConvertToVersionInfo(r *DatasetRef) dsref.VersionInfo {
    12  	build := dsref.VersionInfo{
    13  		Username:  r.Peername,
    14  		ProfileID: r.ProfileID.Encode(),
    15  		Name:      r.Name,
    16  		Path:      r.Path,
    17  	}
    18  	ds := r.Dataset
    19  	// NOTE: InitID is not set when converting from reporef.Dataset
    20  	build.Published = r.Published
    21  	build.Foreign = r.Foreign
    22  	if ds != nil && ds.Meta != nil {
    23  		if ds.Meta.Title != "" {
    24  			build.MetaTitle = ds.Meta.Title
    25  		}
    26  		if ds.Meta.Theme != nil {
    27  			build.ThemeList = strings.Join(ds.Meta.Theme, ",")
    28  		}
    29  	}
    30  	if ds != nil && ds.Structure != nil {
    31  		build.BodySize = ds.Structure.Length
    32  		build.BodyRows = ds.Structure.Entries
    33  		build.BodyFormat = ds.Structure.Format
    34  		build.NumErrors = ds.Structure.ErrCount
    35  	}
    36  	if ds != nil && ds.Commit != nil {
    37  		build.CommitTime = ds.Commit.Timestamp
    38  		build.CommitTitle = ds.Commit.Title
    39  		build.CommitMessage = ds.Commit.Message
    40  	}
    41  	if ds != nil {
    42  		build.CommitCount = ds.NumVersions
    43  	}
    44  	return build
    45  }
    46  
    47  // ConvertToDsref is a shim function to transition from a reporef.DatasetRef to a
    48  // dsref.Ref while we experiment with dsref as the home of name parsing
    49  func ConvertToDsref(ref DatasetRef) dsref.Ref {
    50  	return dsref.Ref{
    51  		Username:  ref.Peername,
    52  		Name:      ref.Name,
    53  		ProfileID: ref.ProfileID.Encode(),
    54  		Path:      ref.Path,
    55  	}
    56  }
    57  
    58  // RefFromDsref creates a datasetRef from a dsref.Ref. The profileID field will be
    59  // an empty string if the input profileID is blank or otherwise cannot be decoded.
    60  func RefFromDsref(r dsref.Ref) DatasetRef {
    61  	return DatasetRef{
    62  		Peername:  r.Username,
    63  		ProfileID: profile.IDB58DecodeOrEmpty(r.ProfileID),
    64  		Name:      r.Name,
    65  		Path:      r.Path,
    66  	}
    67  }
    68  
    69  // RefFromVersionInfo creates a reference from a dsref.VersionInfo, for
    70  // backwards compatibility
    71  func RefFromVersionInfo(vi *dsref.VersionInfo) DatasetRef {
    72  	return DatasetRef{
    73  		Peername:  vi.Username,
    74  		ProfileID: profile.IDB58DecodeOrEmpty(vi.ProfileID),
    75  		Name:      vi.Name,
    76  		Path:      vi.Path,
    77  		Published: vi.Published,
    78  		Foreign:   vi.Foreign,
    79  	}
    80  }