-
Notifications
You must be signed in to change notification settings - Fork 1
/
provider.go
63 lines (52 loc) · 1.24 KB
/
provider.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
61
62
63
package mikado
import (
"reflect"
"runtime"
)
type provider struct {
constructor interface{}
name string
inputTypes []reflect.Type
outputTypes []reflect.Type
}
func newProvider(constructor interface{}) *provider {
t := reflect.TypeOf(constructor)
if t.Kind() != reflect.Func {
panic("a constructor must be a function type")
}
p := &provider{
constructor: constructor,
name: runtime.FuncForPC(reflect.ValueOf(constructor).Pointer()).Name(),
}
// outputTypes
for i := 0; i < t.NumOut(); i++ {
p.outputTypes = append(p.outputTypes, t.Out(i))
}
// inputTypes
numIn := t.NumIn()
if t.IsVariadic() && numIn > 0 {
numIn-- // remove the variadic parameter
}
for i := 0; i < numIn; i++ {
p.inputTypes = append(p.inputTypes, t.In(i))
}
return p
}
func (p *provider) call(inputs []reflect.Value) (outputs []reflect.Value) {
return reflect.ValueOf(p.constructor).Call(inputs)
}
func _isErrorType(t reflect.Type) bool {
return t.Implements(reflect.TypeOf((*error)(nil)).Elem())
}
func findProvider(a *App, key typeKey) *provider {
var p *provider
for _, pp := range a.providers {
for _, output := range pp.outputTypes {
outputKey := typeKey{Type: output}
if key == outputKey {
p = pp
}
}
}
return p
}