class.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Class diagram AST structures based on classDiagram.jison
  2. package ast
  3. // ClassDiagram represents a class diagram
  4. type ClassDiagram struct {
  5. Classes map[string]*ClassNode `json:"classes"`
  6. Relations []*ClassRelation `json:"relations"`
  7. ClassDefs map[string]*ClassDef `json:"classDefs"`
  8. Notes []*ClassNote `json:"notes,omitempty"`
  9. Direction string `json:"direction,omitempty"`
  10. Title *string `json:"title,omitempty"`
  11. Config map[string]any `json:"config,omitempty"`
  12. }
  13. type ClassNode struct {
  14. ID string `json:"id"`
  15. Label string `json:"label"`
  16. Type ClassType `json:"type"`
  17. Members []*ClassMember `json:"members"`
  18. Methods []*ClassMethod `json:"methods"`
  19. Annotations []string `json:"annotations,omitempty"`
  20. Link *string `json:"link,omitempty"`
  21. LinkTarget *string `json:"linkTarget,omitempty"`
  22. Tooltip *string `json:"tooltip,omitempty"`
  23. CssClasses []string `json:"cssClasses,omitempty"`
  24. }
  25. type ClassType string
  26. const (
  27. ClassTypeClass ClassType = "class"
  28. ClassTypeInterface ClassType = "interface"
  29. ClassTypeEnum ClassType = "enumeration"
  30. ClassTypeAbstract ClassType = "abstract"
  31. )
  32. type ClassMember struct {
  33. Name string `json:"name"`
  34. Type string `json:"type"`
  35. Generics []*Generic `json:"generics,omitempty"`
  36. Visibility MemberVisibility `json:"visibility"`
  37. Classifier *string `json:"classifier,omitempty"` // static, abstract
  38. }
  39. type ClassMethod struct {
  40. Name string `json:"name"`
  41. Type string `json:"type"`
  42. Generics []*Generic `json:"generics,omitempty"`
  43. Parameters []string `json:"parameters,omitempty"`
  44. Visibility MemberVisibility `json:"visibility"`
  45. Classifier *string `json:"classifier,omitempty"` // static, abstract
  46. }
  47. type MemberVisibility string
  48. const (
  49. VisibilityPublic MemberVisibility = "+"
  50. VisibilityPrivate MemberVisibility = "-"
  51. VisibilityProtected MemberVisibility = "#"
  52. VisibilityPackage MemberVisibility = "~"
  53. )
  54. type ClassRelation struct {
  55. From string `json:"from"`
  56. To string `json:"to"`
  57. Type ClassRelationType `json:"type"`
  58. Label *string `json:"label,omitempty"`
  59. Cardinality *ClassCardinality `json:"cardinality,omitempty"`
  60. }
  61. type ClassRelationType string
  62. const (
  63. RelationInheritance ClassRelationType = "inheritance" // --|>
  64. RelationComposition ClassRelationType = "composition" // --*
  65. RelationAggregation ClassRelationType = "aggregation" // --o
  66. RelationAssociation ClassRelationType = "association" // -->
  67. RelationRealization ClassRelationType = "realization" // ..|>
  68. RelationDependency ClassRelationType = "dependency" // ..>
  69. )
  70. type ClassCardinality struct {
  71. From string `json:"from,omitempty"`
  72. To string `json:"to,omitempty"`
  73. }
  74. // ClassNote represents a note annotation
  75. type ClassNote struct {
  76. ID string `json:"id,omitempty"`
  77. ForClass *string `json:"forClass,omitempty"`
  78. Text string `json:"text"`
  79. Position NotePlace `json:"position,omitempty"`
  80. }
  81. // Generic represents a generic type parameter
  82. type Generic struct {
  83. Name string `json:"name"`
  84. Constraint *string `json:"constraint,omitempty"`
  85. Arguments []*Generic `json:"arguments,omitempty"`
  86. }
  87. // ClickEvent represents a click interaction
  88. type ClickEvent struct {
  89. NodeID string `json:"nodeId"`
  90. Callback *string `json:"callback,omitempty"`
  91. Link *string `json:"link,omitempty"`
  92. Target *string `json:"target,omitempty"`
  93. Tooltip *string `json:"tooltip,omitempty"`
  94. }
  95. type ClassDef struct {
  96. ID string `json:"id"`
  97. Styles []string `json:"styles"`
  98. }
  99. // Type returns the diagram type
  100. func (c *ClassDiagram) Type() DiagramType {
  101. return DiagramTypeClassDiagram
  102. }
  103. // Validate checks if the class diagram is valid
  104. func (c *ClassDiagram) Validate() error {
  105. // Validate relations reference valid classes
  106. for _, rel := range c.Relations {
  107. if _, exists := c.Classes[rel.From]; !exists {
  108. return NewValidationError("relation references non-existent class: " + rel.From)
  109. }
  110. if _, exists := c.Classes[rel.To]; !exists {
  111. return NewValidationError("relation references non-existent class: " + rel.To)
  112. }
  113. }
  114. return nil
  115. }
  116. // NewClassDiagram creates a new class diagram
  117. func NewClassDiagram() *ClassDiagram {
  118. return &ClassDiagram{
  119. Classes: make(map[string]*ClassNode),
  120. Relations: make([]*ClassRelation, 0),
  121. ClassDefs: make(map[string]*ClassDef),
  122. Notes: make([]*ClassNote, 0),
  123. Config: make(map[string]any),
  124. }
  125. }