Skip to content

Commit f192228

Browse files
authored
discordgo: self-reference Container for UnmarshalJSON (#2044)
This lets users access the fields of a discordgo.Container for components v2. The current implementation unmarshals only into a container's components, not the other fields; by adding a self-reference to the struct we're unmarshalling into, we can ensure all fields are captured properly. This is effectively the exact inverse of MarshalJSON. Signed-off-by: Luca Zeuch <l-zeuch@email.de>
1 parent ad582a3 commit f192228

1 file changed

Lines changed: 6 additions & 0 deletions

File tree

lib/discordgo/components.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,15 +788,21 @@ func (s Container) MarshalJSON() ([]byte, error) {
788788

789789
// UnmarshalJSON is a helper function to unmarshal Container.
790790
func (c *Container) UnmarshalJSON(data []byte) error {
791+
type container Container
791792
var v struct {
793+
container
792794
RawComponents []unmarshalableMessageComponent `json:"components"`
793795
}
796+
794797
err := json.Unmarshal(data, &v)
795798
if err != nil {
796799
return err
797800
}
801+
798802
var ok bool
803+
*c = Container(v.container)
799804
c.Components = make([]TopLevelComponent, len(v.RawComponents))
805+
800806
for i, v := range v.RawComponents {
801807
comp := v.MessageComponent
802808
c.Components[i], ok = comp.(TopLevelComponent)

0 commit comments

Comments
 (0)