From 108913b701ca8d8f2e4d5658be40b3907299d3be Mon Sep 17 00:00:00 2001 From: diamondburned Date: Thu, 29 Oct 2020 13:25:03 -0700 Subject: [PATCH] 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. --- discord/channel.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/discord/channel.go b/discord/channel.go index 58262c9..dd297ad 100644 --- a/discord/channel.go +++ b/discord/channel.go @@ -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 +}