-
Notifications
You must be signed in to change notification settings - Fork 0
/
composite_task.go
60 lines (50 loc) · 1.2 KB
/
composite_task.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
package joyride
import (
"context"
"reflect"
)
type CompositeTask []Executable
func (this CompositeTask) RequiredReads() (reads []interface{}) {
for _, task := range this {
reader, ok := task.(RequiredReads)
if !ok || isNil(reader) {
continue
}
reads = append(reads, reader.RequiredReads()...)
}
return reads
}
func (this CompositeTask) Execute(ctx context.Context) TaskResult {
if len(this) == 0 {
return nil
}
result := New()
var executables CompositeTask
for _, task := range this {
if task == nil || isNil(task) {
continue
}
inner := task.Execute(ctx)
if inner == nil || isNil(inner) {
continue
}
result.AddPendingWrites(inner.PendingWrites()...)
result.AddPendingMessages(inner.PendingMessages()...)
subsequentTask := inner.SubsequentTask()
if subsequentTask != nil {
executables = append(executables, subsequentTask)
}
}
if len(executables) > 0 {
result.SetSubsequentTask(executables)
}
return result
}
func isNil(v interface{}) bool {
switch reflect.TypeOf(v).Kind() {
case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.UnsafePointer, reflect.Interface, reflect.Slice:
return reflect.ValueOf(v).IsNil()
default:
return v == nil
}
}