organization.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Organization chart AST structures
  2. package ast
  3. // OrganizationDiagram represents an organization chart
  4. type OrganizationDiagram struct {
  5. Root *OrganizationNode `json:"root"`
  6. Nodes []*OrganizationNode `json:"nodes"`
  7. Title *string `json:"title,omitempty"`
  8. Config map[string]any `json:"config,omitempty"`
  9. }
  10. type OrganizationNode struct {
  11. ID string `json:"id"`
  12. Name string `json:"name"`
  13. Title *string `json:"title,omitempty"`
  14. Department *string `json:"department,omitempty"`
  15. Level int `json:"level"`
  16. Children []*OrganizationNode `json:"children"`
  17. Parent *OrganizationNode `json:"parent,omitempty"`
  18. Icon *string `json:"icon,omitempty"`
  19. CssClasses []string `json:"cssClasses,omitempty"`
  20. Styles []string `json:"styles,omitempty"`
  21. }
  22. // Type returns the diagram type
  23. func (o *OrganizationDiagram) Type() DiagramType {
  24. return DiagramTypeOrganization
  25. }
  26. // Validate checks if the organization diagram is valid
  27. func (o *OrganizationDiagram) Validate() error {
  28. if o.Root == nil && len(o.Nodes) == 0 {
  29. return NewValidationError("organization diagram must have at least one node")
  30. }
  31. return nil
  32. }
  33. // NewOrganizationDiagram creates a new organization diagram
  34. func NewOrganizationDiagram() *OrganizationDiagram {
  35. return &OrganizationDiagram{
  36. Nodes: make([]*OrganizationNode, 0),
  37. Config: make(map[string]any),
  38. }
  39. }
  40. // AddNode adds a node to the organization chart
  41. func (o *OrganizationDiagram) AddNode(node *OrganizationNode) {
  42. o.Nodes = append(o.Nodes, node)
  43. if o.Root == nil && node.Level == 0 {
  44. o.Root = node
  45. }
  46. }
  47. // FindNode finds a node by ID
  48. func (o *OrganizationDiagram) FindNode(id string) *OrganizationNode {
  49. for _, node := range o.Nodes {
  50. if node.ID == id {
  51. return node
  52. }
  53. }
  54. return nil
  55. }
  56. // AddChild adds a child node to a parent node
  57. func (node *OrganizationNode) AddChild(child *OrganizationNode) {
  58. child.Parent = node
  59. child.Level = node.Level + 1
  60. node.Children = append(node.Children, child)
  61. }