-
Notifications
You must be signed in to change notification settings - Fork 7
/
find.go
54 lines (41 loc) · 1.5 KB
/
find.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
package inject
import (
"fmt"
"reflect"
)
// FindByType resolves all defined pointers that match the type of the supplied slice
// and appends the resolved values to the slice.
func FindByType(g Graph, listPtr interface{}) []reflect.Value {
ptrType := reflect.TypeOf(listPtr)
if ptrType.Kind() != reflect.Ptr {
panic(fmt.Sprintf("listPtr (%v) is not a pointer", ptrType))
}
listType := ptrType.Elem()
if listType.Kind() != reflect.Slice {
panic(fmt.Sprintf("listPtr (%v) is not a pointer to a slice or array", ptrType))
}
listValue := reflect.ValueOf(listPtr).Elem()
values := g.ResolveByType(listType.Elem())
listValue = reflect.Append(listValue, values...)
// update the listPtr value
reflect.ValueOf(listPtr).Elem().Set(listValue)
return values
}
// FindAssignable resolves all defined pointers that are assignable to the type of the supplied slice
// and appends the resolved values to the slice.
func FindAssignable(g Graph, listPtr interface{}) []reflect.Value {
ptrType := reflect.TypeOf(listPtr)
if ptrType.Kind() != reflect.Ptr {
panic(fmt.Sprintf("listPtr (%v) is not a pointer", ptrType))
}
listType := ptrType.Elem()
if listType.Kind() != reflect.Slice {
panic(fmt.Sprintf("listPtr (%v) is not a pointer to a slice or array", ptrType))
}
listValue := reflect.ValueOf(listPtr).Elem()
values := g.ResolveByAssignableType(listType.Elem())
listValue = reflect.Append(listValue, values...)
// update the listPtr value
reflect.ValueOf(listPtr).Elem().Set(listValue)
return values
}