Skip to content

Commit bdc047b

Browse files
committed
feat: implement MarshalJSON
1 parent a1e4f3d commit bdc047b

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

components.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,19 @@ func (Section) Type() ComponentType {
327327
return SectionComponent
328328
}
329329

330+
// MarshalJSON is a method for marshaling Section to a JSON object.
331+
func (s Section) MarshalJSON() ([]byte, error) {
332+
type section Section
333+
334+
return Marshal(struct {
335+
section
336+
Type ComponentType `json:"type"`
337+
}{
338+
section: section(s),
339+
Type: s.Type(),
340+
})
341+
}
342+
330343
// TextDisplay is a top-level component that allows you to add markdown-formatted text to the message.
331344
type TextDisplay struct {
332345
Content string `json:"content"`
@@ -337,6 +350,19 @@ func (TextDisplay) Type() ComponentType {
337350
return TextDisplayComponent
338351
}
339352

353+
// MarshalJSON is a method for marshaling TextDisplay to a JSON object.
354+
func (t TextDisplay) MarshalJSON() ([]byte, error) {
355+
type textDisplay TextDisplay
356+
357+
return Marshal(struct {
358+
textDisplay
359+
Type ComponentType `json:"type"`
360+
}{
361+
textDisplay: textDisplay(t),
362+
Type: t.Type(),
363+
})
364+
}
365+
340366
// Thumbnail component can be used as an accessory for a section component.
341367
type Thumbnail struct {
342368
// Unique identifier for the component; auto populated through increment if not provided.
@@ -351,6 +377,19 @@ func (Thumbnail) Type() ComponentType {
351377
return ThumbnailComponent
352378
}
353379

380+
// MarshalJSON is a method for marshaling Thumbnail to a JSON object.
381+
func (t Thumbnail) MarshalJSON() ([]byte, error) {
382+
type thumbnail Thumbnail
383+
384+
return Marshal(struct {
385+
thumbnail
386+
Type ComponentType `json:"type"`
387+
}{
388+
thumbnail: thumbnail(t),
389+
Type: t.Type(),
390+
})
391+
}
392+
354393
// MediaGallery is a top-level component allows you to group images, videos or gifs into a gallery grid.
355394
type MediaGallery struct {
356395
// Unique identifier for the component; auto populated through increment if not provided.
@@ -364,6 +403,19 @@ func (MediaGallery) Type() ComponentType {
364403
return MediaGalleryComponent
365404
}
366405

406+
// MarshalJSON is a method for marshaling MediaGallery to a JSON object.
407+
func (m MediaGallery) MarshalJSON() ([]byte, error) {
408+
type mediaGallery MediaGallery
409+
410+
return Marshal(struct {
411+
mediaGallery
412+
Type ComponentType `json:"type"`
413+
}{
414+
mediaGallery: mediaGallery(m),
415+
Type: m.Type(),
416+
})
417+
}
418+
367419
// MediaGalleryItem represents an item used in MediaGallery.
368420
type MediaGalleryItem struct {
369421
Media UnfurledMediaItem `json:"media"`
@@ -384,6 +436,19 @@ func (FileComponent) Type() ComponentType {
384436
return FileComponentType
385437
}
386438

439+
// MarshalJSON is a method for marshaling FileComponent to a JSON object.
440+
func (f FileComponent) MarshalJSON() ([]byte, error) {
441+
type fileComponent FileComponent
442+
443+
return Marshal(struct {
444+
fileComponent
445+
Type ComponentType `json:"type"`
446+
}{
447+
fileComponent: fileComponent(f),
448+
Type: f.Type(),
449+
})
450+
}
451+
387452
// SeparatorSpacingSize represents spacing size around the separator.
388453
type SeparatorSpacingSize uint
389454

@@ -402,6 +467,24 @@ type Separator struct {
402467
Spacing *SeparatorSpacingSize `json:"spacing,omitempty"`
403468
}
404469

470+
// Type is a method to get the type of a component.
471+
func (Separator) Type() ComponentType {
472+
return SeparatorComponent
473+
}
474+
475+
// MarshalJSON is a method for marshaling Separator to a JSON object.
476+
func (s Separator) MarshalJSON() ([]byte, error) {
477+
type separator Separator
478+
479+
return Marshal(struct {
480+
separator
481+
Type ComponentType `json:"type"`
482+
}{
483+
separator: separator(s),
484+
Type: s.Type(),
485+
})
486+
}
487+
405488
// Container is a top-level layout component.
406489
// Containers are visually distinct from surrounding components and have an optional customizable color bar (similar to embeds).
407490
type Container struct {
@@ -412,6 +495,24 @@ type Container struct {
412495
Components []MessageComponent `json:"components"`
413496
}
414497

498+
// Type is a method to get the type of a component.
499+
func (Container) Type() ComponentType {
500+
return ContainerComponent
501+
}
502+
503+
// MarshalJSON is a method for marshaling Container to a JSON object.
504+
func (c Container) MarshalJSON() ([]byte, error) {
505+
type container Container
506+
507+
return Marshal(struct {
508+
container
509+
Type ComponentType `json:"type"`
510+
}{
511+
container: container(c),
512+
Type: c.Type(),
513+
})
514+
}
515+
415516
// UnfurledMediaItem represents an unfurled media item.
416517
type UnfurledMediaItem struct {
417518
URL string `json:"url"`

0 commit comments

Comments
 (0)