forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple.go
71 lines (57 loc) · 1.45 KB
/
simple.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
package depinject
import (
"reflect"
"cosmossdk.io/depinject/internal/graphviz"
)
type simpleProvider struct {
provider *providerDescriptor
called bool
values []reflect.Value
moduleKey *moduleKey
}
type simpleResolver struct {
node *simpleProvider
idxInValues int
resolved bool
typ reflect.Type
value reflect.Value
graphNode *graphviz.Node
}
func (s *simpleResolver) getType() reflect.Type {
return s.typ
}
func (s *simpleResolver) describeLocation() string {
return s.node.provider.Location.String()
}
func (s *simpleProvider) resolveValues(ctr *container) ([]reflect.Value, error) {
if !s.called {
values, err := ctr.call(s.provider, s.moduleKey)
if err != nil {
return nil, err
}
s.values = values
s.called = true
}
return s.values, nil
}
func (s *simpleResolver) resolve(c *container, _ *moduleKey, caller Location) (reflect.Value, error) {
// Log
c.logf("Providing %v from %s to %s", s.typ, s.node.provider.Location, caller.Name())
// Resolve
if !s.resolved {
values, err := s.node.resolveValues(c)
if err != nil {
return reflect.Value{}, err
}
value := values[s.idxInValues]
s.value = value
s.resolved = true
}
return s.value, nil
}
func (s simpleResolver) addNode(p *simpleProvider, _ int) error {
return duplicateDefinitionError(s.typ, p.provider.Location, s.node.provider.Location.String())
}
func (s simpleResolver) typeGraphNode() *graphviz.Node {
return s.graphNode
}