fsm: make diagram generation deterministic

This commit is contained in:
Gustavo Stingelin 2026-07-18 15:30:49 -03:00
parent 39c97e60d2
commit 1d5b1c564a
3 changed files with 37 additions and 17 deletions

View file

@ -2,11 +2,11 @@
stateDiagram-v2
[*] --> InitFSM: OnRequestStuff
InitFSM
InitFSM --> StuffSentOut: OnStuffSentOut
InitFSM --> StuffFailed: OnError
InitFSM --> StuffSentOut: OnStuffSentOut
StuffFailed
StuffSentOut
StuffSentOut --> StuffSuccess: OnStuffSuccess
StuffSentOut --> StuffFailed: OnError
StuffSentOut --> StuffSuccess: OnStuffSuccess
StuffSuccess
```

View file

@ -86,8 +86,11 @@ func writeMermaidFile(filename string, states fsm.States) error {
state = "[*]"
}
// write transitions
for edge, target := range edges.Transitions {
fmt.Fprintf(&b, "%s --> %s: %s\n", state, target, edge)
for _, edge := range sortedTransitionKeys(edges.Transitions) {
fmt.Fprintf(
&b, "%s --> %s: %s\n", state,
edges.Transitions[fsm.EventType(edge)], edge,
)
}
}
@ -110,3 +113,14 @@ func sortedKeys(m fsm.States) []string {
sort.Strings(keys)
return keys
}
func sortedTransitionKeys(m fsm.Transitions) []string {
keys := make([]string, len(m))
i := 0
for k := range m {
keys[i] = string(k)
i++
}
sort.Strings(keys)
return keys
}