diff options
author | Michael Muré <batolettre@gmail.com> | 2018-09-30 17:15:54 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2018-09-30 17:15:54 +0200 |
commit | 7f86898ef9a8f9e866835ece3c9824a8edc58036 (patch) | |
tree | 5c5733c9272f3477317935bde6ecb4680276b717 /bug | |
parent | d71bb7dd7632780cf5aad5fda84027fa03a9d0f0 (diff) | |
download | git-bug-7f86898ef9a8f9e866835ece3c9824a8edc58036.tar.gz |
bug: use deditated type for all TimelineItem
Diffstat (limited to 'bug')
-rw-r--r-- | bug/op_add_comment.go | 11 | ||||
-rw-r--r-- | bug/op_create.go | 9 | ||||
-rw-r--r-- | bug/op_create_test.go | 4 | ||||
-rw-r--r-- | bug/op_edit_comment.go | 11 | ||||
-rw-r--r-- | bug/op_edit_comment_test.go | 4 | ||||
-rw-r--r-- | bug/op_label_change.go | 29 | ||||
-rw-r--r-- | bug/op_set_status.go | 28 | ||||
-rw-r--r-- | bug/op_set_title.go | 30 | ||||
-rw-r--r-- | bug/snapshot.go | 7 | ||||
-rw-r--r-- | bug/timeline.go | 36 |
10 files changed, 117 insertions, 52 deletions
diff --git a/bug/op_add_comment.go b/bug/op_add_comment.go index 7f8b8b5b..156a8f71 100644 --- a/bug/op_add_comment.go +++ b/bug/op_add_comment.go @@ -42,7 +42,11 @@ func (op *AddCommentOperation) Apply(snapshot *Snapshot) { panic(err) } - snapshot.Timeline = append(snapshot.Timeline, NewCommentTimelineItem(hash, comment)) + item := &AddCommentTimelineItem{ + CommentTimelineItem: NewCommentTimelineItem(hash, comment), + } + + snapshot.Timeline = append(snapshot.Timeline, item) } func (op *AddCommentOperation) GetFiles() []git.Hash { @@ -73,6 +77,11 @@ func NewAddCommentOp(author Person, unixTime int64, message string, files []git. } } +// CreateTimelineItem replace a AddComment operation in the Timeline and hold its edition history +type AddCommentTimelineItem struct { + CommentTimelineItem +} + // Convenience function to apply the operation func AddComment(b Interface, author Person, unixTime int64, message string) error { return AddCommentWithFiles(b, author, unixTime, message, nil) diff --git a/bug/op_create.go b/bug/op_create.go index 0553137f..200da4ae 100644 --- a/bug/op_create.go +++ b/bug/op_create.go @@ -47,7 +47,9 @@ func (op *CreateOperation) Apply(snapshot *Snapshot) { } snapshot.Timeline = []TimelineItem{ - NewCreateTimelineItem(hash, comment), + &CreateTimelineItem{ + CommentTimelineItem: NewCommentTimelineItem(hash, comment), + }, } } @@ -88,6 +90,11 @@ func NewCreateOp(author Person, unixTime int64, title, message string, files []g } } +// CreateTimelineItem replace a Create operation in the Timeline and hold its edition history +type CreateTimelineItem struct { + CommentTimelineItem +} + // Convenience function to apply the operation func Create(author Person, unixTime int64, title, message string) (*Bug, error) { return CreateWithFiles(author, unixTime, title, message, nil) diff --git a/bug/op_create_test.go b/bug/op_create_test.go index f27f6ee0..d74051ec 100644 --- a/bug/op_create_test.go +++ b/bug/op_create_test.go @@ -36,7 +36,9 @@ func TestCreate(t *testing.T) { Author: rene, CreatedAt: create.Time(), Timeline: []TimelineItem{ - NewCreateTimelineItem(hash, comment), + &CreateTimelineItem{ + CommentTimelineItem: NewCommentTimelineItem(hash, comment), + }, }, } diff --git a/bug/op_edit_comment.go b/bug/op_edit_comment.go index cb4a2216..78976af7 100644 --- a/bug/op_edit_comment.go +++ b/bug/op_edit_comment.go @@ -33,12 +33,7 @@ func (op *EditCommentOperation) Apply(snapshot *Snapshot) { var commentIndex int for i, item := range snapshot.Timeline { - h, err := item.Hash() - - if err != nil { - // Should never happen, we control what goes into the timeline - panic(err) - } + h := item.Hash() if h == op.Target { target = snapshot.Timeline[i] @@ -68,8 +63,8 @@ func (op *EditCommentOperation) Apply(snapshot *Snapshot) { item := target.(*CreateTimelineItem) item.Append(comment) - case *CommentTimelineItem: - item := target.(*CommentTimelineItem) + case *AddCommentTimelineItem: + item := target.(*AddCommentTimelineItem) item.Append(comment) } diff --git a/bug/op_edit_comment_test.go b/bug/op_edit_comment_test.go index 9c32051d..71a7dda2 100644 --- a/bug/op_edit_comment_test.go +++ b/bug/op_edit_comment_test.go @@ -38,7 +38,7 @@ func TestEdit(t *testing.T) { assert.Equal(t, len(snapshot.Timeline), 2) assert.Equal(t, len(snapshot.Timeline[0].(*CreateTimelineItem).History), 2) - assert.Equal(t, len(snapshot.Timeline[1].(*CommentTimelineItem).History), 1) + assert.Equal(t, len(snapshot.Timeline[1].(*AddCommentTimelineItem).History), 1) assert.Equal(t, snapshot.Comments[0].Message, "create edited") assert.Equal(t, snapshot.Comments[1].Message, "comment") @@ -47,7 +47,7 @@ func TestEdit(t *testing.T) { assert.Equal(t, len(snapshot.Timeline), 2) assert.Equal(t, len(snapshot.Timeline[0].(*CreateTimelineItem).History), 2) - assert.Equal(t, len(snapshot.Timeline[1].(*CommentTimelineItem).History), 2) + assert.Equal(t, len(snapshot.Timeline[1].(*AddCommentTimelineItem).History), 2) assert.Equal(t, snapshot.Comments[0].Message, "create edited") assert.Equal(t, snapshot.Comments[1].Message, "comment edited") } diff --git a/bug/op_label_change.go b/bug/op_label_change.go index 5f2dbd6f..b025be81 100644 --- a/bug/op_label_change.go +++ b/bug/op_label_change.go @@ -55,7 +55,22 @@ AddLoop: return string(snapshot.Labels[i]) < string(snapshot.Labels[j]) }) - snapshot.Timeline = append(snapshot.Timeline, op) + hash, err := op.Hash() + if err != nil { + // Should never error unless a programming error happened + // (covered in OpBase.Validate()) + panic(err) + } + + item := &LabelChangeTimelineItem{ + hash: hash, + Author: op.Author, + UnixTime: Timestamp(op.UnixTime), + Added: op.Added, + Removed: op.Removed, + } + + snapshot.Timeline = append(snapshot.Timeline, item) } func (op *LabelChangeOperation) Validate() error { @@ -90,6 +105,18 @@ func NewLabelChangeOperation(author Person, unixTime int64, added, removed []Lab } } +type LabelChangeTimelineItem struct { + hash git.Hash + Author Person + UnixTime Timestamp + Added []Label + Removed []Label +} + +func (l LabelChangeTimelineItem) Hash() git.Hash { + return l.hash +} + // ChangeLabels is a convenience function to apply the operation func ChangeLabels(b Interface, author Person, unixTime int64, add, remove []string) ([]LabelChangeResult, error) { var added, removed []Label diff --git a/bug/op_set_status.go b/bug/op_set_status.go index cdfa25e7..7e9f4314 100644 --- a/bug/op_set_status.go +++ b/bug/op_set_status.go @@ -23,7 +23,22 @@ func (op *SetStatusOperation) Hash() (git.Hash, error) { func (op *SetStatusOperation) Apply(snapshot *Snapshot) { snapshot.Status = op.Status - snapshot.Timeline = append(snapshot.Timeline, op) + + hash, err := op.Hash() + if err != nil { + // Should never error unless a programming error happened + // (covered in OpBase.Validate()) + panic(err) + } + + item := &SetStatusTimelineItem{ + hash: hash, + Author: op.Author, + UnixTime: Timestamp(op.UnixTime), + Status: op.Status, + } + + snapshot.Timeline = append(snapshot.Timeline, item) } func (op *SetStatusOperation) Validate() error { @@ -45,6 +60,17 @@ func NewSetStatusOp(author Person, unixTime int64, status Status) *SetStatusOper } } +type SetStatusTimelineItem struct { + hash git.Hash + Author Person + UnixTime Timestamp + Status Status +} + +func (s SetStatusTimelineItem) Hash() git.Hash { + return s.hash +} + // Convenience function to apply the operation func Open(b Interface, author Person, unixTime int64) error { op := NewSetStatusOp(author, unixTime, OpenStatus) diff --git a/bug/op_set_title.go b/bug/op_set_title.go index 74467ec2..fd964a30 100644 --- a/bug/op_set_title.go +++ b/bug/op_set_title.go @@ -27,7 +27,23 @@ func (op *SetTitleOperation) Hash() (git.Hash, error) { func (op *SetTitleOperation) Apply(snapshot *Snapshot) { snapshot.Title = op.Title - snapshot.Timeline = append(snapshot.Timeline, op) + + hash, err := op.Hash() + if err != nil { + // Should never error unless a programming error happened + // (covered in OpBase.Validate()) + panic(err) + } + + item := &SetTitleTimelineItem{ + hash: hash, + Author: op.Author, + UnixTime: Timestamp(op.UnixTime), + Title: op.Title, + Was: op.Was, + } + + snapshot.Timeline = append(snapshot.Timeline, item) } func (op *SetTitleOperation) Validate() error { @@ -66,6 +82,18 @@ func NewSetTitleOp(author Person, unixTime int64, title string, was string) *Set } } +type SetTitleTimelineItem struct { + hash git.Hash + Author Person + UnixTime Timestamp + Title string + Was string +} + +func (s SetTitleTimelineItem) Hash() git.Hash { + return s.hash +} + // Convenience function to apply the operation func SetTitle(b Interface, author Person, unixTime int64, title string) error { it := NewOperationIterator(b) diff --git a/bug/snapshot.go b/bug/snapshot.go index df39ff46..28a92961 100644 --- a/bug/snapshot.go +++ b/bug/snapshot.go @@ -61,12 +61,7 @@ func (snap *Snapshot) LastEditUnix() int64 { // SearchTimelineItem will search in the timeline for an item matching the given hash func (snap *Snapshot) SearchTimelineItem(hash git.Hash) (TimelineItem, error) { for i := range snap.Timeline { - h, err := snap.Timeline[i].Hash() - if err != nil { - return nil, err - } - - if h == hash { + if snap.Timeline[i].Hash() == hash { return snap.Timeline[i], nil } } diff --git a/bug/timeline.go b/bug/timeline.go index d734e18b..f2feafba 100644 --- a/bug/timeline.go +++ b/bug/timeline.go @@ -6,7 +6,7 @@ import ( type TimelineItem interface { // Hash return the hash of the item - Hash() (git.Hash, error) + Hash() git.Hash } type CommentHistoryStep struct { @@ -14,31 +14,7 @@ type CommentHistoryStep struct { UnixTime Timestamp } -// CreateTimelineItem replace a Create operation in the Timeline and hold its edition history -type CreateTimelineItem struct { - CommentTimelineItem -} - -func NewCreateTimelineItem(hash git.Hash, comment Comment) *CreateTimelineItem { - return &CreateTimelineItem{ - CommentTimelineItem: CommentTimelineItem{ - hash: hash, - Author: comment.Author, - Message: comment.Message, - Files: comment.Files, - CreatedAt: comment.UnixTime, - LastEdit: comment.UnixTime, - History: []CommentHistoryStep{ - { - Message: comment.Message, - UnixTime: comment.UnixTime, - }, - }, - }, - } -} - -// CommentTimelineItem replace a Comment in the Timeline and hold its edition history +// CommentTimelineItem is a TimelineItem that holds a Comment and its edition history type CommentTimelineItem struct { hash git.Hash Author Person @@ -49,8 +25,8 @@ type CommentTimelineItem struct { History []CommentHistoryStep } -func NewCommentTimelineItem(hash git.Hash, comment Comment) *CommentTimelineItem { - return &CommentTimelineItem{ +func NewCommentTimelineItem(hash git.Hash, comment Comment) CommentTimelineItem { + return CommentTimelineItem{ hash: hash, Author: comment.Author, Message: comment.Message, @@ -66,8 +42,8 @@ func NewCommentTimelineItem(hash git.Hash, comment Comment) *CommentTimelineItem } } -func (c *CommentTimelineItem) Hash() (git.Hash, error) { - return c.hash, nil +func (c *CommentTimelineItem) Hash() git.Hash { + return c.hash } // Append will append a new comment in the history and update the other values |