forked from uber-go/dig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cycle.go
142 lines (125 loc) · 4.3 KB
/
cycle.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
// Copyright (c) 2018 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 (
"bytes"
"fmt"
"go.uber.org/dig/internal/digreflect"
)
type cycleEntry struct {
Key key
Func *digreflect.Func
}
type errCycleDetected struct {
Path []cycleEntry
}
func (e errCycleDetected) Error() string {
// We get something like,
//
// foo provided by "path/to/package".NewFoo (path/to/file.go:42)
// depends on bar provided by "another/package".NewBar (somefile.go:1)
// depends on baz provided by "somepackage".NewBar (anotherfile.go:2)
// depends on foo provided by "path/to/package".NewFoo (path/to/file.go:42)
//
b := new(bytes.Buffer)
for i, entry := range e.Path {
if i > 0 {
b.WriteString("\n\tdepends on ")
}
fmt.Fprintf(b, "%v provided by %v", entry.Key, entry.Func)
}
return b.String()
}
// IsCycleDetected returns a boolean as to whether the provided error indicates
// a cycle was detected in the container graph.
func IsCycleDetected(err error) bool {
_, ok := RootCause(err).(errCycleDetected)
return ok
}
func verifyAcyclic(c containerStore, n provider, k key) error {
visited := make(map[key]struct{})
err := detectCycles(n, c, []cycleEntry{
{Key: k, Func: n.Location()},
}, visited)
if err != nil {
err = errWrapf(err, "this function introduces a cycle")
}
return err
}
func detectCycles(n provider, c containerStore, path []cycleEntry, visited map[key]struct{}) error {
var err error
walkParam(n.ParamList(), paramVisitorFunc(func(param param) bool {
if err != nil {
return false
}
var (
k key
providers []provider
)
switch p := param.(type) {
case paramSingle:
k = key{name: p.Name, t: p.Type}
if _, ok := visited[k]; ok {
// We've already checked the dependencies for this type.
return false
}
providers = c.getValueProviders(p.Name, p.Type)
case paramGroupedSlice:
// NOTE: The key uses the element type, not the slice type.
k = key{group: p.Group, t: p.Type.Elem()}
if _, ok := visited[k]; ok {
// We've already checked the dependencies for this type.
return false
}
providers = c.getGroupProviders(p.Group, p.Type.Elem())
default:
// Recurse for non-edge params.
return true
}
entry := cycleEntry{Func: n.Location(), Key: k}
if len(path) > 0 {
// Only mark a key as visited if path exists, i.e. this is not the
// first iteration through the c.verifyAcyclic() check. Otherwise the
// early exit from checking visited above will short circuit the
// cycle check below.
visited[k] = struct{}{}
// If it exists, the first element of path is the new addition to the
// graph, therefore it must be in any cycle that exists, assuming
// verifyAcyclic has been run for every previous Provide.
//
// Alternatively, if deferAcyclicVerification was set and detectCycles
// is only being called before the first Invoke, each node in the
// graph will be tested as the first element of the path, so any
// cycle that exists is guaranteed to trip the following condition.
if path[0].Key == k {
err = errCycleDetected{Path: append(path, entry)}
return false
}
}
for _, n := range providers {
if e := detectCycles(n, c, append(path, entry), visited); e != nil {
err = e
return false
}
}
return true
}))
return err
}