attachment.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. }