-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcast.go
More file actions
79 lines (74 loc) · 1.99 KB
/
cast.go
File metadata and controls
79 lines (74 loc) · 1.99 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package kinitx
import (
"reflect"
"github.com/go-kata/kerror"
"github.com/go-kata/kinit"
)
// castToConstructor returns a constructor based on the given entity.
//
// See the documentation for the Provide to find out possible values of the argument x.
func castToConstructor(x interface{}) (kinit.Constructor, error) {
if x == nil {
return nil, kerror.New(kerror.EViolation, "function, struct or struct pointer expected, nil given")
}
if ctor, ok := x.(kinit.Constructor); ok {
return ctor, nil
}
var ctor kinit.Constructor
var err error
t := reflect.TypeOf(x)
switch t.Kind() {
default:
return nil, kerror.Newf(kerror.EViolation, "function, struct or struct pointer expected, %s given", t)
case reflect.Func:
var isOpener bool
switch t.NumOut() {
case 2:
if t.Out(1) != errorType {
break
}
fallthrough
case 1:
isOpener = t.Out(0).Implements(closerType)
}
if isOpener {
ctor, err = NewOpener(x)
} else {
ctor, err = NewConstructor(x)
}
case reflect.Struct, reflect.Ptr:
ctor, err = NewInitializer(x)
}
return ctor, err
}
// castToProcessor returns a processor based on the given entity.
//
// See the documentation for the Attach to find out possible values of the argument x.
func castToProcessor(x interface{}) (kinit.Processor, error) {
if x == nil {
return nil, kerror.New(kerror.EViolation, "function expected, nil given")
}
if proc, ok := x.(kinit.Processor); ok {
return proc, nil
}
return NewProcessor(x)
}
// castToFunctor returns a new functor based on the given entity.
//
// See the documentation for the Run to find out possible values of the argument x.
func castToFunctor(x interface{}) (kinit.Functor, error) {
if x == nil {
return nil, kerror.New(kerror.EViolation, "function or value expected, nil given")
}
if fun, ok := x.(kinit.Functor); ok {
return fun, nil
}
var fun kinit.Functor
var err error
if reflect.TypeOf(x).Kind() == reflect.Func {
fun, err = NewFunctor(x)
} else {
fun, err = NewInjector(x)
}
return fun, err
}