Skip to content

Commit 1190017

Browse files
authored
Replace use of text/template with string manipulations (#425)
feat: replace template with strings
1 parent a6503f1 commit 1190017

File tree

1 file changed

+60
-42
lines changed

1 file changed

+60
-42
lines changed

visualize.go

Lines changed: 60 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"fmt"
2626
"io"
2727
"strconv"
28-
"text/template"
2928

3029
"go.uber.org/dig/internal/dot"
3130
)
@@ -92,46 +91,6 @@ func updateGraph(dg *dot.Graph, err error) error {
9291
return nil
9392
}
9493

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-
13594
// Visualize parses the graph in Container c into DOT format and writes it to
13695
// io.Writer w.
13796
func Visualize(c *Container, w io.Writer, opts ...VisualizeOption) error {
@@ -148,7 +107,66 @@ func Visualize(c *Container, w io.Writer, opts ...VisualizeOption) error {
148107
}
149108
}
150109

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\trankdir=RL;\n\tgraph [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\tsubgraph 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\tconstructor_%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\tconstructor_%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\tconstructor_%d -> %s [ltail=cluster_%d];\n\t\t\n", index, strconv.Quote(p.String()), index)
169+
}
152170
}
153171

154172
// CanVisualizeError returns true if the error is an errVisualizer.

0 commit comments

Comments
 (0)