-
Notifications
You must be signed in to change notification settings - Fork 208
/
visualize.go
200 lines (171 loc) · 4.99 KB
/
visualize.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// 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 (
"errors"
"fmt"
"io"
"strconv"
"text/template"
"go.uber.org/dig/internal/dot"
)
// A VisualizeOption modifies the default behavior of Visualize.
type VisualizeOption interface {
applyVisualizeOption(*visualizeOptions)
}
type visualizeOptions struct {
VisualizeError error
}
// VisualizeError includes a visualization of the given error in the output of
// Visualize if an error was returned by Invoke or Provide.
//
// if err := c.Provide(...); err != nil {
// dig.Visualize(c, w, dig.VisualizeError(err))
// }
//
// This option has no effect if the error was nil or if it didn't contain any
// information to visualize.
func VisualizeError(err error) VisualizeOption {
return visualizeErrorOption{err}
}
type visualizeErrorOption struct{ err error }
func (o visualizeErrorOption) String() string {
return fmt.Sprintf("VisualizeError(%v)", o.err)
}
func (o visualizeErrorOption) applyVisualizeOption(opt *visualizeOptions) {
opt.VisualizeError = o.err
}
func updateGraph(dg *dot.Graph, err error) error {
var errs []errVisualizer
// Unwrap error to find the root cause.
for {
if ev, ok := err.(errVisualizer); ok {
errs = append(errs, ev)
}
e := errors.Unwrap(err)
if e == nil {
break
}
err = e
}
// If there are no errVisualizers included, we do not modify the graph.
if len(errs) == 0 {
return nil
}
// We iterate in reverse because the last element is the root cause.
for i := len(errs) - 1; i >= 0; i-- {
errs[i].updateGraph(dg)
}
// Remove non-error entries from the graph for readability.
dg.PruneSuccess()
return nil
}
var _graphTmpl = template.Must(
template.New("DotGraph").
Funcs(template.FuncMap{
"quote": strconv.Quote,
}).
Parse(`digraph {
rankdir=RL;
graph [compound=true];
{{range $g := .Groups}}
{{- quote .String}} [{{.Attributes}}];
{{range .Results}}
{{- quote $g.String}} -> {{quote .String}};
{{end}}
{{end -}}
{{range $index, $ctor := .Ctors}}
subgraph cluster_{{$index}} {
{{ with .Package }}label = {{ quote .}};
{{ end -}}
constructor_{{$index}} [shape=plaintext label={{quote .Name}}];
{{with .ErrorType}}color={{.Color}};{{end}}
{{range .Results}}
{{- quote .String}} [{{.Attributes}}];
{{end}}
}
{{range .Params}}
constructor_{{$index}} -> {{quote .String}} [ltail=cluster_{{$index}}{{if .Optional}} style=dashed{{end}}];
{{end}}
{{range .GroupParams}}
constructor_{{$index}} -> {{quote .String}} [ltail=cluster_{{$index}}];
{{end -}}
{{end}}
{{range .Failed.TransitiveFailures}}
{{- quote .String}} [color=orange];
{{end -}}
{{range .Failed.RootCauses}}
{{- quote .String}} [color=red];
{{end}}
}`))
// Visualize parses the graph in Container c into DOT format and writes it to
// io.Writer w.
func Visualize(c *Container, w io.Writer, opts ...VisualizeOption) error {
dg := c.createGraph()
var options visualizeOptions
for _, o := range opts {
o.applyVisualizeOption(&options)
}
if options.VisualizeError != nil {
if err := updateGraph(dg, options.VisualizeError); err != nil {
return err
}
}
return _graphTmpl.Execute(w, dg)
}
// CanVisualizeError returns true if the error is an errVisualizer.
func CanVisualizeError(err error) bool {
for {
if _, ok := err.(errVisualizer); ok {
return true
}
e := errors.Unwrap(err)
if e == nil {
break
}
err = e
}
return false
}
func (c *Container) createGraph() *dot.Graph {
return c.scope.createGraph()
}
func (s *Scope) createGraph() *dot.Graph {
dg := dot.NewGraph()
s.addNodes(dg)
return dg
}
func (s *Scope) addNodes(dg *dot.Graph) {
for _, n := range s.nodes {
dg.AddCtor(newDotCtor(n), n.paramList.DotParam(), n.resultList.DotResult())
}
for _, cs := range s.childScopes {
cs.addNodes(dg)
}
}
func newDotCtor(n *constructorNode) *dot.Ctor {
return &dot.Ctor{
ID: n.id,
Name: n.location.Name,
Package: n.location.Package,
File: n.location.File,
Line: n.location.Line,
}
}