1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2025-02-11 05:52:58 +00:00

discord: Fix {un,}marshal loop

This commit is contained in:
diamondburned 2021-05-29 22:02:55 -07:00 committed by diamondburned
parent 6f73088e7d
commit 4595c87e36

View file

@ -1,10 +1,11 @@
package discord
import (
"github.com/diamondburned/arikawa/v2/utils/json"
"strconv"
"strings"
"time"
"github.com/diamondburned/arikawa/v2/utils/json"
)
// https://discord.com/developers/docs/resources/channel#channel-object
@ -65,13 +66,16 @@ type Channel struct {
//
// If RTCRegionID is an empty string, the region is automatically
// determined.
RTCRegionID *string `json:"rtc_region,omitempty"`
RTCRegionID string `json:"rtc_region,omitempty"`
// VideoQualityMode is the camera video quality mode of the voice channel.
VideoQualityMode VideoQualityMode `json:"video_quality_mode,omitempty"`
}
func (ch *Channel) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(data, ch); err != nil {
type RawChannel Channel
var newCh RawChannel
if err := json.Unmarshal(data, &newCh); err != nil {
return err
}
@ -84,30 +88,24 @@ func (ch *Channel) UnmarshalJSON(data []byte) error {
ch.VideoQualityMode = 1
}
// "rtc_region" is present in the json and was unmarshalled as nil, so
// Discord sent us JSON null. Set this to &"", refer to the doc of
// .RTCRegionID for more information.
if strings.Contains(string(data), `"rtc_region"`) && ch.RTCRegionID == nil {
region := ""
ch.RTCRegionID = &region
}
return nil
}
func (ch Channel) MarshalJSON() ([]byte, error) {
if ch.RTCRegionID == nil || *ch.RTCRegionID != "" {
return json.Marshal(ch)
type RawChannel Channel
if ch.RTCRegionID != "" {
return json.Marshal(RawChannel(ch))
}
marshalChannel := struct {
Channel
RawChannel
// Remove the ",omitempty" flag, forcing RTCRegionID to be marshalled
// as JSON null. See the doc of Channel.RTCRegionID for more
// information.
RTCRegionID *string `json:"rtc_region"`
}{
Channel: ch,
RawChannel: RawChannel(ch),
RTCRegionID: nil,
}