forked from yourheropaul/inj
/
graph_connect.go
150 lines (111 loc) · 3.04 KB
/
graph_connect.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package inj
import (
"fmt"
"reflect"
)
// Usually called after Provide() to assign the values
// of all requested dependencies.
func (g *Graph) connect() {
// Reset error counts
g.UnmetDependencies = 0
g.Errors = make([]string, 0)
// loop through all nodes
for _, node := range g.nodes {
// assign dependencies to the object
for _, dep := range node.Dependencies {
if e := g.assignValueToNode(node.Value, dep); e != nil {
g.UnmetDependencies++
g.Errors = append(g.Errors, e.Error())
}
}
}
}
func (g *Graph) assignValueToNode(o reflect.Value, dep graphNodeDependency) error {
parents := []reflect.Value{}
v, err := g.findFieldValue(o, dep.Path, &parents)
vtype := v.Type()
if err != nil {
return err
}
// If value has already been set, then skip it
if !zero(v) {
return nil
}
// Sanity check
if !v.CanSet() {
return fmt.Errorf("%s%s can't be set", o, dep.Path)
}
// If there are any datasource paths supplied...
for _, path := range dep.DatasourcePaths {
// ...check to see if a datasource reader has the value
for _, d := range g.datasourceReaders {
if dsvalue, err := d.Read(path); err == nil {
typ := reflect.TypeOf(dsvalue)
value := reflect.ValueOf(dsvalue)
if typ != vtype && typ.ConvertibleTo(vtype) {
value = value.Convert(vtype)
}
if value.Type().AssignableTo(vtype) {
// The value can be set by reflection
v.Set(value)
// Any datasourcewriters need to be updated
for _, w := range g.datasourceWriters {
w.Write(path, v.Interface())
}
return nil
}
}
}
}
// Run through the graph and see if anything is settable
for typ, node := range g.nodes {
valid := true
// Don't assign anything to itself or its children
for _, parent := range parents {
if parent.Interface() == node.Value.Interface() {
valid = false
break
}
}
if !valid {
continue
}
if typ.AssignableTo(v.Type()) {
// The value can be set by reflection
v.Set(node.Value)
// Any datasourcewriters need to be updated
for _, path := range dep.DatasourcePaths {
for _, w := range g.datasourceWriters {
w.Write(path, v.Interface())
}
}
return nil
}
}
return fmt.Errorf("Couldn't find suitable dependency for %s", dep.Type)
}
// Required a struct type
func (g *Graph) findFieldValue(parent reflect.Value, path structPath, linneage *[]reflect.Value) (reflect.Value, error) {
*linneage = append(*linneage, parent)
// Dereference incoming values
if parent.Kind() == reflect.Ptr {
parent = parent.Elem()
}
// Only accept structs
if parent.Kind() != reflect.Struct {
return parent, fmt.Errorf("Type is %s, not struct", parent.Kind().String())
}
// Take the first entry from the path
stub, path := path.Shift()
// Try to get the field
f := parent.FieldByName(stub)
if !f.IsValid() {
return f, fmt.Errorf("Can't find field %s in %s", stub, parent)
}
// If that's the end of the path, return the value
if path.Empty() {
return f, nil
}
// Otherwise recurse
return g.findFieldValue(f, path, linneage)
}