feat: Add UnMarshal

This commit is contained in:
Siwoo Jeon 2025-05-14 21:24:12 +09:00
parent 4ee5a8485c
commit 8e0346dc12
Signed by: migan
GPG key ID: 036E9A8C5E8E48DA

View file

@ -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"`