sequence.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. Activations []*SequenceActivation `json:"activations"`
  15. Title *string `json:"title,omitempty"`
  16. Config map[string]any `json:"config,omitempty"`
  17. }
  18. type SequenceParticipant struct {
  19. ID string `json:"id"`
  20. Name string `json:"name"`
  21. Type ParticipantType `json:"type"`
  22. Alias *string `json:"alias,omitempty"`
  23. Description *string `json:"description,omitempty"`
  24. Config map[string]any `json:"config,omitempty"`
  25. }
  26. type ParticipantType string
  27. const (
  28. ParticipantTypeParticipant ParticipantType = "participant"
  29. ParticipantTypeActor ParticipantType = "actor"
  30. )
  31. type SequenceMessage struct {
  32. From string `json:"from"`
  33. To string `json:"to"`
  34. Message string `json:"message"`
  35. Type SequenceMessageType `json:"type"`
  36. Activate bool `json:"activate,omitempty"`
  37. Deactivate bool `json:"deactivate,omitempty"`
  38. Note *string `json:"note,omitempty"`
  39. }
  40. type SequenceMessageType string
  41. const (
  42. MessageTypeSolid SequenceMessageType = "->"
  43. MessageTypeDotted SequenceMessageType = "-->"
  44. MessageTypeSolidCross SequenceMessageType = "-x"
  45. MessageTypeDottedCross SequenceMessageType = "--x"
  46. MessageTypeSolidOpen SequenceMessageType = "-)"
  47. MessageTypeDottedOpen SequenceMessageType = "--)"
  48. MessageTypeBidirectional SequenceMessageType = "<->"
  49. )
  50. type SequenceLoop struct {
  51. Label string `json:"label"`
  52. Messages []*SequenceMessage `json:"messages"`
  53. StartLine int `json:"startLine"`
  54. EndLine int `json:"endLine"`
  55. }
  56. type SequenceAlt struct {
  57. Label string `json:"label"`
  58. IfMessages []*SequenceMessage `json:"ifMessages"`
  59. ElseMessages []*SequenceMessage `json:"elseMessages,omitempty"`
  60. StartLine int `json:"startLine"`
  61. EndLine int `json:"endLine"`
  62. }
  63. type SequenceOpt struct {
  64. Label string `json:"label"`
  65. Messages []*SequenceMessage `json:"messages"`
  66. StartLine int `json:"startLine"`
  67. EndLine int `json:"endLine"`
  68. }
  69. type SequencePar struct {
  70. Sections []SequenceParSection `json:"sections"`
  71. StartLine int `json:"startLine"`
  72. EndLine int `json:"endLine"`
  73. }
  74. type SequenceParSection struct {
  75. Label *string `json:"label,omitempty"`
  76. Messages []*SequenceMessage `json:"messages"`
  77. }
  78. type SequenceNote struct {
  79. Actor string `json:"actor"`
  80. Placement NotePlace `json:"placement"`
  81. Message string `json:"message"`
  82. }
  83. type NotePlace string
  84. const (
  85. NotePlaceLeft NotePlace = "left of"
  86. NotePlaceRight NotePlace = "right of"
  87. NotePlaceOver NotePlace = "over"
  88. )
  89. type SequenceBox struct {
  90. Name string `json:"name"`
  91. Color *string `json:"color,omitempty"`
  92. Participants []string `json:"participants"`
  93. }
  94. type SequenceActivation struct {
  95. Actor string `json:"actor"`
  96. Type ActivationType `json:"type"`
  97. }
  98. type ActivationType string
  99. const (
  100. ActivationTypeActivate ActivationType = "activate"
  101. ActivationTypeDeactivate ActivationType = "deactivate"
  102. )
  103. // Type returns the diagram type
  104. func (s *SequenceDiagram) Type() DiagramType {
  105. return DiagramTypeSequence
  106. }
  107. // Validate checks if the sequence diagram is valid
  108. func (s *SequenceDiagram) Validate() error {
  109. participantMap := make(map[string]bool)
  110. for _, p := range s.Participants {
  111. participantMap[p.ID] = true
  112. }
  113. // Validate messages reference valid participants
  114. for _, msg := range s.Messages {
  115. if !participantMap[msg.From] {
  116. return NewValidationError("message references non-existent participant: " + msg.From)
  117. }
  118. if !participantMap[msg.To] {
  119. return NewValidationError("message references non-existent participant: " + msg.To)
  120. }
  121. }
  122. return nil
  123. }
  124. // NewSequenceDiagram creates a new sequence diagram
  125. func NewSequenceDiagram() *SequenceDiagram {
  126. return &SequenceDiagram{
  127. Participants: make([]*SequenceParticipant, 0),
  128. Messages: make([]*SequenceMessage, 0),
  129. Loops: make([]*SequenceLoop, 0),
  130. Alts: make([]*SequenceAlt, 0),
  131. Opts: make([]*SequenceOpt, 0),
  132. Pars: make([]*SequencePar, 0),
  133. Notes: make([]*SequenceNote, 0),
  134. Boxes: make([]*SequenceBox, 0),
  135. Activations: make([]*SequenceActivation, 0),
  136. Config: make(map[string]any),
  137. }
  138. }