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

     1  package logbook
     2  
     3  import (
     4  	"github.com/qri-io/qri/logbook/oplog"
     5  )
     6  
     7  // UserLog is the top-level log representing users that make datasets
     8  type UserLog struct {
     9  	l *oplog.Log
    10  }
    11  
    12  func newUserLog(log *oplog.Log) *UserLog {
    13  	return &UserLog{l: log}
    14  }
    15  
    16  // TODO(dustmop): Consider changing the "Append" methods to type-safe methods that are specific
    17  // to each log level, which accept individual parameters instead of type-unsafe Op values.
    18  
    19  // Append adds an op to the UserLog
    20  func (alog *UserLog) Append(op oplog.Op) {
    21  	if op.Model != UserModel {
    22  		log.Errorf("cannot Append, incorrect model %d for UserLog", op.Model)
    23  		return
    24  	}
    25  
    26  	alog.l.Append(op)
    27  }
    28  
    29  // ProfileID returns the profileID for the user
    30  func (alog *UserLog) ProfileID() string {
    31  	return alog.l.Ops[0].AuthorID
    32  }
    33  
    34  // AddChild adds a child log
    35  // TODO(dustmop): Change this parameter to be a DatasetLog
    36  func (alog *UserLog) AddChild(l *oplog.Log) {
    37  	alog.l.AddChild(l)
    38  }
    39  
    40  // DatasetLog is the mid-level log representing a single dataset
    41  type DatasetLog struct {
    42  	l *oplog.Log
    43  }
    44  
    45  func newDatasetLog(log *oplog.Log) *DatasetLog {
    46  	return &DatasetLog{l: log}
    47  }
    48  
    49  // Append adds an op to the DatasetLog
    50  func (dlog *DatasetLog) Append(op oplog.Op) {
    51  	if op.Model != DatasetModel {
    52  		log.Errorf("cannot Append, incorrect model %d for DatasetLog", op.Model)
    53  		return
    54  	}
    55  	dlog.l.Append(op)
    56  }
    57  
    58  // InitID returns the initID for the dataset
    59  func (dlog *DatasetLog) InitID() string {
    60  	return dlog.l.ID()
    61  }
    62  
    63  // BranchLog is the bottom-level log representing a branch of a dataset history
    64  type BranchLog struct {
    65  	l *oplog.Log
    66  }
    67  
    68  func newBranchLog(l *oplog.Log) *BranchLog {
    69  	blog := &BranchLog{l: l}
    70  	// BranchLog should never have logs underneath it, display error if any are found
    71  	if len(blog.l.Logs) > 0 {
    72  		log.Errorf("invalid branchLog, has %d child Logs", len(blog.l.Logs))
    73  	}
    74  	return blog
    75  }
    76  
    77  // Append adds an op to the BranchLog
    78  func (blog *BranchLog) Append(op oplog.Op) {
    79  	if op.Model != BranchModel && op.Model != CommitModel && op.Model != PushModel && op.Model != RunModel {
    80  		log.Errorf("cannot Append, incorrect model %d for BranchLog", op.Model)
    81  		return
    82  	}
    83  	blog.l.Append(op)
    84  }
    85  
    86  // Size returns the size of the branch
    87  func (blog *BranchLog) Size() int {
    88  	return len(blog.l.Ops)
    89  }
    90  
    91  // Ops returns the raw Op list
    92  func (blog *BranchLog) Ops() []oplog.Op {
    93  	return blog.l.Ops
    94  }