-
Notifications
You must be signed in to change notification settings - Fork 0
/
tour.go
90 lines (71 loc) · 1.68 KB
/
tour.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package tour
import (
"strconv"
"strings"
u "github.com/ipfs/go-ipfs/util"
)
var log = u.Logger("tour")
// ID is a string identifier for topics
type ID string
// LessThan returns whether this ID is sorted earlier than another.
func (i ID) LessThan(o ID) bool {
return compareDottedInts(string(i), string(o))
}
// IDSlice implements the sort interface for ID slices.
type IDSlice []ID
func (a IDSlice) Len() int { return len(a) }
func (a IDSlice) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a IDSlice) Less(i, j int) bool { return a[i].LessThan(a[j]) }
// Topic is a type of objects that structures a tour topic.
type Topic struct {
ID ID
Content
}
type Content struct {
Title string
Text string
}
// Topics is a sorted list of topic IDs
var IDs []ID
// Topics contains a mapping of Tour Topic ID to Topic
var Topics = map[ID]Topic{}
// NextTopic returns the next in-order topic
func NextTopic(topic ID) ID {
for _, id := range IDs {
if topic.LessThan(id) {
return id
}
}
return topic // last one, it seems.
}
// TopicID returns a valid tour topic ID from given string
func TopicID(t string) ID {
if t == "" { // if empty, use first ID
return IDs[0]
}
return ID(t)
}
func compareDottedInts(i, o string) bool {
is := strings.Split(i, ".")
os := strings.Split(o, ".")
for n, vis := range is {
if n >= len(os) {
return false // os is smaller.
}
vos := os[n]
ivis, err1 := strconv.Atoi(vis)
ivos, err2 := strconv.Atoi(vos)
if err1 != nil || err2 != nil {
log.Debug(err1)
log.Debug(err2)
panic("tour ID LessThan: not an int")
}
if ivis < ivos {
return true
}
if ivis > ivos {
return false
}
}
return len(os) > len(is)
}