forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup.go
99 lines (82 loc) · 2.65 KB
/
group.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
package depinject
import (
"fmt"
"reflect"
"github.com/cockroachdb/errors"
"cosmossdk.io/depinject/internal/graphviz"
)
// ManyPerContainerType marks a type which automatically gets grouped together. For an ManyPerContainerType T,
// T and []T can be declared as output parameters for providers as many times within the container
// as desired. All of the provided values for T can be retrieved by declaring an
// []T input parameter.
type ManyPerContainerType interface {
// IsManyPerContainerType is a marker function which just indicates that this is a many-per-container type.
IsManyPerContainerType()
}
var manyPerContainerTypeType = reflect.TypeOf((*ManyPerContainerType)(nil)).Elem()
func isManyPerContainerType(t reflect.Type) bool {
return t.Implements(manyPerContainerTypeType)
}
func isManyPerContainerSliceType(typ reflect.Type) bool {
return typ.Kind() == reflect.Slice && isManyPerContainerType(typ.Elem())
}
type groupResolver struct {
typ reflect.Type
sliceType reflect.Type
idxsInValues []int
providers []*simpleProvider
resolved bool
values reflect.Value
graphNode *graphviz.Node
}
func (g *groupResolver) getType() reflect.Type {
return g.sliceType
}
type sliceGroupResolver struct {
*groupResolver
}
func (g *groupResolver) describeLocation() string {
return fmt.Sprintf("many-per-container type %v", g.typ)
}
func (g *sliceGroupResolver) resolve(c *container, _ *moduleKey, caller Location) (reflect.Value, error) {
// Log
c.logf("Providing many-per-container type slice %v to %s from:", g.sliceType, caller.Name())
c.indentLogger()
for _, node := range g.providers {
c.logf(node.provider.Location.String())
}
c.dedentLogger()
// Resolve
if !g.resolved {
res := reflect.MakeSlice(g.sliceType, 0, 0)
for i, node := range g.providers {
values, err := node.resolveValues(c)
if err != nil {
return reflect.Value{}, err
}
value := values[g.idxsInValues[i]]
if value.Kind() == reflect.Slice {
n := value.Len()
for j := 0; j < n; j++ {
res = reflect.Append(res, value.Index(j))
}
} else {
res = reflect.Append(res, value)
}
}
g.values = res
g.resolved = true
}
return g.values, nil
}
func (g *groupResolver) resolve(_ *container, _ *moduleKey, _ Location) (reflect.Value, error) {
return reflect.Value{}, errors.Errorf("%v is an many-per-container type and cannot be used as an input value, instead use %v", g.typ, g.sliceType)
}
func (g *groupResolver) addNode(n *simpleProvider, i int) error {
g.providers = append(g.providers, n)
g.idxsInValues = append(g.idxsInValues, i)
return nil
}
func (g groupResolver) typeGraphNode() *graphviz.Node {
return g.graphNode
}