flowchart.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Package ast defines the Abstract Syntax Tree structures for Mermaid diagrams.
  2. // This package mirrors the TypeScript types from mermaid.js for compatibility.
  3. package ast
  4. // DiagramType represents the type of mermaid diagram
  5. type DiagramType string
  6. const (
  7. DiagramTypeFlowchart DiagramType = "flowchart"
  8. DiagramTypeSequence DiagramType = "sequenceDiagram"
  9. DiagramTypeClassDiagram DiagramType = "classDiagram"
  10. DiagramTypeStateDiagram DiagramType = "stateDiagram"
  11. DiagramTypeERDiagram DiagramType = "erDiagram"
  12. DiagramTypeUserJourney DiagramType = "journey"
  13. DiagramTypeTimeline DiagramType = "timeline"
  14. DiagramTypeBlock DiagramType = "block"
  15. DiagramTypeGantt DiagramType = "gantt"
  16. DiagramTypePie DiagramType = "pie"
  17. DiagramTypeQuadrant DiagramType = "quadrantChart"
  18. DiagramTypeRequirement DiagramType = "requirementDiagram"
  19. )
  20. // Diagram is the base interface for all mermaid diagrams
  21. type Diagram interface {
  22. Type() DiagramType
  23. Validate() error
  24. }
  25. // FlowVertexTypeParam represents valid vertex types from flow.jison
  26. // Corresponds to FlowVertexTypeParam in mermaid.js
  27. type FlowVertexTypeParam string
  28. const (
  29. VertexTypeSquare FlowVertexTypeParam = "square"
  30. VertexTypeDoubleCircle FlowVertexTypeParam = "doublecircle"
  31. VertexTypeCircle FlowVertexTypeParam = "circle"
  32. VertexTypeEllipse FlowVertexTypeParam = "ellipse"
  33. VertexTypeStadium FlowVertexTypeParam = "stadium"
  34. VertexTypeSubroutine FlowVertexTypeParam = "subroutine"
  35. VertexTypeRect FlowVertexTypeParam = "rect"
  36. VertexTypeCylinder FlowVertexTypeParam = "cylinder"
  37. VertexTypeRound FlowVertexTypeParam = "round"
  38. VertexTypeDiamond FlowVertexTypeParam = "diamond"
  39. VertexTypeHexagon FlowVertexTypeParam = "hexagon"
  40. VertexTypeOdd FlowVertexTypeParam = "odd"
  41. VertexTypeTrapezoid FlowVertexTypeParam = "trapezoid"
  42. VertexTypeInvTrapezoid FlowVertexTypeParam = "inv_trapezoid"
  43. VertexTypeLeanRight FlowVertexTypeParam = "lean_right"
  44. VertexTypeLeanLeft FlowVertexTypeParam = "lean_left"
  45. )
  46. // FlowVertex represents a node in a flowchart
  47. // Mirrors the FlowVertex interface from mermaid.js
  48. type FlowVertex struct {
  49. ID string `json:"id"`
  50. Classes []string `json:"classes"`
  51. Dir *string `json:"dir,omitempty"`
  52. DomID string `json:"domId"`
  53. HaveCallback *bool `json:"haveCallback,omitempty"`
  54. LabelType string `json:"labelType"` // Always "text" in original
  55. Link *string `json:"link,omitempty"`
  56. LinkTarget *string `json:"linkTarget,omitempty"`
  57. Props map[string]any `json:"props,omitempty"`
  58. Styles []string `json:"styles"`
  59. Text *string `json:"text,omitempty"`
  60. Type *FlowVertexTypeParam `json:"type,omitempty"`
  61. Icon *string `json:"icon,omitempty"`
  62. Form *string `json:"form,omitempty"`
  63. Pos *string `json:"pos,omitempty"` // 't' or 'b'
  64. Img *string `json:"img,omitempty"`
  65. AssetWidth *int `json:"assetWidth,omitempty"`
  66. AssetHeight *int `json:"assetHeight,omitempty"`
  67. DefaultWidth *int `json:"defaultWidth,omitempty"`
  68. ImageAspectRatio *float64 `json:"imageAspectRatio,omitempty"`
  69. Constraint *string `json:"constraint,omitempty"` // "on" or "off"
  70. }
  71. // FlowText represents text content in diagrams
  72. type FlowText struct {
  73. Text string `json:"text"`
  74. Type string `json:"type"` // Always "text"
  75. }
  76. // FlowEdgeStroke represents edge stroke types
  77. type FlowEdgeStroke string
  78. const (
  79. StrokeNormal FlowEdgeStroke = "normal"
  80. StrokeThick FlowEdgeStroke = "thick"
  81. StrokeInvisible FlowEdgeStroke = "invisible"
  82. StrokeDotted FlowEdgeStroke = "dotted"
  83. )
  84. // FlowEdge represents a connection between vertices
  85. // Mirrors the FlowEdge interface from mermaid.js
  86. type FlowEdge struct {
  87. ID string `json:"id,omitempty"`
  88. IsUserDefinedID bool `json:"isUserDefinedId"`
  89. Start string `json:"start"`
  90. End string `json:"end"`
  91. Interpolate *string `json:"interpolate,omitempty"`
  92. Type *string `json:"type,omitempty"`
  93. Stroke *FlowEdgeStroke `json:"stroke,omitempty"`
  94. Style []string `json:"style,omitempty"`
  95. Length *int `json:"length,omitempty"`
  96. Text string `json:"text"`
  97. LabelType string `json:"labelType"` // Always "text"
  98. Classes []string `json:"classes"`
  99. Animation *string `json:"animation,omitempty"` // "fast" or "slow"
  100. Animate *bool `json:"animate,omitempty"`
  101. }
  102. // FlowClass represents CSS class definitions
  103. type FlowClass struct {
  104. ID string `json:"id"`
  105. Styles []string `json:"styles"`
  106. TextStyles []string `json:"textStyles"`
  107. }
  108. // FlowSubGraph represents a subgraph container
  109. type FlowSubGraph struct {
  110. ID string `json:"id"`
  111. Classes []string `json:"classes"`
  112. Dir *string `json:"dir,omitempty"`
  113. LabelType string `json:"labelType"`
  114. Nodes []string `json:"nodes"`
  115. Title string `json:"title"`
  116. }
  117. // FlowLink represents link connection details
  118. type FlowLink struct {
  119. Length *int `json:"length,omitempty"`
  120. Stroke string `json:"stroke"`
  121. Type string `json:"type"`
  122. Text *string `json:"text,omitempty"`
  123. }
  124. // Flowchart represents a complete flowchart diagram
  125. // Mirrors the FlowDB state in mermaid.js
  126. type Flowchart struct {
  127. Direction string `json:"direction,omitempty"`
  128. Vertices map[string]*FlowVertex `json:"vertices"`
  129. Edges []*FlowEdge `json:"edges"`
  130. Classes map[string]*FlowClass `json:"classes"`
  131. SubGraphs []*FlowSubGraph `json:"subGraphs"`
  132. SubGraphLookup map[string]*FlowSubGraph `json:"subGraphLookup"`
  133. Tooltips map[string]string `json:"tooltips"`
  134. Version string `json:"version"` // "gen-1" or "gen-2"
  135. }
  136. // Type returns the diagram type
  137. func (f *Flowchart) Type() DiagramType {
  138. return DiagramTypeFlowchart
  139. }
  140. // Validate checks if the flowchart is valid
  141. func (f *Flowchart) Validate() error {
  142. // Basic validation - ensure all edges reference valid vertices
  143. for _, edge := range f.Edges {
  144. if _, exists := f.Vertices[edge.Start]; !exists {
  145. return NewValidationError("edge references non-existent start vertex: " + edge.Start)
  146. }
  147. if _, exists := f.Vertices[edge.End]; !exists {
  148. return NewValidationError("edge references non-existent end vertex: " + edge.End)
  149. }
  150. }
  151. return nil
  152. }
  153. // ValidationError represents a diagram validation error
  154. type ValidationError struct {
  155. Message string
  156. }
  157. func (e *ValidationError) Error() string {
  158. return "validation error: " + e.Message
  159. }
  160. // NewValidationError creates a new validation error
  161. func NewValidationError(message string) *ValidationError {
  162. return &ValidationError{Message: message}
  163. }
  164. // NewFlowchart creates a new flowchart with default values
  165. func NewFlowchart() *Flowchart {
  166. return &Flowchart{
  167. Vertices: make(map[string]*FlowVertex),
  168. Edges: make([]*FlowEdge, 0),
  169. Classes: make(map[string]*FlowClass),
  170. SubGraphs: make([]*FlowSubGraph, 0),
  171. SubGraphLookup: make(map[string]*FlowSubGraph),
  172. Tooltips: make(map[string]string),
  173. Version: "gen-2",
  174. }
  175. }