@@ -25,7 +25,6 @@ import (
25
25
"fmt"
26
26
"io"
27
27
"strconv"
28
- "text/template"
29
28
30
29
"go.uber.org/dig/internal/dot"
31
30
)
@@ -92,46 +91,6 @@ func updateGraph(dg *dot.Graph, err error) error {
92
91
return nil
93
92
}
94
93
95
- var _graphTmpl = template .Must (
96
- template .New ("DotGraph" ).
97
- Funcs (template.FuncMap {
98
- "quote" : strconv .Quote ,
99
- }).
100
- Parse (`digraph {
101
- rankdir=RL;
102
- graph [compound=true];
103
- {{range $g := .Groups}}
104
- {{- quote .String}} [{{.Attributes}}];
105
- {{range .Results}}
106
- {{- quote $g.String}} -> {{quote .String}};
107
- {{end}}
108
- {{end -}}
109
- {{range $index, $ctor := .Ctors}}
110
- subgraph cluster_{{$index}} {
111
- {{ with .Package }}label = {{ quote .}};
112
- {{ end -}}
113
-
114
- constructor_{{$index}} [shape=plaintext label={{quote .Name}}];
115
- {{with .ErrorType}}color={{.Color}};{{end}}
116
- {{range .Results}}
117
- {{- quote .String}} [{{.Attributes}}];
118
- {{end}}
119
- }
120
- {{range .Params}}
121
- constructor_{{$index}} -> {{quote .String}} [ltail=cluster_{{$index}}{{if .Optional}} style=dashed{{end}}];
122
- {{end}}
123
- {{range .GroupParams}}
124
- constructor_{{$index}} -> {{quote .String}} [ltail=cluster_{{$index}}];
125
- {{end -}}
126
- {{end}}
127
- {{range .Failed.TransitiveFailures}}
128
- {{- quote .String}} [color=orange];
129
- {{end -}}
130
- {{range .Failed.RootCauses}}
131
- {{- quote .String}} [color=red];
132
- {{end}}
133
- }` ))
134
-
135
94
// Visualize parses the graph in Container c into DOT format and writes it to
136
95
// io.Writer w.
137
96
func Visualize (c * Container , w io.Writer , opts ... VisualizeOption ) error {
@@ -148,7 +107,66 @@ func Visualize(c *Container, w io.Writer, opts ...VisualizeOption) error {
148
107
}
149
108
}
150
109
151
- return _graphTmpl .Execute (w , dg )
110
+ visualizeGraph (w , dg )
111
+ return nil
112
+ }
113
+
114
+ func visualizeGraph (w io.Writer , dg * dot.Graph ) {
115
+ w .Write ([]byte ("digraph {\n \t rankdir=RL;\n \t graph [compound=true];\n " ))
116
+ for _ , g := range dg .Groups {
117
+ visualizeGroup (w , g )
118
+ }
119
+ w .Write ([]byte ("\t \n " ))
120
+ for idx , c := range dg .Ctors {
121
+ visualizeCtor (w , idx , c )
122
+ }
123
+ for _ , f := range dg .Failed .TransitiveFailures {
124
+ fmt .Fprintf (w , "\t %s [color=orange];\n " , strconv .Quote (f .String ()))
125
+ }
126
+ for _ , f := range dg .Failed .RootCauses {
127
+ fmt .Fprintf (w , "\t %s [color=red];\n " , strconv .Quote (f .String ()))
128
+ }
129
+ w .Write ([]byte ("\t \n }" ))
130
+ }
131
+
132
+ func visualizeGroup (w io.Writer , g * dot.Group ) {
133
+ fmt .Fprintf (w , "\t %s [%s];\n " , strconv .Quote (g .String ()), g .Attributes ())
134
+ for _ , r := range g .Results {
135
+ fmt .Fprintf (w , "\t \t %s -> %s;\n " , strconv .Quote (g .String ()), strconv .Quote (r .String ()))
136
+ }
137
+ w .Write ([]byte ("\t \t \n " ))
138
+ }
139
+
140
+ func visualizeCtor (w io.Writer , index int , c * dot.Ctor ) {
141
+ fmt .Fprintf (w , "\t \t subgraph cluster_%d {\n " , index )
142
+ w .Write ([]byte ("\t \t \t " ))
143
+ if c .Package != "" {
144
+ fmt .Fprintf (w , "label = %s;" , strconv .Quote (c .Package ))
145
+ }
146
+ w .Write ([]byte ("\n " ))
147
+ fmt .Fprintf (w , "\t \t \t constructor_%d [shape=plaintext label=%s];\n " , index , strconv .Quote (c .Name ))
148
+
149
+ w .Write ([]byte ("\t \t \t " ))
150
+ if c .ErrorType != 0 {
151
+ fmt .Fprintf (w , "color=%s;" , c .ErrorType .Color ())
152
+ }
153
+ w .Write ([]byte ("\n " ))
154
+ for _ , r := range c .Results {
155
+ fmt .Fprintf (w , "\t \t \t %s [%s];\n " , strconv .Quote (r .String ()), r .Attributes ())
156
+ }
157
+ fmt .Fprintf (w , "\t \t \t \n \t \t }\n \t \t \n " )
158
+ for _ , p := range c .Params {
159
+ var optionalStyle string
160
+ if p .Optional {
161
+ optionalStyle = " style=dashed"
162
+ }
163
+
164
+ fmt .Fprintf (w , "\t \t \t constructor_%d -> %s [ltail=cluster_%d%s];\n \t \t \n " , index , strconv .Quote (p .String ()), index , optionalStyle )
165
+ }
166
+ w .Write ([]byte ("\t \t \n " ))
167
+ for _ , p := range c .GroupParams {
168
+ fmt .Fprintf (w , "\t \t \t constructor_%d -> %s [ltail=cluster_%d];\n \t \t \n " , index , strconv .Quote (p .String ()), index )
169
+ }
152
170
}
153
171
154
172
// CanVisualizeError returns true if the error is an errVisualizer.
0 commit comments