-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathoperation.go
58 lines (46 loc) · 1.18 KB
/
operation.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
package ograph
type Op func(pipeline *Pipeline, element *Element)
var Rely = func(dependencies ...*Element) Op {
return func(pipeline *Pipeline, element *Element) {
for _, dep := range dependencies {
if pipeline.elements[dep.Name] == nil {
pipeline.Register(dep)
}
if pipeline.elements[dep.Name] == dep {
pipeline.graph.AddEdge(dep.Name, element.Name)
}
}
}
}
var Then = func(nextElements ...*Element) Op {
return func(pipeline *Pipeline, element *Element) {
for _, next := range nextElements {
if pipeline.elements[next.Name] == nil {
pipeline.Register(next)
}
if pipeline.elements[next.Name] == next {
pipeline.graph.AddEdge(element.Name, next.Name)
}
}
}
}
// Register(a, ograph.Branch(b, c, d)) => a->b->c->d
var Branch = func(elements ...*Element) Op {
return func(pipeline *Pipeline, element *Element) {
if len(elements) == 0 {
return
}
var prev, next *Element
prev = element
for i := range elements {
next = elements[i]
if pipeline.elements[next.Name] == nil {
pipeline.Register(next)
}
if pipeline.elements[next.Name] == next {
pipeline.graph.AddEdge(prev.Name, next.Name)
}
prev = next
}
}
}