-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpass.go
58 lines (45 loc) · 1.19 KB
/
pass.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 pipe
import (
"errors"
"reflect"
)
var (
errPassArgsFnCandidateIllegalType = errors.New("pipe.Pass(...) only accepts function in the argument")
errPassArgsFnCandidateNoArgument = errors.New("pipe.Pass(...) accepted function should accept at least one argument")
errPassArgsFnCandidateVoidReturn = errors.New("pipe.Pass(...) accepted function should not return void")
)
type emptyFnCandidate struct {
}
type passArgs struct {
fnCandidate reflect.Value
}
func Pass(fn ...interface{}) *passArgs {
if len(fn) > 0 {
return &passArgs{fnCandidate: reflect.ValueOf(fn[0])}
}
return &passArgs{fnCandidate: reflect.ValueOf(emptyFnCandidate{})}
}
func (pa *passArgs) validateDeclaration() error {
if pa.fnCandidate.Kind() != reflect.Func {
return errPassArgsFnCandidateIllegalType
}
if pa.fnCandidate.Type().NumIn() == 0 {
return errPassArgsFnCandidateNoArgument
}
if pa.fnCandidate.Type().NumOut() == 0 {
return errPassArgsFnCandidateVoidReturn
}
return nil
}
func isPassArgs(arg interface{}) bool {
_, ok := arg.(passArgs)
return ok
}
func isApplyFnHasPassArgs(args []interface{}) bool {
for _, arg := range args {
if isPassArgs(arg) {
return true
}
}
return false
}