-
Notifications
You must be signed in to change notification settings - Fork 3
/
builder.go
374 lines (323 loc) · 12 KB
/
builder.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package brigodier
// Literal returns a new literal node builder.
func Literal(literal string) *LiteralArgumentBuilder {
return &LiteralArgumentBuilder{Literal: literal}
}
// Argument returns a new argument node builder.
func Argument(name string, argType ArgumentType) *RequiredArgumentBuilder {
return &RequiredArgumentBuilder{Name: name, Type: argType}
}
type (
// Builder is a CommandNode builder.
Builder interface{ Build() CommandNode }
// NodeBuilder is a Builder with build-methods.
NodeBuilder interface {
Builder
Then(arguments ...Builder) NodeBuilder
Executes(command Command) NodeBuilder
Requires(fn RequireFn) NodeBuilder
Redirect(target CommandNode) NodeBuilder
RedirectWithModifier(target CommandNode, modifier RedirectModifier) NodeBuilder
Fork(target CommandNode, modifier RedirectModifier) NodeBuilder
Forward(target CommandNode, modifier RedirectModifier, fork bool) NodeBuilder
}
// LiteralNodeBuilder is a LiteralCommandNode builder.
LiteralNodeBuilder interface {
Builder
BuildLiteral() *LiteralCommandNode
NodeBuilder() NodeBuilder // Convert to NodeBuilder
Then(arguments ...Builder) LiteralNodeBuilder
Executes(command Command) LiteralNodeBuilder
Requires(fn RequireFn) LiteralNodeBuilder
Redirect(target CommandNode) LiteralNodeBuilder
RedirectWithModifier(target CommandNode, modifier RedirectModifier) LiteralNodeBuilder
Fork(target CommandNode, modifier RedirectModifier) LiteralNodeBuilder
Forward(target CommandNode, modifier RedirectModifier, fork bool) LiteralNodeBuilder
}
// ArgumentNodeBuilder is an ArgumentCommandNode builder.
ArgumentNodeBuilder interface {
Builder
BuildArgument() *ArgumentCommandNode
NodeBuilder() NodeBuilder // Convert to NodeBuilder
Then(arguments ...Builder) ArgumentNodeBuilder
Suggests(provider SuggestionProvider) ArgumentNodeBuilder
Executes(command Command) ArgumentNodeBuilder
Requires(fn RequireFn) ArgumentNodeBuilder
Redirect(target CommandNode) ArgumentNodeBuilder
RedirectWithModifier(target CommandNode, modifier RedirectModifier) ArgumentNodeBuilder
Fork(target CommandNode, modifier RedirectModifier) ArgumentNodeBuilder
Forward(target CommandNode, modifier RedirectModifier, fork bool) ArgumentNodeBuilder
}
// LiteralArgumentBuilder builds a LiteralCommandNode.
LiteralArgumentBuilder struct {
Literal string
ArgumentBuilder
}
// RequiredArgumentBuilder builds an ArgumentCommandNode.
RequiredArgumentBuilder struct {
Name string
Type ArgumentType
SuggestionsProvider SuggestionProvider // Optional
ArgumentBuilder
}
)
var _ LiteralNodeBuilder = (*LiteralArgumentBuilder)(nil)
var _ ArgumentNodeBuilder = (*RequiredArgumentBuilder)(nil)
// ArgumentBuilder has the common builder fields and is wrapped by
// LiteralArgumentBuilder and RequiredArgumentBuilder
type ArgumentBuilder struct {
Arguments RootCommandNode
Command Command
Requirement RequireFn
Target CommandNode
Modifier RedirectModifier
Forks bool
}
func (b *ArgumentBuilder) build() *Node {
n := &Node{
requirement: b.Requirement,
redirect: b.Target,
command: b.Command,
modifier: b.Modifier,
forks: b.Forks,
}
b.Arguments.ChildrenOrdered().Range(func(_ string, arg CommandNode) bool {
n.AddChild(arg)
return true
})
return n
}
func (b *ArgumentBuilder) then(arguments ...Builder) {
for _, a := range arguments {
b.Arguments.AddChild(a.Build())
}
}
// Then adds arguments to the resulting LiteralCommandNode.
func (b *LiteralArgumentBuilder) Then(arguments ...Builder) LiteralNodeBuilder {
b.ArgumentBuilder.then(arguments...)
return b
}
// Then adds arguments to the resulting ArgumentCommandNode.
func (b *RequiredArgumentBuilder) Then(arguments ...Builder) ArgumentNodeBuilder {
b.ArgumentBuilder.then(arguments...)
return b
}
func (b *RequiredArgumentBuilder) NodeBuilder() NodeBuilder { return &nodeBuilder{a: b} }
func (b *LiteralArgumentBuilder) NodeBuilder() NodeBuilder { return &nodeBuilder{l: b} }
func (n *LiteralCommandNode) Build() CommandNode { return n }
func (n *LiteralCommandNode) CreateBuilder() NodeBuilder {
return &nodeBuilder{l: n.CreateLiteralBuilder()}
}
func (n *LiteralCommandNode) CreateLiteralBuilder() LiteralNodeBuilder {
return Literal(n.Literal).
Requires(n.Requirement()).
Forward(n.Redirect(), n.RedirectModifier(), n.IsFork()).
Executes(n.Command())
}
func (b *LiteralArgumentBuilder) Build() CommandNode { return b.BuildLiteral() }
func (b *LiteralArgumentBuilder) BuildLiteral() *LiteralCommandNode {
return &LiteralCommandNode{
Node: *b.ArgumentBuilder.build(),
Literal: b.Literal,
}
}
func (a *ArgumentCommandNode) Build() CommandNode { return a }
func (a *ArgumentCommandNode) CreateBuilder() NodeBuilder {
return &nodeBuilder{a: a.CreateArgumentBuilder()}
}
func (a *ArgumentCommandNode) CreateArgumentBuilder() ArgumentNodeBuilder {
return Argument(a.Name(), a.Type()).
Requires(a.Requirement()).
Forward(a.Redirect(), a.RedirectModifier(), a.IsFork()).
Suggests(a.CustomSuggestions()).
Executes(a.Command())
}
func (b *RequiredArgumentBuilder) Build() CommandNode { return b.BuildArgument() }
func (b *RequiredArgumentBuilder) BuildArgument() *ArgumentCommandNode {
return &ArgumentCommandNode{
Node: *b.ArgumentBuilder.build(),
name: b.Name,
argType: b.Type,
customSuggestions: b.SuggestionsProvider,
}
}
// Suggests defines the SuggestionProvider of the resulting ArgumentCommandNode.
func (b *RequiredArgumentBuilder) Suggests(provider SuggestionProvider) ArgumentNodeBuilder {
b.SuggestionsProvider = provider
return b
}
// Executes defines the Command of the resulting LiteralCommandNode.
func (b *LiteralArgumentBuilder) Executes(command Command) LiteralNodeBuilder {
b.ArgumentBuilder.Executes(command)
return b
}
// Executes defines the Command of the resulting ArgumentCommandNode.
func (b *RequiredArgumentBuilder) Executes(command Command) ArgumentNodeBuilder {
b.ArgumentBuilder.Executes(command)
return b
}
// Executes defines the Command of the resulting CommandNode.
func (b *ArgumentBuilder) Executes(command Command) *ArgumentBuilder {
b.Command = command
return b
}
// Requires defines the RequireFn of the resulting LiteralCommandNode.
func (b *LiteralArgumentBuilder) Requires(fn RequireFn) LiteralNodeBuilder {
b.ArgumentBuilder.Requires(fn)
return b
}
// Requires defines the RequireFn of the resulting ArgumentCommandNode.
func (b *RequiredArgumentBuilder) Requires(fn RequireFn) ArgumentNodeBuilder {
b.ArgumentBuilder.Requires(fn)
return b
}
// Requires defines the RequireFn of the resulting CommandNode.
func (b *ArgumentBuilder) Requires(fn RequireFn) *ArgumentBuilder {
b.Requirement = fn
return b
}
// Redirect defines the redirect node of the resulting LiteralCommandNode.
func (b *LiteralArgumentBuilder) Redirect(target CommandNode) LiteralNodeBuilder {
b.ArgumentBuilder.Redirect(target)
return b
}
// Redirect defines the redirect node of the resulting ArgumentCommandNode.
func (b *RequiredArgumentBuilder) Redirect(target CommandNode) ArgumentNodeBuilder {
b.ArgumentBuilder.Redirect(target)
return b
}
// Redirect defines the redirect node of the resulting CommandNode.
func (b *ArgumentBuilder) Redirect(target CommandNode) *ArgumentBuilder {
return b.Forward(target, nil, false)
}
// RedirectWithModifier defines the redirect modifier of the resulting LiteralCommandNode.
func (b *LiteralArgumentBuilder) RedirectWithModifier(target CommandNode, modifier RedirectModifier) LiteralNodeBuilder {
b.ArgumentBuilder.RedirectWithModifier(target, modifier)
return b
}
// RedirectWithModifier defines the redirect modifier of the resulting ArgumentCommandNode.
func (b *RequiredArgumentBuilder) RedirectWithModifier(target CommandNode, modifier RedirectModifier) ArgumentNodeBuilder {
b.ArgumentBuilder.RedirectWithModifier(target, modifier)
return b
}
// RedirectWithModifier defines the redirect modifier of the resulting CommandNode.
func (b *ArgumentBuilder) RedirectWithModifier(target CommandNode, modifier RedirectModifier) *ArgumentBuilder {
return b.Forward(target, modifier, false)
}
// Fork defines the fork of the resulting LiteralCommandNode.
func (b *LiteralArgumentBuilder) Fork(target CommandNode, modifier RedirectModifier) LiteralNodeBuilder {
b.ArgumentBuilder.Fork(target, modifier)
return b
}
// Fork defines the fork of the resulting ArgumentCommandNode.
func (b *RequiredArgumentBuilder) Fork(target CommandNode, modifier RedirectModifier) ArgumentNodeBuilder {
b.ArgumentBuilder.Fork(target, modifier)
return b
}
// Fork defines the fork of the resulting CommandNode.
func (b *ArgumentBuilder) Fork(target CommandNode, modifier RedirectModifier) *ArgumentBuilder {
return b.Forward(target, modifier, true)
}
// Forward defines the forward of the resulting ArgumentCommandNode.
func (b *LiteralArgumentBuilder) Forward(target CommandNode, modifier RedirectModifier, fork bool) LiteralNodeBuilder {
b.ArgumentBuilder.Forward(target, modifier, fork)
return b
}
// Forward defines the forward of the resulting ArgumentCommandNode.
func (b *RequiredArgumentBuilder) Forward(target CommandNode, modifier RedirectModifier, fork bool) ArgumentNodeBuilder {
b.ArgumentBuilder.Forward(target, modifier, fork)
return b
}
func (b *ArgumentBuilder) Forward(target CommandNode, modifier RedirectModifier, fork bool) *ArgumentBuilder {
if len(b.Arguments.children) != 0 {
return b // cannot forward a node with children
}
b.Target = target
b.Modifier = modifier
b.Forks = fork
return b
}
// CreateBuilder cannot create a builder from root node and returns a nop-builder!
func (r *RootCommandNode) CreateBuilder() NodeBuilder {
return nil
}
// nodeBuilder implements NodeBuilder and wraps either one of:
// LiteralNodeBuilder
// ArgumentNodeBuilder
type nodeBuilder struct {
l LiteralNodeBuilder
a ArgumentNodeBuilder
}
var _ NodeBuilder = (*nodeBuilder)(nil)
func (b *nodeBuilder) Build() CommandNode {
if b.l == nil {
return b.a.Build()
}
return b.l.Build()
}
func (b *nodeBuilder) Then(arguments ...Builder) NodeBuilder {
if b.l == nil {
b.a.Then(arguments...)
} else {
b.l.Then(arguments...)
}
return b
}
func (b *nodeBuilder) Executes(command Command) NodeBuilder {
if b.l == nil {
b.a.Executes(command)
} else {
b.l.Executes(command)
}
return b
}
func (b *nodeBuilder) Requires(fn RequireFn) NodeBuilder {
if b.l == nil {
b.a.Requires(fn)
} else {
b.l.Requires(fn)
}
return b
}
func (b *nodeBuilder) Redirect(target CommandNode) NodeBuilder {
if b.l == nil {
b.a.Redirect(target)
} else {
b.l.Redirect(target)
}
return b
}
func (b *nodeBuilder) RedirectWithModifier(target CommandNode, modifier RedirectModifier) NodeBuilder {
if b.l == nil {
b.a.RedirectWithModifier(target, modifier)
} else {
b.l.RedirectWithModifier(target, modifier)
}
return b
}
func (b *nodeBuilder) Fork(target CommandNode, modifier RedirectModifier) NodeBuilder {
if b.l == nil {
b.a.Fork(target, modifier)
} else {
b.l.Fork(target, modifier)
}
return b
}
func (b *nodeBuilder) Forward(target CommandNode, modifier RedirectModifier, fork bool) NodeBuilder {
if b.l == nil {
b.a.Forward(target, modifier, fork)
} else {
b.l.Forward(target, modifier, fork)
}
return b
}
type nopNodeBuilder struct{}
var _ NodeBuilder = (*nopNodeBuilder)(nil)
func (b *nopNodeBuilder) Build() CommandNode { return nil }
func (b *nopNodeBuilder) Then(...Builder) NodeBuilder { return b }
func (b *nopNodeBuilder) Executes(Command) NodeBuilder { return b }
func (b *nopNodeBuilder) Requires(RequireFn) NodeBuilder { return b }
func (b *nopNodeBuilder) Redirect(CommandNode) NodeBuilder { return b }
func (b *nopNodeBuilder) RedirectWithModifier(CommandNode, RedirectModifier) NodeBuilder { return b }
func (b *nopNodeBuilder) Fork(CommandNode, RedirectModifier) NodeBuilder { return b }
func (b *nopNodeBuilder) Forward(CommandNode, RedirectModifier, bool) NodeBuilder { return b }