@@ -33,6 +33,7 @@ func NewMermaidWriter(opts ...MermaidOption) *MermaidWriter {
3333 m := & MermaidWriter {
3434 MinEdgeName : minEdgeName ,
3535 SpecifiedPackageName : specifiedPackageName ,
36+ UseV0Semantics : true ,
3637 }
3738
3839 for _ , opt := range opts {
@@ -53,9 +54,9 @@ func WithSpecifiedPackageName(specifiedPackageName string) MermaidOption {
5354 }
5455}
5556
56- func WithV0Semantics () MermaidOption {
57+ func WithV0Semantics (useV0Semantics bool ) MermaidOption {
5758 return func (o * MermaidWriter ) {
58- o .UseV0Semantics = true
59+ o .UseV0Semantics = useV0Semantics
5960 }
6061}
6162
@@ -131,7 +132,8 @@ func (writer *MermaidWriter) WriteChannels(cfg DeclarativeConfig, out io.Writer)
131132 }
132133
133134 var deprecatedPackage string
134- deprecatedChannels := []string {}
135+ deprecatedChannelIDs := []string {}
136+ decoratedBundleIDs := map [string ][]string {"deprecated" : {}, "skipped" : {}, "deprecatedskipped" : {}}
135137 linkID := 0
136138 skippedLinkIDs := []string {}
137139
@@ -154,7 +156,7 @@ func (writer *MermaidWriter) WriteChannels(cfg DeclarativeConfig, out io.Writer)
154156 }
155157
156158 if depByChannel .Has (filteredChannel .Name ) {
157- deprecatedChannels = append (deprecatedChannels , channelID )
159+ deprecatedChannelIDs = append (deprecatedChannelIDs , channelID )
158160 }
159161
160162 // sort edges by decreasing version
@@ -169,29 +171,44 @@ func (writer *MermaidWriter) WriteChannels(cfg DeclarativeConfig, out io.Writer)
169171
170172 skippedEntities := sets.Set [string ]{}
171173
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+
172194 for _ , ce := range sortedEntries {
173- bundleDecoration := ""
195+ entryID := fmt .Sprintf ("%s-%s" , channelID , ce .Name )
196+
197+ // mermaid allows specification of only a single decoration class, so any combinations must be independently represented
174198 switch {
175199 case depByBundle .Has (ce .Name ) && skippedEntities .Has (ce .Name ):
176- bundleDecoration = ":::depandskip"
200+ decoratedBundleIDs [ "deprecatedskipped" ] = append ( decoratedBundleIDs [ "deprecatedskipped" ], entryID )
177201 case depByBundle .Has (ce .Name ):
178- bundleDecoration = "::: deprecated"
202+ decoratedBundleIDs [ "deprecated" ] = append ( decoratedBundleIDs [ " deprecated"], entryID )
179203 case skippedEntities .Has (ce .Name ):
180- bundleDecoration = "::: skipped"
204+ decoratedBundleIDs [ "skipped" ] = append ( decoratedBundleIDs [ " skipped"], entryID )
181205 }
182206
183- entryID := fmt .Sprintf ("%s-%s" , channelID , ce .Name )
184- fmt .Fprintf (pkgBuilder , " %s[%q]%s\n " , entryID , ce .Name , bundleDecoration )
185-
186207 if len (ce .Skips ) > 0 {
187208 for _ , s := range ce .Skips {
188209 skipsID := fmt .Sprintf ("%s-%s" , channelID , s )
189210 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- }
211+ handleSemantics (s , linkID , captureNewEntry )
195212 linkID ++
196213 }
197214 }
@@ -202,9 +219,7 @@ func (writer *MermaidWriter) WriteChannels(cfg DeclarativeConfig, out io.Writer)
202219 if skipRange (versionMap [edgeName .Name ]) {
203220 skipRangeID := fmt .Sprintf ("%s-%s" , channelID , edgeName .Name )
204221 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- }
222+ handleSemantics (ce .Name , linkID , processExisting )
208223 linkID ++
209224 }
210225 }
@@ -216,9 +231,7 @@ func (writer *MermaidWriter) WriteChannels(cfg DeclarativeConfig, out io.Writer)
216231 if len (ce .Replaces ) > 0 {
217232 replacesID := fmt .Sprintf ("%s-%s" , channelID , ce .Replaces )
218233 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- }
234+ handleSemantics (ce .Name , linkID , processExisting )
222235 linkID ++
223236 }
224237 }
@@ -229,7 +242,7 @@ func (writer *MermaidWriter) WriteChannels(cfg DeclarativeConfig, out io.Writer)
229242 _ , _ = out .Write ([]byte ("graph LR\n " ))
230243 _ , _ = out .Write ([]byte (" classDef deprecated fill:#E8960F\n " ))
231244 _ , _ = 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 " ))
245+ _ , _ = out .Write ([]byte (" classDef deprecatedskipped fill:#E8960F ,stroke:#FF0000,stroke-width:4px\n " ))
233246 pkgNames := []string {}
234247 for pname := range pkgs {
235248 pkgNames = append (pkgNames , pname )
@@ -248,12 +261,19 @@ func (writer *MermaidWriter) WriteChannels(cfg DeclarativeConfig, out io.Writer)
248261 _ , _ = out .Write ([]byte (fmt .Sprintf ("style %s fill:#989695\n " , deprecatedPackage )))
249262 }
250263
251- if len (deprecatedChannels ) > 0 {
252- for _ , deprecatedChannel := range deprecatedChannels {
264+ if len (deprecatedChannelIDs ) > 0 {
265+ for _ , deprecatedChannel := range deprecatedChannelIDs {
253266 _ , _ = out .Write ([]byte (fmt .Sprintf ("style %s fill:#DCD0FF\n " , deprecatedChannel )))
254267 }
255268 }
256269
270+ // express the decoration classes
271+ for key := range decoratedBundleIDs {
272+ if len (decoratedBundleIDs [key ]) > 0 {
273+ _ , _ = out .Write ([]byte (fmt .Sprintf ("class %s %s\n " , strings .Join (decoratedBundleIDs [key ], "," ), key )))
274+ }
275+ }
276+
257277 if len (skippedLinkIDs ) > 0 {
258278 _ , _ = out .Write ([]byte ("linkStyle " + strings .Join (skippedLinkIDs , "," ) + " stroke:#FF0000,stroke-width:3px,stroke-dasharray:5;" ))
259279 }
0 commit comments