forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscope_dep.go
57 lines (45 loc) · 1.3 KB
/
scope_dep.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
package container
import (
"reflect"
"github.com/goccy/go-graphviz/cgraph"
)
type scopeDepProvider struct {
provider *ProviderDescriptor
calledForScope map[Scope]bool
valueMap map[Scope][]reflect.Value
}
type scopeDepResolver struct {
typ reflect.Type
idxInValues int
node *scopeDepProvider
valueMap map[Scope]reflect.Value
graphNode *cgraph.Node
}
func (s scopeDepResolver) describeLocation() string {
return s.node.provider.Location.String()
}
func (s scopeDepResolver) resolve(ctr *container, scope Scope, caller Location) (reflect.Value, error) {
// Log
ctr.logf("Providing %v from %s to %s", s.typ, s.node.provider.Location, caller.Name())
// Resolve
if val, ok := s.valueMap[scope]; ok {
return val, nil
}
if !s.node.calledForScope[scope] {
values, err := ctr.call(s.node.provider, scope)
if err != nil {
return reflect.Value{}, err
}
s.node.valueMap[scope] = values
s.node.calledForScope[scope] = true
}
value := s.node.valueMap[scope][s.idxInValues]
s.valueMap[scope] = value
return value, nil
}
func (s scopeDepResolver) addNode(p *simpleProvider, _ int) error {
return duplicateDefinitionError(s.typ, p.provider.Location, s.node.provider.Location.String())
}
func (s scopeDepResolver) typeGraphNode() *cgraph.Node {
return s.graphNode
}