@@ -33,6 +33,7 @@ func NewMermaidWriter(opts ...MermaidOption) *MermaidWriter {
33
33
m := & MermaidWriter {
34
34
MinEdgeName : minEdgeName ,
35
35
SpecifiedPackageName : specifiedPackageName ,
36
+ UseV0Semantics : true ,
36
37
}
37
38
38
39
for _ , opt := range opts {
@@ -53,9 +54,9 @@ func WithSpecifiedPackageName(specifiedPackageName string) MermaidOption {
53
54
}
54
55
}
55
56
56
- func WithV0Semantics () MermaidOption {
57
+ func WithV0Semantics (useV0Semantics bool ) MermaidOption {
57
58
return func (o * MermaidWriter ) {
58
- o .UseV0Semantics = true
59
+ o .UseV0Semantics = useV0Semantics
59
60
}
60
61
}
61
62
@@ -131,7 +132,8 @@ func (writer *MermaidWriter) WriteChannels(cfg DeclarativeConfig, out io.Writer)
131
132
}
132
133
133
134
var deprecatedPackage string
134
- deprecatedChannels := []string {}
135
+ deprecatedChannelIDs := []string {}
136
+ decoratedBundleIDs := map [string ][]string {"deprecated" : {}, "skipped" : {}, "deprecatedskipped" : {}}
135
137
linkID := 0
136
138
skippedLinkIDs := []string {}
137
139
@@ -154,7 +156,7 @@ func (writer *MermaidWriter) WriteChannels(cfg DeclarativeConfig, out io.Writer)
154
156
}
155
157
156
158
if depByChannel .Has (filteredChannel .Name ) {
157
- deprecatedChannels = append (deprecatedChannels , channelID )
159
+ deprecatedChannelIDs = append (deprecatedChannelIDs , channelID )
158
160
}
159
161
160
162
// sort edges by decreasing version
@@ -169,29 +171,45 @@ func (writer *MermaidWriter) WriteChannels(cfg DeclarativeConfig, out io.Writer)
169
171
170
172
skippedEntities := sets.Set [string ]{}
171
173
174
+ const (
175
+ captureNewEntry = true
176
+ processExisting = false
177
+ )
178
+ handleSemantics := func (edge string , linkID int , captureNew bool ) {
179
+ if writer .UseV0Semantics {
180
+ if captureNew {
181
+ if skippedEntities .Has (edge ) {
182
+ skippedLinkIDs = append (skippedLinkIDs , fmt .Sprintf ("%d" , linkID ))
183
+ } else {
184
+ skippedEntities .Insert (edge )
185
+ }
186
+ } else {
187
+ if skippedEntities .Has (edge ) {
188
+ skippedLinkIDs = append (skippedLinkIDs , fmt .Sprintf ("%d" , linkID ))
189
+ }
190
+ }
191
+ }
192
+ }
193
+
172
194
for _ , ce := range sortedEntries {
173
- bundleDecoration := ""
195
+ entryID := fmt .Sprintf ("%s-%s" , channelID , ce .Name )
196
+ fmt .Fprintf (pkgBuilder , " %s[%q]\n " , entryID , ce .Name )
197
+
198
+ // mermaid allows specification of only a single decoration class, so any combinations must be independently represented
174
199
switch {
175
200
case depByBundle .Has (ce .Name ) && skippedEntities .Has (ce .Name ):
176
- bundleDecoration = ":::depandskip"
201
+ decoratedBundleIDs [ "deprecatedskipped" ] = append ( decoratedBundleIDs [ "deprecatedskipped" ], entryID )
177
202
case depByBundle .Has (ce .Name ):
178
- bundleDecoration = "::: deprecated"
203
+ decoratedBundleIDs [ "deprecated" ] = append ( decoratedBundleIDs [ " deprecated"], entryID )
179
204
case skippedEntities .Has (ce .Name ):
180
- bundleDecoration = "::: skipped"
205
+ decoratedBundleIDs [ "skipped" ] = append ( decoratedBundleIDs [ " skipped"], entryID )
181
206
}
182
207
183
- entryID := fmt .Sprintf ("%s-%s" , channelID , ce .Name )
184
- fmt .Fprintf (pkgBuilder , " %s[%q]%s\n " , entryID , ce .Name , bundleDecoration )
185
-
186
208
if len (ce .Skips ) > 0 {
187
209
for _ , s := range ce .Skips {
188
210
skipsID := fmt .Sprintf ("%s-%s" , channelID , s )
189
211
fmt .Fprintf (pkgBuilder , " %s[%q]-- %s --> %s[%q]\n " , skipsID , s , "skip" , entryID , ce .Name )
190
- if skippedEntities .Has (s ) {
191
- skippedLinkIDs = append (skippedLinkIDs , fmt .Sprintf ("%d" , linkID ))
192
- } else {
193
- skippedEntities .Insert (s )
194
- }
212
+ handleSemantics (s , linkID , captureNewEntry )
195
213
linkID ++
196
214
}
197
215
}
@@ -202,9 +220,7 @@ func (writer *MermaidWriter) WriteChannels(cfg DeclarativeConfig, out io.Writer)
202
220
if skipRange (versionMap [edgeName .Name ]) {
203
221
skipRangeID := fmt .Sprintf ("%s-%s" , channelID , edgeName .Name )
204
222
fmt .Fprintf (pkgBuilder , " %s[%q]-- \" %s(%s)\" --> %s[%q]\n " , skipRangeID , edgeName .Name , "skipRange" , ce .SkipRange , entryID , ce .Name )
205
- if skippedEntities .Has (ce .Name ) {
206
- skippedLinkIDs = append (skippedLinkIDs , fmt .Sprintf ("%d" , linkID ))
207
- }
223
+ handleSemantics (ce .Name , linkID , processExisting )
208
224
linkID ++
209
225
}
210
226
}
@@ -216,9 +232,7 @@ func (writer *MermaidWriter) WriteChannels(cfg DeclarativeConfig, out io.Writer)
216
232
if len (ce .Replaces ) > 0 {
217
233
replacesID := fmt .Sprintf ("%s-%s" , channelID , ce .Replaces )
218
234
fmt .Fprintf (pkgBuilder , " %s[%q]-- %s --> %s[%q]\n " , replacesID , ce .Replaces , "replace" , entryID , ce .Name )
219
- if skippedEntities .Has (ce .Name ) {
220
- skippedLinkIDs = append (skippedLinkIDs , fmt .Sprintf ("%d" , linkID ))
221
- }
235
+ handleSemantics (ce .Name , linkID , processExisting )
222
236
linkID ++
223
237
}
224
238
}
@@ -229,7 +243,7 @@ func (writer *MermaidWriter) WriteChannels(cfg DeclarativeConfig, out io.Writer)
229
243
_ , _ = out .Write ([]byte ("graph LR\n " ))
230
244
_ , _ = out .Write ([]byte (" classDef deprecated fill:#E8960F\n " ))
231
245
_ , _ = out .Write ([]byte (" classDef skipped stroke:#FF0000,stroke-width:4px\n " ))
232
- _ , _ = out .Write ([]byte (" classDef depandskip fill:#E8960 ,stroke:#FF0000,stroke-width:4px\n " ))
246
+ _ , _ = out .Write ([]byte (" classDef deprecatedskipped fill:#E8960F ,stroke:#FF0000,stroke-width:4px\n " ))
233
247
pkgNames := []string {}
234
248
for pname := range pkgs {
235
249
pkgNames = append (pkgNames , pname )
@@ -248,14 +262,21 @@ func (writer *MermaidWriter) WriteChannels(cfg DeclarativeConfig, out io.Writer)
248
262
_ , _ = out .Write ([]byte (fmt .Sprintf ("style %s fill:#989695\n " , deprecatedPackage )))
249
263
}
250
264
251
- if len (deprecatedChannels ) > 0 {
252
- for _ , deprecatedChannel := range deprecatedChannels {
265
+ if len (deprecatedChannelIDs ) > 0 {
266
+ for _ , deprecatedChannel := range deprecatedChannelIDs {
253
267
_ , _ = out .Write ([]byte (fmt .Sprintf ("style %s fill:#DCD0FF\n " , deprecatedChannel )))
254
268
}
255
269
}
256
270
271
+ // express the decoration classes
272
+ for key := range decoratedBundleIDs {
273
+ if len (decoratedBundleIDs [key ]) > 0 {
274
+ _ , _ = out .Write ([]byte (fmt .Sprintf ("class %s %s\n " , strings .Join (decoratedBundleIDs [key ], "," ), key )))
275
+ }
276
+ }
277
+
257
278
if len (skippedLinkIDs ) > 0 {
258
- _ , _ = out .Write ([]byte ("linkStyle " + strings .Join (skippedLinkIDs , "," ) + " stroke:#FF0000,stroke-width:3px,stroke-dasharray:5;" ))
279
+ _ , _ = out .Write ([]byte ("linkStyle " + strings .Join (skippedLinkIDs , "," ) + " stroke:#FF0000,stroke-width:3px,stroke-dasharray:5;\n " ))
259
280
}
260
281
261
282
return nil
0 commit comments