sequence.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Package ast defines AST structures for all Mermaid diagram types
  2. package ast
  3. // SequenceDiagram represents a sequence diagram
  4. // Based on sequenceDiagram.jison and sequenceDb.ts
  5. type SequenceDiagram struct {
  6. Participants []*SequenceParticipant `json:"participants"`
  7. Messages []*SequenceMessage `json:"messages"`
  8. Loops []*SequenceLoop `json:"loops"`
  9. Alts []*SequenceAlt `json:"alts"`
  10. Opts []*SequenceOpt `json:"opts"`
  11. Pars []*SequencePar `json:"pars"`
  12. Notes []*SequenceNote `json:"notes"`
  13. Boxes []*SequenceBox `json:"boxes"`
  14. Rects []*SequenceRect `json:"rects,omitempty"`
  15. Criticals []*SequenceCritical `json:"criticals,omitempty"`
  16. Breaks []*SequenceBreak `json:"breaks,omitempty"`
  17. Activations []*SequenceActivation `json:"activations"`
  18. Title *string `json:"title,omitempty"`
  19. Config map[string]any `json:"config,omitempty"`
  20. }
  21. type SequenceParticipant struct {
  22. ID string `json:"id"`
  23. Name string `json:"name"`
  24. Type ParticipantType `json:"type"`
  25. Alias *string `json:"alias,omitempty"`
  26. Description *string `json:"description,omitempty"`
  27. Config map[string]any `json:"config,omitempty"`
  28. }
  29. type ParticipantType string
  30. const (
  31. ParticipantTypeParticipant ParticipantType = "participant"
  32. ParticipantTypeActor ParticipantType = "actor"
  33. )
  34. type SequenceMessage struct {
  35. From string `json:"from"`
  36. To string `json:"to"`
  37. Message string `json:"message"`
  38. Type SequenceMessageType `json:"type"`
  39. Activate bool `json:"activate,omitempty"`
  40. Deactivate bool `json:"deactivate,omitempty"`
  41. Note *string `json:"note,omitempty"`
  42. }
  43. type SequenceMessageType string
  44. const (
  45. MessageTypeSolid SequenceMessageType = "->"
  46. MessageTypeDotted SequenceMessageType = "-->"
  47. MessageTypeSolidCross SequenceMessageType = "-x"
  48. MessageTypeDottedCross SequenceMessageType = "--x"
  49. MessageTypeSolidOpen SequenceMessageType = "-)"
  50. MessageTypeDottedOpen SequenceMessageType = "--)"
  51. MessageTypeBidirectional SequenceMessageType = "<->"
  52. )
  53. type SequenceLoop struct {
  54. Label string `json:"label"`
  55. Messages []*SequenceMessage `json:"messages"`
  56. StartLine int `json:"startLine"`
  57. EndLine int `json:"endLine"`
  58. }
  59. type SequenceAlt struct {
  60. Label string `json:"label"`
  61. IfMessages []*SequenceMessage `json:"ifMessages"`
  62. ElseMessages []*SequenceMessage `json:"elseMessages,omitempty"`
  63. StartLine int `json:"startLine"`
  64. EndLine int `json:"endLine"`
  65. }
  66. type SequenceOpt struct {
  67. Label string `json:"label"`
  68. Messages []*SequenceMessage `json:"messages"`
  69. StartLine int `json:"startLine"`
  70. EndLine int `json:"endLine"`
  71. }
  72. type SequencePar struct {
  73. Sections []SequenceParSection `json:"sections"`
  74. StartLine int `json:"startLine"`
  75. EndLine int `json:"endLine"`
  76. }
  77. type SequenceParSection struct {
  78. Label *string `json:"label,omitempty"`
  79. Messages []*SequenceMessage `json:"messages"`
  80. }
  81. type SequenceNote struct {
  82. Actor string `json:"actor"`
  83. Placement NotePlace `json:"placement"`
  84. Message string `json:"message"`
  85. }
  86. type NotePlace string
  87. const (
  88. NotePlaceLeft NotePlace = "left of"
  89. NotePlaceRight NotePlace = "right of"
  90. NotePlaceOver NotePlace = "over"
  91. )
  92. type SequenceBox struct {
  93. Name string `json:"name"`
  94. Color *string `json:"color,omitempty"`
  95. Participants []string `json:"participants"`
  96. }
  97. type SequenceActivation struct {
  98. Actor string `json:"actor"`
  99. Type ActivationType `json:"type"`
  100. }
  101. type ActivationType string
  102. const (
  103. ActivationTypeActivate ActivationType = "activate"
  104. ActivationTypeDeactivate ActivationType = "deactivate"
  105. )
  106. type SequenceRect struct {
  107. Color *string `json:"color,omitempty"`
  108. Messages []*SequenceMessage `json:"messages"`
  109. }
  110. type SequenceCritical struct {
  111. Label string `json:"label"`
  112. Options []*SequenceOption `json:"options"`
  113. Messages []*SequenceMessage `json:"messages"`
  114. }
  115. type SequenceOption struct {
  116. Label string `json:"label"`
  117. Messages []*SequenceMessage `json:"messages"`
  118. }
  119. type SequenceBreak struct {
  120. Label string `json:"label"`
  121. Messages []*SequenceMessage `json:"messages"`
  122. }
  123. // Type returns the diagram type
  124. func (s *SequenceDiagram) Type() DiagramType {
  125. return DiagramTypeSequence
  126. }
  127. // Validate checks if the sequence diagram is valid
  128. func (s *SequenceDiagram) Validate() error {
  129. participantMap := make(map[string]bool)
  130. for _, p := range s.Participants {
  131. participantMap[p.ID] = true
  132. }
  133. // Validate messages reference valid participants
  134. for _, msg := range s.Messages {
  135. if !participantMap[msg.From] {
  136. return NewValidationError("message references non-existent participant: " + msg.From)
  137. }
  138. if !participantMap[msg.To] {
  139. return NewValidationError("message references non-existent participant: " + msg.To)
  140. }
  141. }
  142. return nil
  143. }
  144. // NewSequenceDiagram creates a new sequence diagram
  145. func NewSequenceDiagram() *SequenceDiagram {
  146. return &SequenceDiagram{
  147. Participants: make([]*SequenceParticipant, 0),
  148. Messages: make([]*SequenceMessage, 0),
  149. Loops: make([]*SequenceLoop, 0),
  150. Alts: make([]*SequenceAlt, 0),
  151. Opts: make([]*SequenceOpt, 0),
  152. Pars: make([]*SequencePar, 0),
  153. Notes: make([]*SequenceNote, 0),
  154. Boxes: make([]*SequenceBox, 0),
  155. Rects: make([]*SequenceRect, 0),
  156. Criticals: make([]*SequenceCritical, 0),
  157. Breaks: make([]*SequenceBreak, 0),
  158. Activations: make([]*SequenceActivation, 0),
  159. Config: make(map[string]any),
  160. }
  161. }