diff --git a/components.go b/components.go index 41e5733..a1fb68e 100644 --- a/components.go +++ b/components.go @@ -354,6 +354,42 @@ func (s Section) MarshalJSON() ([]byte, error) { }) } +// UnmarshalJSON is a method for unmarshaling Section from JSON. +// origin https://github.com/bwmarrin/discordgo/pull/1616 +func (s *Section) UnmarshalJSON(data []byte) error { + type sectionAlias Section + aux := struct { + sectionAlias + RawComponents []json.RawMessage `json:"components"` + RawAccessory json.RawMessage `json:"accessory"` + }{} + + if err := json.Unmarshal(data, &aux); err != nil { + return err + } + + *s = Section(aux.sectionAlias) + + s.Components = make([]MessageComponent, len(aux.RawComponents)) + for i, raw := range aux.RawComponents { + component, err := MessageComponentFromJSON(raw) + if err != nil { + return fmt.Errorf("failed to unmarshal section component %d: %w", i, err) + } + s.Components[i] = component + } + + if len(aux.RawAccessory) > 0 && string(aux.RawAccessory) != "null" { + accessory, err := MessageComponentFromJSON(aux.RawAccessory) + if err != nil { + return fmt.Errorf("failed to unmarshal accessory: %w", err) + } + s.Accessory = accessory + } + + return nil +} + // TextDisplay is a top-level component that allows you to add markdown-formatted text to the message. type TextDisplay struct { Content string `json:"content"` @@ -527,6 +563,33 @@ func (c Container) MarshalJSON() ([]byte, error) { }) } +// UnmarshalJSON is a method for unmarshaling Container from JSON. +// origin https://github.com/bwmarrin/discordgo/pull/1616 +func (c *Container) UnmarshalJSON(data []byte) error { + type containerAlias Container + aux := struct { + containerAlias + RawComponents []json.RawMessage `json:"components"` + }{} + + if err := json.Unmarshal(data, &aux); err != nil { + return err + } + + *c = Container(aux.containerAlias) + + c.Components = make([]MessageComponent, len(aux.RawComponents)) + for i, raw := range aux.RawComponents { + component, err := MessageComponentFromJSON(raw) + if err != nil { + return fmt.Errorf("failed to parse component at index %d: %w", i, err) + } + c.Components[i] = component + } + + return nil +} + // UnfurledMediaItem represents an unfurled media item. type UnfurledMediaItem struct { URL string `json:"url"`