attachment.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package attachment
  2. import (
  3. "github.com/google/uuid"
  4. )
  5. type TextSource struct {
  6. Value string `toml:"value"`
  7. }
  8. type FileSource struct {
  9. Path string `toml:"path"`
  10. Mime string `toml:"mime"`
  11. Data []byte `toml:"data,omitempty"` // Optional for image data
  12. }
  13. type SymbolSource struct {
  14. Path string `toml:"path"`
  15. Name string `toml:"name"`
  16. Kind int `toml:"kind"`
  17. Range SymbolRange `toml:"range"`
  18. }
  19. type SymbolRange struct {
  20. Start Position `toml:"start"`
  21. End Position `toml:"end"`
  22. }
  23. type Position struct {
  24. Line int `toml:"line"`
  25. Char int `toml:"char"`
  26. }
  27. type Attachment struct {
  28. ID string `toml:"id"`
  29. Type string `toml:"type"`
  30. Display string `toml:"display"`
  31. URL string `toml:"url"`
  32. Filename string `toml:"filename"`
  33. MediaType string `toml:"media_type"`
  34. StartIndex int `toml:"start_index"`
  35. EndIndex int `toml:"end_index"`
  36. Source any `toml:"source,omitempty"`
  37. }
  38. // NewAttachment creates a new attachment with a unique ID
  39. func NewAttachment() *Attachment {
  40. return &Attachment{
  41. ID: uuid.NewString(),
  42. }
  43. }
  44. func (a *Attachment) GetTextSource() (*TextSource, bool) {
  45. if a.Type != "text" {
  46. return nil, false
  47. }
  48. ts, ok := a.Source.(*TextSource)
  49. return ts, ok
  50. }
  51. // GetFileSource returns the source as FileSource if the attachment is a file type
  52. func (a *Attachment) GetFileSource() (*FileSource, bool) {
  53. if a.Type != "file" {
  54. return nil, false
  55. }
  56. fs, ok := a.Source.(*FileSource)
  57. return fs, ok
  58. }
  59. // GetSymbolSource returns the source as SymbolSource if the attachment is a symbol type
  60. func (a *Attachment) GetSymbolSource() (*SymbolSource, bool) {
  61. if a.Type != "symbol" {
  62. return nil, false
  63. }
  64. ss, ok := a.Source.(*SymbolSource)
  65. return ss, ok
  66. }
  67. // FromMap creates a TextSource from a map[string]any
  68. func (ts *TextSource) FromMap(sourceMap map[string]any) {
  69. if value, ok := sourceMap["value"].(string); ok {
  70. ts.Value = value
  71. }
  72. }
  73. // FromMap creates a FileSource from a map[string]any
  74. func (fs *FileSource) FromMap(sourceMap map[string]any) {
  75. if path, ok := sourceMap["path"].(string); ok {
  76. fs.Path = path
  77. }
  78. if mime, ok := sourceMap["mime"].(string); ok {
  79. fs.Mime = mime
  80. }
  81. if data, ok := sourceMap["data"].([]byte); ok {
  82. fs.Data = data
  83. }
  84. }
  85. // FromMap creates a SymbolSource from a map[string]any
  86. func (ss *SymbolSource) FromMap(sourceMap map[string]any) {
  87. if path, ok := sourceMap["path"].(string); ok {
  88. ss.Path = path
  89. }
  90. if name, ok := sourceMap["name"].(string); ok {
  91. ss.Name = name
  92. }
  93. if kind, ok := sourceMap["kind"].(int); ok {
  94. ss.Kind = kind
  95. }
  96. if rangeMap, ok := sourceMap["range"].(map[string]any); ok {
  97. ss.Range = SymbolRange{}
  98. if startMap, ok := rangeMap["start"].(map[string]any); ok {
  99. if line, ok := startMap["line"].(int); ok {
  100. ss.Range.Start.Line = line
  101. }
  102. if char, ok := startMap["char"].(int); ok {
  103. ss.Range.Start.Char = char
  104. }
  105. }
  106. if endMap, ok := rangeMap["end"].(map[string]any); ok {
  107. if line, ok := endMap["line"].(int); ok {
  108. ss.Range.End.Line = line
  109. }
  110. if char, ok := endMap["char"].(int); ok {
  111. ss.Range.End.Char = char
  112. }
  113. }
  114. }
  115. }
  116. // RestoreSourceType converts a map[string]any source back to the proper type
  117. func (a *Attachment) RestoreSourceType() {
  118. if a.Source == nil {
  119. return
  120. }
  121. // Check if Source is a map[string]any
  122. if sourceMap, ok := a.Source.(map[string]any); ok {
  123. switch a.Type {
  124. case "text":
  125. ts := &TextSource{}
  126. ts.FromMap(sourceMap)
  127. a.Source = ts
  128. case "file":
  129. fs := &FileSource{}
  130. fs.FromMap(sourceMap)
  131. a.Source = fs
  132. case "symbol":
  133. ss := &SymbolSource{}
  134. ss.FromMap(sourceMap)
  135. a.Source = ss
  136. }
  137. }
  138. }