Skip to content

Commit cc5e254

Browse files
committed
Use SearchComment instead of dedicated iteration
1 parent 64c5ebe commit cc5e254

2 files changed

Lines changed: 8 additions & 11 deletions

File tree

bug/op_edit_comment.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,10 @@ func (op *EditCommentOperation) Apply(snapshot *Snapshot) {
7171
snapshot.addActor(op.Author_)
7272

7373
// Updating the corresponding comment
74-
75-
//TODO use snapshot.SearchComment instead of iteration
76-
//TODO put *editComment* in snapshot or add *edit* to Comment interface?
77-
for i := range snapshot.Comments() {
78-
if snapshot.Comments()[i].Id() == commentId {
79-
snapshot.Comments()[i].Message = op.Message
80-
snapshot.Comments()[i].Files = op.Files
81-
break
82-
}
74+
editedComment, err := snapshot.SearchComment(commentId)
75+
if err == nil {
76+
editedComment.Message = op.Message
77+
editedComment.Files = op.Files
8378
}
8479
}
8580

bug/snapshot.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,11 @@ func (snap *Snapshot) SearchTimelineItem(id entity.Id) (TimelineItem, error) {
102102

103103
// SearchComment will search for a comment matching the given hash
104104
func (snap *Snapshot) SearchComment(id entity.Id) (*Comment, error) {
105-
for _, c := range snap.comments {
105+
for i, c := range snap.comments {
106106
if c.id == id {
107-
return &c, nil
107+
// NOTE don't return c or &c! Otherwise callers cannot change the
108+
// returned comment. As c is a copy the found comment!
109+
return &snap.comments[i], nil
108110
}
109111
}
110112

0 commit comments

Comments
 (0)