Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom JSON marshal/unmarshal functions #1162

Merged
merged 1 commit into from Apr 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions components.go
Expand Up @@ -70,7 +70,7 @@ type ActionsRow struct {
func (r ActionsRow) MarshalJSON() ([]byte, error) {
type actionsRow ActionsRow

return json.Marshal(struct {
return Marshal(struct {
actionsRow
Type ComponentType `json:"type"`
}{
Expand Down Expand Up @@ -145,7 +145,7 @@ func (b Button) MarshalJSON() ([]byte, error) {
b.Style = PrimaryButton
}

return json.Marshal(struct {
return Marshal(struct {
button
Type ComponentType `json:"type"`
}{
Expand Down Expand Up @@ -192,7 +192,7 @@ func (SelectMenu) Type() ComponentType {
func (m SelectMenu) MarshalJSON() ([]byte, error) {
type selectMenu SelectMenu

return json.Marshal(struct {
return Marshal(struct {
selectMenu
Type ComponentType `json:"type"`
}{
Expand Down Expand Up @@ -222,7 +222,7 @@ func (TextInput) Type() ComponentType {
func (m TextInput) MarshalJSON() ([]byte, error) {
type inputText TextInput

return json.Marshal(struct {
return Marshal(struct {
inputText
Type ComponentType `json:"type"`
}{
Expand Down
17 changes: 12 additions & 5 deletions restapi.go
Expand Up @@ -39,6 +39,13 @@ var (
ErrUnauthorized = errors.New("HTTP request was unauthorized. This could be because the provided token was not a bot token. Please add \"Bot \" to the start of your token. https://discord.com/developers/docs/reference#authentication-example-bot-token-authorization-header")
)

var (
// Marshal defines function used to encode JSON payloads
Marshal func(v interface{}) ([]byte, error) = json.Marshal
// Unmarshal defines function used to decode JSON payloads
Unmarshal func(src []byte, v interface{}) error = json.Unmarshal
)

// RESTError stores error information about a request with a bad response code.
// Message is not always present, there are cases where api calls can fail
// without returning a json message.
Expand All @@ -60,7 +67,7 @@ func newRestError(req *http.Request, resp *http.Response, body []byte) *RESTErro

// Attempt to decode the error and assume no message was provided if it fails
var msg *APIErrorMessage
err := json.Unmarshal(body, &msg)
err := Unmarshal(body, &msg)
if err == nil {
restErr.Message = msg
}
Expand Down Expand Up @@ -94,7 +101,7 @@ func (s *Session) Request(method, urlStr string, data interface{}) (response []b
func (s *Session) RequestWithBucketID(method, urlStr string, data interface{}, bucketID string) (response []byte, err error) {
var body []byte
if data != nil {
body, err = json.Marshal(data)
body, err = Marshal(data)
if err != nil {
return
}
Expand Down Expand Up @@ -193,7 +200,7 @@ func (s *Session) RequestWithLockedBucket(method, urlStr, contentType string, b
}
case 429: // TOO MANY REQUESTS - Rate limiting
rl := TooManyRequests{}
err = json.Unmarshal(response, &rl)
err = Unmarshal(response, &rl)
if err != nil {
s.log(LogError, "rate limit unmarshal error, %s", err)
return
Expand Down Expand Up @@ -225,7 +232,7 @@ func (s *Session) RequestWithLockedBucket(method, urlStr, contentType string, b
}

func unmarshal(data []byte, v interface{}) error {
err := json.Unmarshal(data, v)
err := Unmarshal(data, v)
if err != nil {
return fmt.Errorf("%w: %s", ErrJSONUnmarshal, err)
}
Expand Down Expand Up @@ -2292,7 +2299,7 @@ func (s *Session) WebhookMessage(webhookID, token, messageID string) (message *M
return
}

err = json.Unmarshal(body, &message)
err = Unmarshal(body, &message)

return
}
Expand Down
10 changes: 5 additions & 5 deletions structs.go
Expand Up @@ -878,7 +878,7 @@ func (p GuildScheduledEventParams) MarshalJSON() ([]byte, error) {
type guildScheduledEventParams GuildScheduledEventParams

if p.EntityType == GuildScheduledEventEntityTypeExternal && p.ChannelID == "" {
return json.Marshal(struct {
return Marshal(struct {
guildScheduledEventParams
ChannelID json.RawMessage `json:"channel_id"`
}{
Expand All @@ -887,7 +887,7 @@ func (p GuildScheduledEventParams) MarshalJSON() ([]byte, error) {
})
}

return json.Marshal(guildScheduledEventParams(p))
return Marshal(guildScheduledEventParams(p))
}

// GuildScheduledEventEntityMetadata holds additional metadata for guild scheduled event.
Expand Down Expand Up @@ -1129,7 +1129,7 @@ func (t *TimeStamps) UnmarshalJSON(b []byte) error {
End float64 `json:"end,omitempty"`
Start float64 `json:"start,omitempty"`
}{}
err := json.Unmarshal(b, &temp)
err := Unmarshal(b, &temp)
if err != nil {
return err
}
Expand Down Expand Up @@ -1267,7 +1267,7 @@ func (t *TooManyRequests) UnmarshalJSON(b []byte) error {
Message string `json:"message"`
RetryAfter float64 `json:"retry_after"`
}{}
err := json.Unmarshal(b, &u)
err := Unmarshal(b, &u)
if err != nil {
return err
}
Expand Down Expand Up @@ -1687,7 +1687,7 @@ func (activity *Activity) UnmarshalJSON(b []byte) error {
Instance bool `json:"instance,omitempty"`
Flags int `json:"flags,omitempty"`
}{}
err := json.Unmarshal(b, &temp)
err := Unmarshal(b, &temp)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions util.go
Expand Up @@ -2,7 +2,6 @@ package discordgo

import (
"bytes"
"encoding/json"
"fmt"
"io"
"mime/multipart"
Expand Down Expand Up @@ -30,7 +29,7 @@ func MultipartBodyWithJSON(data interface{}, files []*File) (requestContentType
body := &bytes.Buffer{}
bodywriter := multipart.NewWriter(body)

payload, err := json.Marshal(data)
payload, err := Marshal(data)
if err != nil {
return
}
Expand Down