-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtodo.go
More file actions
62 lines (51 loc) · 1.04 KB
/
Copy pathtodo.go
File metadata and controls
62 lines (51 loc) · 1.04 KB
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
61
62
package main
import (
"fmt"
"time"
)
type Status int
const (
InProgress Status = iota
New
Done
)
func (s Status) String() string {
names := []string{
"In Progress",
"New",
"Finished",
}
return names[s]
}
type Todo struct {
Message string `json:message`
Timestamp time.Time `json:timestamp`
TodoStatus Status `json:status`
}
func NewTodo(message string, timestamp time.Time) *Todo {
return &Todo{Message: message, Timestamp: timestamp, TodoStatus: New}
}
func (todo Todo) String() string {
return fmt.Sprintf("\t%s\t%s",
todo.Message,
todo.TodoStatus,
)
}
// The type def and the follwing three funcs are for sorting todos
// by timestamp. Latest todo at the top.
type TodoByTimestamp []*Todo
func (t TodoByTimestamp) Len() int {
return len(t)
}
func (t TodoByTimestamp) Less(i, j int) bool {
if t[j].TodoStatus > t[i].TodoStatus {
return true
}
if t[j].TodoStatus < t[i].TodoStatus {
return false
}
return t[j].Timestamp.Before(t[i].Timestamp)
}
func (t TodoByTimestamp) Swap(i, j int) {
t[i], t[j] = t[j], t[i]
}