Discord: OverwriteType to accept numbers both stringed and not

Because Discord is so incompetent that they can't even handle 1s and 0s
in JSON, we can't really predict when they quote a number and when they
don't, because as it turns out, the documentation does not cover all
cases.

This commit adds a JSONUnmarshaler into OverwriteType to always trim the
quotes around then parse the number manually.
This commit is contained in:
diamondburned 2020-10-29 13:25:03 -07:00
parent 21ac7972fa
commit 108913b701
1 changed files with 20 additions and 0 deletions

View File

@ -1,5 +1,10 @@
package discord
import (
"bytes"
"strconv"
)
// https://discord.com/developers/docs/resources/channel#channel-object
type Channel struct {
// ID is the id of this channel.
@ -122,3 +127,18 @@ const (
// OverwriteMember is an overwrite for a member.
OverwriteMember
)
// UnmarshalJSON unmarshals both a string-quoteed number and a regular number
// into OverwriteType. We need to do this because Discord is so bad that they
// can't even handle 1s and 0s properly.
func (otype *OverwriteType) UnmarshalJSON(b []byte) error {
b = bytes.Trim(b, `"`)
u, err := strconv.ParseUint(string(b), 10, 8)
if err != nil {
return err
}
*otype = OverwriteType(u)
return nil
}