-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
43 lines (34 loc) · 880 Bytes
/
utils.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
package godepi
import (
"fmt"
"reflect"
)
func getDepName(dep interface{}) string {
switch dep.(type) {
case string:
// TODO add checking of depName existence
return dep.(string)
default:
nType := reflect.TypeOf(dep)
// TODO add fields count check
nField := nType.Field(0)
return getFullDepPath(nField.Type)
}
}
func getFullDepPath(t reflect.Type) string {
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
return t.PkgPath() + "." + t.Name()
}
func checkFactory(dep interface{}, f FactoryFunc) {
if getFactoryReturnDepName(f) != getDepName(dep) {
panic(fmt.Sprintf(`wrong return type in factory for "%s" dependency, got "%s"`, getDepName(dep), getFactoryReturnDepName(f)))
}
}
func getFactoryReturnDepName(f FactoryFunc) string {
fType := reflect.TypeOf(f)
// TODO add fields count check
retType := fType.Out(0)
return getFullDepPath(retType)
}