-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdisplay.go
60 lines (52 loc) · 1.3 KB
/
display.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package issuesapp
import (
"fmt"
"time"
"github.com/shurcooL/issues"
)
// issueItem represents an issue item for display purposes.
type issueItem struct {
// IssueItem can be one of issues.Comment, issues.Event.
IssueItem interface{}
}
func (i issueItem) TemplateName() string {
switch i.IssueItem.(type) {
case issues.Comment:
return "comment"
case issues.Event:
return "event"
default:
panic(fmt.Errorf("unknown item type %T", i.IssueItem))
}
}
func (i issueItem) CreatedAt() time.Time {
switch i := i.IssueItem.(type) {
case issues.Comment:
return i.CreatedAt
case issues.Event:
return i.CreatedAt
default:
panic(fmt.Errorf("unknown item type %T", i))
}
}
func (i issueItem) ID() uint64 {
switch i := i.IssueItem.(type) {
case issues.Comment:
return i.ID
case issues.Event:
return i.ID
default:
panic(fmt.Errorf("unknown item type %T", i))
}
}
// byCreatedAtID implements sort.Interface.
type byCreatedAtID []issueItem
func (s byCreatedAtID) Len() int { return len(s) }
func (s byCreatedAtID) Less(i, j int) bool {
if s[i].CreatedAt().Equal(s[j].CreatedAt()) {
// If CreatedAt time is equal, fall back to ID as a tiebreaker.
return s[i].ID() < s[j].ID()
}
return s[i].CreatedAt().Before(s[j].CreatedAt())
}
func (s byCreatedAtID) Swap(i, j int) { s[i], s[j] = s[j], s[i] }