Skip to content

Commit 948015d

Browse files
committed
feat: implement custom JSON marshaling and unmarshaling for ThreadChannelPost
1 parent 7b523c2 commit 948015d

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

discord/thread.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,44 @@ type ThreadChannelPost struct {
3232
Message Message `json:"message"`
3333
}
3434

35+
func (c *ThreadChannelPost) UnmarshalJSON(data []byte) error {
36+
var thread GuildThread
37+
if err := json.Unmarshal(data, &thread); err != nil {
38+
return err
39+
}
40+
41+
c.GuildThread = thread
42+
43+
var v struct {
44+
Message Message `json:"message"`
45+
}
46+
if err := json.Unmarshal(data, &v); err != nil {
47+
return err
48+
}
49+
50+
c.GuildThread = thread
51+
c.Message = v.Message
52+
return nil
53+
}
54+
55+
func (c ThreadChannelPost) MarshalJSON() ([]byte, error) {
56+
data1, err := json.Marshal(c.GuildThread)
57+
if err != nil {
58+
return nil, err
59+
}
60+
61+
data2, err := json.Marshal(struct {
62+
Message Message `json:"message"`
63+
}{
64+
Message: c.Message,
65+
})
66+
if err != nil {
67+
return nil, err
68+
}
69+
70+
return json.Merge(data1, data2)
71+
}
72+
3573
type ThreadCreate interface {
3674
json.Marshaler
3775
Type() ChannelType

0 commit comments

Comments
 (0)