-
Notifications
You must be signed in to change notification settings - Fork 0
/
apply.go
45 lines (36 loc) · 1.1 KB
/
apply.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
package pipe
import (
"errors"
"reflect"
)
var (
errApplyNotAcceptFn = errors.New("pipe.Apply(...) not accept function at the first arg")
errApplyNotAcceptArgs = errors.New("pipe.Apply(...) given function could not accept any arg")
errApplyReturnVoid = errors.New("pipe.Apply(...) given function has void return value")
errApplyMultiReturn = errors.New("pipe.Apply(...) given function has multiple return values")
)
func Apply(fn interface{}, args ...interface{}) *applyFn {
return &applyFn{
fnCandidateValue: reflect.ValueOf(fn),
args: args,
}
}
type applyFn struct {
fnCandidateValue reflect.Value
args []interface{}
}
func (applyFn *applyFn) validateDeclaration(applyFnSequence int) error {
if applyFn.fnCandidateValue.Kind() != reflect.Func {
return errApplyNotAcceptFn
}
if applyFn.fnCandidateValue.Type().NumIn() == 0 && applyFnSequence > 0 {
return errApplyNotAcceptArgs
}
if applyFn.fnCandidateValue.Type().NumOut() == 0 {
return errApplyReturnVoid
}
if applyFn.fnCandidateValue.Type().NumOut() > 1 {
return errApplyMultiReturn
}
return nil
}