-
Notifications
You must be signed in to change notification settings - Fork 208
/
graph.go
115 lines (100 loc) · 3.75 KB
/
graph.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
// Copyright (c) 2021 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package dig
import "go.uber.org/dig/internal/graph"
// graphNode is a single node in the dependency graph.
type graphNode struct {
Wrapped interface{}
}
// graphHolder is the dependency graph of the container.
// It saves constructorNodes and paramGroupedSlice (value groups)
// as nodes in the graph.
// It implements the graph interface defined by internal/graph.
// It has 1-1 correspondence with the Scope whose graph it represents.
type graphHolder struct {
// all the nodes defined in the graph.
nodes []*graphNode
// Scope whose graph this holder contains.
s *Scope
// Number of nodes in the graph at last snapshot.
// -1 if no snapshot has been taken.
snap int
}
var _ graph.Graph = (*graphHolder)(nil)
func newGraphHolder(s *Scope) *graphHolder {
return &graphHolder{s: s, snap: -1}
}
func (gh *graphHolder) Order() int { return len(gh.nodes) }
// EdgesFrom returns the indices of nodes that are dependencies of node u.
//
// To do that, it needs to do one of the following:
//
// For constructor nodes, it retrieves the providers of the constructor's
// parameters from the container and reports their orders.
//
// For value group nodes, it retrieves the group providers from the container
// and reports their orders.
func (gh *graphHolder) EdgesFrom(u int) []int {
var orders []int
switch w := gh.Lookup(u).(type) {
case *constructorNode:
for _, param := range w.paramList.Params {
orders = append(orders, getParamOrder(gh, param)...)
}
case *paramGroupedSlice:
providers := gh.s.getAllGroupProviders(w.Group, w.Type.Elem())
for _, provider := range providers {
orders = append(orders, provider.Order(gh.s))
}
}
return orders
}
// NewNode adds a new value to the graph and returns its order.
func (gh *graphHolder) NewNode(wrapped interface{}) int {
order := len(gh.nodes)
gh.nodes = append(gh.nodes, &graphNode{
Wrapped: wrapped,
})
return order
}
// Lookup retrieves the value for the node with the given order.
// Lookup panics if i is invalid.
func (gh *graphHolder) Lookup(i int) interface{} {
return gh.nodes[i].Wrapped
}
// Snapshot takes a temporary snapshot of the current state of the graph.
// Use with Rollback to undo changes to the graph.
//
// Only one snapshot is allowed at a time.
// Multiple calls to snapshot will overwrite prior snapshots.
func (gh *graphHolder) Snapshot() {
gh.snap = len(gh.nodes)
}
// Rollback rolls back a snapshot to a previously captured state.
// This is a no-op if no snapshot was captured.
func (gh *graphHolder) Rollback() {
if gh.snap < 0 {
return
}
// nodes is an append-only list. To rollback, we just drop the
// extraneous entries from the slice.
gh.nodes = gh.nodes[:gh.snap]
gh.snap = -1
}