bug: apply an operation with a pointer to the snapshot

Michael Muré created

Change summary

bug/bug.go             | 2 +-
bug/op_add_comment.go  | 4 +---
bug/op_create.go       | 3 +--
bug/op_create_test.go  | 2 +-
bug/op_label_change.go | 4 +---
bug/op_set_status.go   | 4 +---
bug/op_set_title.go    | 4 +---
bug/operation.go       | 2 +-
bug/with_snapshot.go   | 6 ++----
9 files changed, 10 insertions(+), 21 deletions(-)

Detailed changes

bug/bug.go 🔗

@@ -641,7 +641,7 @@ func (bug *Bug) Compile() Snapshot {
 
 	for it.Next() {
 		op := it.Value()
-		snap = op.Apply(snap)
+		op.Apply(&snap)
 		snap.Operations = append(snap.Operations, op)
 	}
 

bug/op_add_comment.go 🔗

@@ -26,7 +26,7 @@ func (op AddCommentOperation) Hash() (git.Hash, error) {
 	return hashOperation(op)
 }
 
-func (op AddCommentOperation) Apply(snapshot Snapshot) Snapshot {
+func (op AddCommentOperation) Apply(snapshot *Snapshot) {
 	comment := Comment{
 		Message:  op.Message,
 		Author:   op.Author,
@@ -35,8 +35,6 @@ func (op AddCommentOperation) Apply(snapshot Snapshot) Snapshot {
 	}
 
 	snapshot.Comments = append(snapshot.Comments, comment)
-
-	return snapshot
 }
 
 func (op AddCommentOperation) GetFiles() []git.Hash {

bug/op_create.go 🔗

@@ -27,7 +27,7 @@ func (op CreateOperation) Hash() (git.Hash, error) {
 	return hashOperation(op)
 }
 
-func (op CreateOperation) Apply(snapshot Snapshot) Snapshot {
+func (op CreateOperation) Apply(snapshot *Snapshot) {
 	snapshot.Title = op.Title
 	snapshot.Comments = []Comment{
 		{
@@ -38,7 +38,6 @@ func (op CreateOperation) Apply(snapshot Snapshot) Snapshot {
 	}
 	snapshot.Author = op.Author
 	snapshot.CreatedAt = op.Time()
-	return snapshot
 }
 
 func (op CreateOperation) GetFiles() []git.Hash {

bug/op_create_test.go 🔗

@@ -18,7 +18,7 @@ func TestCreate(t *testing.T) {
 
 	create := NewCreateOp(rene, unix, "title", "message", nil)
 
-	snapshot = create.Apply(snapshot)
+	create.Apply(&snapshot)
 
 	expected := Snapshot{
 		Title: "title",

bug/op_label_change.go 🔗

@@ -26,7 +26,7 @@ func (op LabelChangeOperation) Hash() (git.Hash, error) {
 }
 
 // Apply apply the operation
-func (op LabelChangeOperation) Apply(snapshot Snapshot) Snapshot {
+func (op LabelChangeOperation) Apply(snapshot *Snapshot) {
 	// Add in the set
 AddLoop:
 	for _, added := range op.Added {
@@ -54,8 +54,6 @@ AddLoop:
 	sort.Slice(snapshot.Labels, func(i, j int) bool {
 		return string(snapshot.Labels[i]) < string(snapshot.Labels[j])
 	})
-
-	return snapshot
 }
 
 func (op LabelChangeOperation) Validate() error {

bug/op_set_status.go 🔗

@@ -22,10 +22,8 @@ func (op SetStatusOperation) Hash() (git.Hash, error) {
 	return hashOperation(op)
 }
 
-func (op SetStatusOperation) Apply(snapshot Snapshot) Snapshot {
+func (op SetStatusOperation) Apply(snapshot *Snapshot) {
 	snapshot.Status = op.Status
-
-	return snapshot
 }
 
 func (op SetStatusOperation) Validate() error {

bug/op_set_title.go 🔗

@@ -26,10 +26,8 @@ func (op SetTitleOperation) Hash() (git.Hash, error) {
 	return hashOperation(op)
 }
 
-func (op SetTitleOperation) Apply(snapshot Snapshot) Snapshot {
+func (op SetTitleOperation) Apply(snapshot *Snapshot) {
 	snapshot.Title = op.Title
-
-	return snapshot
 }
 
 func (op SetTitleOperation) Validate() error {

bug/operation.go 🔗

@@ -35,7 +35,7 @@ type Operation interface {
 	// GetFiles return the files needed by this operation
 	GetFiles() []git.Hash
 	// Apply the operation to a Snapshot to create the final state
-	Apply(snapshot Snapshot) Snapshot
+	Apply(snapshot *Snapshot)
 	// Validate check if the operation is valid (ex: a title is a single line)
 	Validate() error
 	// SetMetadata store arbitrary metadata about the operation

bug/with_snapshot.go 🔗

@@ -27,10 +27,8 @@ func (b *WithSnapshot) Append(op Operation) {
 		return
 	}
 
-	snap := op.Apply(*b.snap)
-	snap.Operations = append(snap.Operations, op)
-
-	b.snap = &snap
+	op.Apply(b.snap)
+	b.snap.Operations = append(b.snap.Operations, op)
 }
 
 // Commit intercept Bug.Commit() to update the snapshot efficiently