-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdo.go
55 lines (42 loc) · 1.01 KB
/
do.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
package pipe
import "errors"
var (
ErrPassArgsOnDoApplyFnFirstSeqNotAllowed = errors.New("pipe.Pass() on pipe.Do(...) first sequence is not allowed")
)
type do struct {
applyFns []*applyFn
errors []error
}
func Do(applyFns ...*applyFn) interface{} {
d := do{applyFns: applyFns}
return d.Result()
}
func (do *do) validate() error {
if len(do.applyFns) == 0 {
return nil
}
if isApplyFnHasPassArgs(do.applyFns[0].args) {
return ErrPassArgsOnDoApplyFnFirstSeqNotAllowed
}
for applyFnSeq, applyFn := range do.applyFns {
if err := applyFn.validateDeclaration(applyFnSeq); err != nil {
return err
}
}
return nil
}
func (do *do) Result() interface{} {
if err := do.validate(); err != nil {
panic(err)
}
if len(do.applyFns) == 0 {
return nil
}
var prepare prepare
for sequence, applyFn := range do.applyFns {
prepare.sequence = sequence
prepare.applyFn = applyFn
prepare.compoundResult = applyFn.fnCandidateValue.Call(prepare.fnArgs())[0]
}
return prepare.compoundResult.Interface()
}