github.com/hoffie/larasync@v0.0.0-20151025221940-0384d2bddcef/repository/nib/revision.go (about)

     1  package nib
     2  
     3  import (
     4  	"reflect"
     5  
     6  	"github.com/hoffie/larasync/repository/odf"
     7  )
     8  
     9  // Revision is part of an NIB and contains references to
    10  // Metadata information, contents and administration data.
    11  type Revision struct {
    12  	MetadataID   string
    13  	ContentIDs   []string
    14  	UTCTimestamp int64
    15  	DeviceID     string
    16  }
    17  
    18  // newRevisionFromPb returns a new Revision, pre-filled with the
    19  // data from the given protobuf revision.
    20  func newRevisionFromPb(pbRev *odf.Revision) *Revision {
    21  	return &Revision{
    22  		MetadataID:   pbRev.GetMetadataID(),
    23  		ContentIDs:   pbRev.GetContentIDs(),
    24  		UTCTimestamp: pbRev.GetUTCTimestamp(),
    25  		DeviceID:     pbRev.GetDeviceID(),
    26  	}
    27  }
    28  
    29  // toPb converts this Revision to a protobuf Revision.
    30  // This is used by the encoder.
    31  func (r *Revision) toPb() *odf.Revision {
    32  	pb := &odf.Revision{
    33  		MetadataID:   &r.MetadataID,
    34  		ContentIDs:   r.ContentIDs,
    35  		UTCTimestamp: &r.UTCTimestamp,
    36  		DeviceID:     &r.DeviceID,
    37  	}
    38  	return pb
    39  }
    40  
    41  // AddContentID adds the given object id to the list of
    42  // required content ids.
    43  func (r *Revision) AddContentID(id string) error {
    44  	r.ContentIDs = append(r.ContentIDs, id)
    45  	return nil
    46  }
    47  
    48  // HasSameContent returns whether this revision's metadata and
    49  // content ids match the ids of the provided other revision instance.
    50  func (r *Revision) HasSameContent(other *Revision) bool {
    51  	if r.MetadataID != other.MetadataID {
    52  		return false
    53  	}
    54  	if !reflect.DeepEqual(r.ContentIDs, other.ContentIDs) {
    55  		return false
    56  	}
    57  	return true
    58  }
    59  
    60  // IsDeletion returns if the revision marks the item as being deleted.
    61  func (r *Revision) IsDeletion() bool {
    62  	return len(r.ContentIDs) == 0
    63  }
    64  
    65  // Clone returns an exact copy of the given revision.
    66  func (r *Revision) Clone() *Revision {
    67  	return &Revision{
    68  		MetadataID:   r.MetadataID,
    69  		ContentIDs:   r.cloneContentIDs(),
    70  		UTCTimestamp: r.UTCTimestamp,
    71  		DeviceID:     r.DeviceID,
    72  	}
    73  }
    74  
    75  // cloneContentIDs returns a new array populated with the current list
    76  // of contentIDs.
    77  func (r *Revision) cloneContentIDs() []string {
    78  	return append([]string{}, r.ContentIDs...)
    79  }