-
Notifications
You must be signed in to change notification settings - Fork 0
/
voyager.go
206 lines (167 loc) · 5.17 KB
/
voyager.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package years
import (
"fmt"
"slices"
)
// Voyager is a wrapper for a waypoint that allows for traversing through it
type Voyager struct {
root Waypoint
// parser is used for parsing time when needed (e.g. when navigating)
parser *Parser
}
func NewVoyager(root Waypoint, parserArg ...*Parser) *Voyager {
v := &Voyager{root: root, parser: NewParser()}
if len(parserArg) > 0 {
v.parser = parserArg[0]
} else {
v.parser = NewParser()
}
return v
}
// Traversing means walking through voyager's prepared tree
// TraverseDirection is a direction for traversing (e.g. past or future)
type TraverseDirection string
const (
TraverseDirectionPast TraverseDirection = "past"
TraverseDirectionFuture TraverseDirection = "future"
)
// TraverseNodesMode specifies which type of nodes to traverse (e.g. leaves only or containers only)
type TraverseNodesMode string
const (
TraverseLeavesOnly TraverseNodesMode = "leaves_only"
TraverseContainersOnly TraverseNodesMode = "containers_only"
TraverseAllNodes TraverseNodesMode = "all"
)
type traverseConfig struct {
direction TraverseDirection
nodesMode TraverseNodesMode
includeNonCalendarNodes bool
}
// defaultTraverseConfig is Future->Past + all type of nodes
func defaultTraverseConfig() traverseConfig {
return traverseConfig{
direction: TraverseDirectionPast,
nodesMode: TraverseAllNodes,
}
}
// isTraversable checks if a given waypoint is traversable corresponding to config
func (config *traverseConfig) isTraversable(waypoint Waypoint) bool {
if waypoint.Time().IsZero() && !config.includeNonCalendarNodes {
return false
}
if config.nodesMode == TraverseAllNodes {
return true
}
okLeavesOnly := config.nodesMode == TraverseLeavesOnly && !waypoint.IsContainer()
if okLeavesOnly {
return true
}
okContainersOnly := config.nodesMode == TraverseContainersOnly && waypoint.IsContainer()
if okContainersOnly {
return true
}
return false
}
// TraverseOption defines functional options for the Traverse function
type TraverseOption func(*traverseConfig)
// O_PAST returns a TraverseOption for traversing in Past direction
func O_PAST() TraverseOption {
return func(o *traverseConfig) { o.direction = TraverseDirectionPast }
}
// O_FUTURE returns a TraverseOption for traversing in Future direction
func O_FUTURE() TraverseOption {
return func(o *traverseConfig) { o.direction = TraverseDirectionFuture }
}
// O_LEAVES_ONLY returns a TraverseOption for traversing only leaf nodes
func O_LEAVES_ONLY() TraverseOption {
return func(o *traverseConfig) { o.nodesMode = TraverseLeavesOnly }
}
// O_CONTAINERS_ONLY returns a TraverseOption for traversing only container nodes
func O_CONTAINERS_ONLY() TraverseOption {
return func(o *traverseConfig) { o.nodesMode = TraverseContainersOnly }
}
// O_ALL returns a TraverseOption for traversing all nodes
func O_ALL() TraverseOption {
return func(o *traverseConfig) { o.nodesMode = TraverseAllNodes }
}
// O_NON_CALENDAR returns a TraverseOption for including non calendar nodes
func O_NON_CALENDAR() TraverseOption {
return func(o *traverseConfig) { o.includeNonCalendarNodes = true }
}
// Traverse traverses through a given waypoint (all its children recursively)
func (v *Voyager) Traverse(cb func(w Waypoint), opts ...TraverseOption) error {
config := defaultTraverseConfig()
for _, opt := range opts {
opt(&config)
}
// directionSign will be used in sorting func
var directionSign int
switch config.direction {
case TraverseDirectionPast:
directionSign = -1
case TraverseDirectionFuture:
directionSign = 1
default:
panic("invalid traverse direction: " + config.direction)
}
sortFn := func(a, b Waypoint) int {
if a.Time() == b.Time() {
return directionSign
}
if a.Time().After(b.Time()) {
return directionSign
}
return -directionSign
}
sorted := AllChildren(v.root)
sorted = append(sorted, v.root)
slices.SortFunc(sorted, sortFn)
for _, sw := range sorted {
if config.isTraversable(sw) {
cb(sw)
}
}
return nil
}
// Navigate returns the first found Waypoint that matches given time (as a string)
// E.g. Navigate("yesterday") returns waypoint corresponding to the yesterday's date
func (v *Voyager) Navigate(to string) (Waypoint, error) {
navigateTo, err := v.parser.Parse("", to)
if err != nil {
return nil, fmt.Errorf("could not parse time: %w", err)
}
var found Waypoint
if err := v.Traverse(func(w Waypoint) {
if found != nil {
return
}
if w.Time().Equal(navigateTo) {
found = w
return
}
}); err != nil {
return nil, fmt.Errorf("could not traverse: %w", err)
}
return found, nil
}
// Find returns the all found Waypoints that match given time (as a string)
// e.g. Find("yesterday") returns all waypoints whose time is in the "yesterday" range
func (v *Voyager) Find(timeStr string) ([]Waypoint, error) {
navigateTo, err := v.parser.Parse("", timeStr)
if err != nil {
return nil, fmt.Errorf("could not parse time: %w", err)
}
found := make([]Waypoint, 0)
if err := v.Traverse(func(w Waypoint) {
if found != nil {
return
}
if w.Time().Equal(navigateTo) {
found = append(found, w)
return
}
}); err != nil {
return nil, fmt.Errorf("could not traverse: %w", err)
}
return found, nil
}