c4.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // C4 diagram AST structures based on C4 model
  2. package ast
  3. // C4Diagram represents a base C4 diagram
  4. type C4Diagram struct {
  5. Title *string `json:"title,omitempty"`
  6. Elements []*C4Element `json:"elements"`
  7. Relations []*C4Relation `json:"relations"`
  8. Boundaries []*C4Boundary `json:"boundaries"`
  9. Config map[string]any `json:"config,omitempty"`
  10. }
  11. // C4Element represents a C4 element (Person, System, Container, Component)
  12. type C4Element struct {
  13. ID string `json:"id"`
  14. Type C4ElementType `json:"type"`
  15. Name string `json:"name"`
  16. Description string `json:"description"`
  17. Technology *string `json:"technology,omitempty"`
  18. Properties map[string]any `json:"properties,omitempty"`
  19. }
  20. // C4ElementType represents the type of C4 element
  21. type C4ElementType string
  22. const (
  23. C4ElementPerson C4ElementType = "person"
  24. C4ElementSystem C4ElementType = "system"
  25. C4ElementContainer C4ElementType = "container"
  26. C4ElementComponent C4ElementType = "component"
  27. C4ElementDatabase C4ElementType = "database"
  28. C4ElementQueue C4ElementType = "queue"
  29. C4ElementExternal C4ElementType = "external"
  30. )
  31. // C4Relation represents a relationship between C4 elements
  32. type C4Relation struct {
  33. From string `json:"from"`
  34. To string `json:"to"`
  35. Label *string `json:"label,omitempty"`
  36. Technology *string `json:"technology,omitempty"`
  37. Type C4RelationType `json:"type"`
  38. Properties map[string]any `json:"properties,omitempty"`
  39. }
  40. // C4RelationType represents the type of C4 relation
  41. type C4RelationType string
  42. const (
  43. C4RelationSync C4RelationType = "sync"
  44. C4RelationAsync C4RelationType = "async"
  45. C4RelationWebSocket C4RelationType = "websocket"
  46. )
  47. // C4Boundary represents a boundary in C4 diagrams
  48. type C4Boundary struct {
  49. ID string `json:"id"`
  50. Type C4BoundaryType `json:"type"`
  51. Name string `json:"name"`
  52. Description *string `json:"description,omitempty"`
  53. Elements []string `json:"elements"` // IDs of elements within this boundary
  54. }
  55. // C4BoundaryType represents the type of C4 boundary
  56. type C4BoundaryType string
  57. const (
  58. C4BoundarySystem C4BoundaryType = "system"
  59. C4BoundaryContainer C4BoundaryType = "container"
  60. )
  61. // C4ContextDiagram represents a C4 Context diagram
  62. type C4ContextDiagram struct {
  63. C4Diagram
  64. }
  65. // Type returns the diagram type
  66. func (c *C4ContextDiagram) Type() DiagramType {
  67. return DiagramTypeC4Context
  68. }
  69. // Validate checks if the C4 Context diagram is valid
  70. func (c *C4ContextDiagram) Validate() error {
  71. // Basic validation - ensure all relations reference valid elements
  72. elementMap := make(map[string]bool)
  73. for _, element := range c.Elements {
  74. elementMap[element.ID] = true
  75. }
  76. for _, relation := range c.Relations {
  77. if !elementMap[relation.From] {
  78. return NewValidationError("relation references non-existent element: " + relation.From)
  79. }
  80. if !elementMap[relation.To] {
  81. return NewValidationError("relation references non-existent element: " + relation.To)
  82. }
  83. }
  84. return nil
  85. }
  86. // NewC4ContextDiagram creates a new C4 Context diagram
  87. func NewC4ContextDiagram() *C4ContextDiagram {
  88. return &C4ContextDiagram{
  89. C4Diagram: C4Diagram{
  90. Elements: make([]*C4Element, 0),
  91. Relations: make([]*C4Relation, 0),
  92. Boundaries: make([]*C4Boundary, 0),
  93. Config: make(map[string]any),
  94. },
  95. }
  96. }
  97. // C4ContainerDiagram represents a C4 Container diagram
  98. type C4ContainerDiagram struct {
  99. C4Diagram
  100. }
  101. // Type returns the diagram type
  102. func (c *C4ContainerDiagram) Type() DiagramType {
  103. return DiagramTypeC4Container
  104. }
  105. // Validate checks if the C4 Container diagram is valid
  106. func (c *C4ContainerDiagram) Validate() error {
  107. return c.C4Diagram.validate()
  108. }
  109. // NewC4ContainerDiagram creates a new C4 Container diagram
  110. func NewC4ContainerDiagram() *C4ContainerDiagram {
  111. return &C4ContainerDiagram{
  112. C4Diagram: C4Diagram{
  113. Elements: make([]*C4Element, 0),
  114. Relations: make([]*C4Relation, 0),
  115. Boundaries: make([]*C4Boundary, 0),
  116. Config: make(map[string]any),
  117. },
  118. }
  119. }
  120. // C4ComponentDiagram represents a C4 Component diagram
  121. type C4ComponentDiagram struct {
  122. C4Diagram
  123. }
  124. // Type returns the diagram type
  125. func (c *C4ComponentDiagram) Type() DiagramType {
  126. return DiagramTypeC4Component
  127. }
  128. // Validate checks if the C4 Component diagram is valid
  129. func (c *C4ComponentDiagram) Validate() error {
  130. return c.C4Diagram.validate()
  131. }
  132. // NewC4ComponentDiagram creates a new C4 Component diagram
  133. func NewC4ComponentDiagram() *C4ComponentDiagram {
  134. return &C4ComponentDiagram{
  135. C4Diagram: C4Diagram{
  136. Elements: make([]*C4Element, 0),
  137. Relations: make([]*C4Relation, 0),
  138. Boundaries: make([]*C4Boundary, 0),
  139. Config: make(map[string]any),
  140. },
  141. }
  142. }
  143. // C4DynamicDiagram represents a C4 Dynamic diagram
  144. type C4DynamicDiagram struct {
  145. C4Diagram
  146. }
  147. // Type returns the diagram type
  148. func (c *C4DynamicDiagram) Type() DiagramType {
  149. return DiagramTypeC4Dynamic
  150. }
  151. // Validate checks if the C4 Dynamic diagram is valid
  152. func (c *C4DynamicDiagram) Validate() error {
  153. return c.C4Diagram.validate()
  154. }
  155. // NewC4DynamicDiagram creates a new C4 Dynamic diagram
  156. func NewC4DynamicDiagram() *C4DynamicDiagram {
  157. return &C4DynamicDiagram{
  158. C4Diagram: C4Diagram{
  159. Elements: make([]*C4Element, 0),
  160. Relations: make([]*C4Relation, 0),
  161. Boundaries: make([]*C4Boundary, 0),
  162. Config: make(map[string]any),
  163. },
  164. }
  165. }
  166. // C4DeploymentDiagram represents a C4 Deployment diagram
  167. type C4DeploymentDiagram struct {
  168. C4Diagram
  169. }
  170. // Type returns the diagram type
  171. func (c *C4DeploymentDiagram) Type() DiagramType {
  172. return DiagramTypeC4Deployment
  173. }
  174. // Validate checks if the C4 Deployment diagram is valid
  175. func (c *C4DeploymentDiagram) Validate() error {
  176. return c.C4Diagram.validate()
  177. }
  178. // NewC4DeploymentDiagram creates a new C4 Deployment diagram
  179. func NewC4DeploymentDiagram() *C4DeploymentDiagram {
  180. return &C4DeploymentDiagram{
  181. C4Diagram: C4Diagram{
  182. Elements: make([]*C4Element, 0),
  183. Relations: make([]*C4Relation, 0),
  184. Boundaries: make([]*C4Boundary, 0),
  185. Config: make(map[string]any),
  186. },
  187. }
  188. }
  189. // validate is a helper method for common C4 diagram validation
  190. func (c *C4Diagram) validate() error {
  191. elementMap := make(map[string]bool)
  192. for _, element := range c.Elements {
  193. elementMap[element.ID] = true
  194. }
  195. for _, relation := range c.Relations {
  196. if !elementMap[relation.From] {
  197. return NewValidationError("relation references non-existent element: " + relation.From)
  198. }
  199. if !elementMap[relation.To] {
  200. return NewValidationError("relation references non-existent element: " + relation.To)
  201. }
  202. }
  203. return nil
  204. }
  205. // AddElement adds an element to the C4 diagram
  206. func (c *C4Diagram) AddElement(element *C4Element) {
  207. c.Elements = append(c.Elements, element)
  208. }
  209. // AddRelation adds a relation to the C4 diagram
  210. func (c *C4Diagram) AddRelation(relation *C4Relation) {
  211. c.Relations = append(c.Relations, relation)
  212. }
  213. // AddBoundary adds a boundary to the C4 diagram
  214. func (c *C4Diagram) AddBoundary(boundary *C4Boundary) {
  215. c.Boundaries = append(c.Boundaries, boundary)
  216. }
  217. // FindElement finds an element by ID
  218. func (c *C4Diagram) FindElement(id string) *C4Element {
  219. for _, element := range c.Elements {
  220. if element.ID == id {
  221. return element
  222. }
  223. }
  224. return nil
  225. }
  226. // FindBoundary finds a boundary by ID
  227. func (c *C4Diagram) FindBoundary(id string) *C4Boundary {
  228. for _, boundary := range c.Boundaries {
  229. if boundary.ID == id {
  230. return boundary
  231. }
  232. }
  233. return nil
  234. }