generated from snivilised/astrolib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomposites.go
236 lines (219 loc) Β· 6.3 KB
/
composites.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package age
import (
"github.com/snivilised/agenor/pref"
"github.com/snivilised/pants"
)
// Composites enable the client to build CLIs without having to duplicate
// code. A composite is useful for any CLI that supports more than 1 scenario
// that would otherwise require code duplication.
//
// π `Hydra` (multi faceted/many headed) : supports all four scenarios
// π° `Hare` (speedy): only supports run
// π’ `Tortoise` (slow): only supports walk
// π‘ `Goldfish` (no memory/no resume) : only supports prime
type (
// Scenario is a function that encodes within it the semantics of walk vs run
// and prime vs resume. When invoked, it returns the underlying navigator
// upon which a traversal session can be executed. The defined scenarios are
// as follows:
// * walk/prime
// * walk/resume
// * run/prime
// * run/resume
Scenario func(facade pref.Facade, settings ...pref.Option) Navigator
)
// Hydra is a composite that a client can use to build a cli that implements
// all four scenarios.
//
// The walk scenarios would ordinarily look something like this...
//
// # Walk/Prime
//
// Example:
//
// age.Walk().Configure().Extent(age.Prime(facade, options...)).Navigate(ctx)
//
// # Walk/Resume
//
// Example:
//
// age.Walk().Configure().Extent(age.Resume(facade, options...)).Navigate(ctx)
//
// The run scenarios would ordinarily look something like this...
//
// # Run/Prime
//
// Example:
//
// age.Run().Configure().Extent(age.Prime(facade, options...)).Navigate(ctx)
//
// # Run/Resume
//
// Example:
//
// age.Run().Configure().Extent(age.Resume(facade, options...)).Navigate(ctx)
//
// and these would need to be invoked conditionally depending on flags on the CLI.
// Notice the duplication; this can be resolved using the Hydra composite
// which can run all scenarios, so 2 values needs to be specified:
// isWalk and isPrime.
//
// # Hydra
//
// Example:
//
// var wg sync.WaitGroup
// isWalk := \<set depending on wether user requested walk or run\>
// isPrime := \<set depending on wether user requested prime or resume\>
// age.Hydra(isWalk, isPrime, &wg)(facade, options...).Navigate(ctx)
func Hydra(isWalk, isPrime bool, wg pants.WaitGroup) Scenario {
if isWalk && isPrime {
return slowPrime
}
if isWalk && !isPrime {
return slowResume
}
if !isWalk && isPrime {
return func(facade pref.Facade, settings ...pref.Option) Navigator {
return fastPrime(facade, wg, settings...)
}
}
return func(facade pref.Facade, settings ...pref.Option) Navigator {
return fastResume(facade, wg, settings...)
}
}
// Hare is a composite that a client can use to build a cli that only implements
// the run scenarios.
//
// The run scenarios would ordinarily look something like this...
//
// # Run/Prime
//
// Example:
//
// age.Run().Configure().Extent(age.Prime(facade, options...)).Navigate(ctx)
//
// # Run/Resume
//
// Example:
//
// age.Run().Configure().Extent(age.Resume(facade, options...)).Navigate(ctx)
//
// and these would need to be invoked conditionally depending on flags on the CLI.
// Notice the duplication; this can be resolved using the Hare composite
// which can only invoke Run sessions, so the query function is being asked to
// determine if it should prime or resume:
//
// # Hare
//
// Example:
//
// var wg sync.WaitGroup
// isPrime := \<set depending on wether user requested prime or resume\>
// age.Hare(isPrime, &wg)(facade, options...).Navigate(ctx)
func Hare(isPrime bool, wg pants.WaitGroup) Scenario {
if isPrime {
return func(facade pref.Facade, settings ...pref.Option) Navigator {
return fastPrime(facade, wg, settings...)
}
}
return func(facade pref.Facade, settings ...pref.Option) Navigator {
return fastResume(facade, wg, settings...)
}
}
// Tortoise is a composite that a client can use to build a cli that only implements
// the walk scenarios.
//
// The walk scenarios would ordinarily look something like this...
//
// # Walk/Prime
//
// Example:
//
// age.Walk().Configure().Extent(age.Prime(facade, options...)).Navigate(ctx)
//
// # Walk/Resume
//
// Example:
//
// age.Walk().Configure().Extent(age.Resume(facade, options...)).Navigate(ctx)
//
// and these would need to be invoked conditionally depending on flags on the CLI.
// Notice the duplication; this can be resolved using the Tortoise composite
// which can only run Walk sessions, so the query function is being asked to
// determine if it should prime or resume:
//
// # Tortoise
//
// Example:
//
// var wg sync.WaitGroup
// isPrime := \<set depending on wether user requested prime or resume\>
// age.Tortoise(isPrime)(facade, options...).Navigate(ctx)
func Tortoise(isPrime bool) Scenario {
if isPrime {
return slowPrime
}
return slowResume
}
// Goldfish is a composite that a client can use to build a cli that only implements
// the prime scenarios.
//
// The prime scenarios would ordinarily look something like this...
//
// # Walk/Prime
//
// Example:
//
// age.Walk().Configure().Extent(age.Prime(facade, options...)).Navigate(ctx)
//
// # Run/Prime
//
// Example:
//
// age.Run().Configure().Extent(age.Prime(facade, options...)).Navigate(ctx)
//
// and these would need to be invoked conditionally depending on flags on the CLI.
// Notice the duplication; this can be resolved using the Goldfish composite
// which can only run Prime sessions, so the query function is being asked to
// determine if it should walk or run:
//
// # Goldfish
//
// Example:
//
// var wg sync.WaitGroup
// isWalk := \<set depending on wether user requested walk or run\>
// age.Goldfish(isWalk, &wg)(facade, options...).Navigate(ctx)
func Goldfish(isWalk bool, wg pants.WaitGroup) Scenario {
if isWalk {
return slowPrime
}
return func(facade pref.Facade, settings ...pref.Option) Navigator {
return fastPrime(facade, wg, settings...)
}
}
func slowPrime(facade pref.Facade, settings ...pref.Option) Navigator {
return Walk().Configure().Extent(Prime(
facade,
settings...,
))
}
func slowResume(facade pref.Facade, settings ...pref.Option) Navigator {
return Walk().Configure().Extent(Resume(
facade,
settings...,
))
}
func fastPrime(facade pref.Facade, wg pants.WaitGroup, settings ...pref.Option) Navigator {
return Run(wg).Configure().Extent(Prime(
facade,
settings...,
))
}
func fastResume(facade pref.Facade, wg pants.WaitGroup, settings ...pref.Option) Navigator {
return Run(wg).Configure().Extent(Resume(
facade,
settings...,
))
}