From d69d6750dc9a5505f1a148384ec0bfa1c8c95ca4 Mon Sep 17 00:00:00 2001 From: diamondburned Date: Sat, 26 Dec 2020 16:08:41 -0800 Subject: [PATCH] API: Separated min function --- api/guild.go | 9 +++++---- api/math.go | 10 ---------- api/member.go | 5 +++-- api/message.go | 11 ++++++----- api/message_reaction.go | 9 +++++---- internal/intmath/math.go | 10 ++++++++++ {api => internal/intmath}/math_test.go | 6 +++--- 7 files changed, 32 insertions(+), 28 deletions(-) delete mode 100644 api/math.go create mode 100644 internal/intmath/math.go rename {api => internal/intmath}/math_test.go (87%) diff --git a/api/guild.go b/api/guild.go index 94b6d9d..0f21f02 100644 --- a/api/guild.go +++ b/api/guild.go @@ -5,6 +5,7 @@ import ( "net/url" "github.com/diamondburned/arikawa/v2/discord" // for clarity + "github.com/diamondburned/arikawa/v2/internal/intmath" "github.com/diamondburned/arikawa/v2/utils/httputil" "github.com/diamondburned/arikawa/v2/utils/json/option" ) @@ -146,8 +147,8 @@ func (c *Client) GuildsBefore(before discord.GuildID, limit uint) ([]discord.Gui for limit > 0 || unlimited { if limit > 0 { // Only fetch as much as we need. Since limit gradually decreases, - // we only need to fetch min(fetch, limit). - fetch = uint(min(MaxGuildFetchLimit, int(limit))) + // we only need to fetch intmath.Min(fetch, limit). + fetch = uint(intmath.Min(MaxGuildFetchLimit, int(limit))) limit -= fetch } @@ -191,8 +192,8 @@ func (c *Client) GuildsAfter(after discord.GuildID, limit uint) ([]discord.Guild for limit > 0 || unlimited { if limit > 0 { // Only fetch as much as we need. Since limit gradually decreases, - // we only need to fetch min(fetch, limit). - fetch = uint(min(MaxGuildFetchLimit, int(limit))) + // we only need to fetch intmath.Min(fetch, limit). + fetch = uint(intmath.Min(MaxGuildFetchLimit, int(limit))) limit -= fetch } diff --git a/api/math.go b/api/math.go deleted file mode 100644 index 9041fd3..0000000 --- a/api/math.go +++ /dev/null @@ -1,10 +0,0 @@ -package api - -// min returns the smaller of the two passed numbers. -func min(a, b int) int { - if a < b { - return a - } - - return b -} diff --git a/api/member.go b/api/member.go index 0182e7b..d82e678 100644 --- a/api/member.go +++ b/api/member.go @@ -2,6 +2,7 @@ package api import ( "github.com/diamondburned/arikawa/v2/discord" + "github.com/diamondburned/arikawa/v2/internal/intmath" "github.com/diamondburned/arikawa/v2/utils/httputil" "github.com/diamondburned/arikawa/v2/utils/json/option" ) @@ -45,9 +46,9 @@ func (c *Client) MembersAfter( for limit > 0 || unlimited { // Only fetch as much as we need. Since limit gradually decreases, - // we only need to fetch min(fetch, limit). + // we only need to fetch intmath.Min(fetch, limit). if limit > 0 { - fetch = uint(min(MaxMemberFetchLimit, int(limit))) + fetch = uint(intmath.Min(MaxMemberFetchLimit, int(limit))) limit -= fetch } diff --git a/api/message.go b/api/message.go index 6b53ef2..5f5ed89 100644 --- a/api/message.go +++ b/api/message.go @@ -4,6 +4,7 @@ import ( "github.com/pkg/errors" "github.com/diamondburned/arikawa/v2/discord" + "github.com/diamondburned/arikawa/v2/internal/intmath" "github.com/diamondburned/arikawa/v2/utils/httputil" "github.com/diamondburned/arikawa/v2/utils/json/option" ) @@ -66,8 +67,8 @@ func (c *Client) MessagesBefore( for limit > 0 || unlimited { if limit > 0 { // Only fetch as much as we need. Since limit gradually decreases, - // we only need to fetch min(fetch, limit). - fetch = uint(min(maxMessageFetchLimit, int(limit))) + // we only need to fetch intmath.Min(fetch, limit). + fetch = uint(intmath.Min(maxMessageFetchLimit, int(limit))) limit -= maxMessageFetchLimit } @@ -123,8 +124,8 @@ func (c *Client) MessagesAfter( for limit > 0 || unlimited { if limit > 0 { // Only fetch as much as we need. Since limit gradually decreases, - // we only need to fetch min(fetch, limit). - fetch = uint(min(maxMessageFetchLimit, int(limit))) + // we only need to fetch intmath.Min(fetch, limit). + fetch = uint(intmath.Min(maxMessageFetchLimit, int(limit))) limit -= maxMessageFetchLimit } @@ -350,7 +351,7 @@ func (c *Client) DeleteMessages(channelID discord.ChannelID, messageIDs []discor // If the number of messages to be deleted exceeds the amount discord is willing // to accept at one time then batches of messages will be deleted for start := 0; start < len(messageIDs); start += maxMessageDeleteLimit { - end := min(len(messageIDs), start+maxMessageDeleteLimit) + end := intmath.Min(len(messageIDs), start+maxMessageDeleteLimit) err := c.deleteMessages(channelID, messageIDs[start:end]) if err != nil { return err diff --git a/api/message_reaction.go b/api/message_reaction.go index f902e20..6aabb59 100644 --- a/api/message_reaction.go +++ b/api/message_reaction.go @@ -2,6 +2,7 @@ package api import ( "github.com/diamondburned/arikawa/v2/discord" + "github.com/diamondburned/arikawa/v2/internal/intmath" "github.com/diamondburned/arikawa/v2/utils/httputil" ) @@ -73,8 +74,8 @@ func (c *Client) ReactionsBefore( for limit > 0 || unlimited { if limit > 0 { // Only fetch as much as we need. Since limit gradually decreases, - // we only need to fetch min(fetch, limit). - fetch = uint(min(MaxMessageReactionFetchLimit, int(limit))) + // we only need to fetch intmath.Min(fetch, limit). + fetch = uint(intmath.Min(MaxMessageReactionFetchLimit, int(limit))) limit -= fetch } @@ -121,8 +122,8 @@ func (c *Client) ReactionsAfter( for limit > 0 || unlimited { if limit > 0 { // Only fetch as much as we need. Since limit gradually decreases, - // we only need to fetch min(fetch, limit). - fetch = uint(min(MaxMessageReactionFetchLimit, int(limit))) + // we only need to fetch intmath.Min(fetch, limit). + fetch = uint(intmath.Min(MaxMessageReactionFetchLimit, int(limit))) limit -= fetch } diff --git a/internal/intmath/math.go b/internal/intmath/math.go new file mode 100644 index 0000000..37beb20 --- /dev/null +++ b/internal/intmath/math.go @@ -0,0 +1,10 @@ +package intmath + +// Min returns the smaller of the two passed numbers. +func Min(a, b int) int { + if a < b { + return a + } + + return b +} diff --git a/api/math_test.go b/internal/intmath/math_test.go similarity index 87% rename from api/math_test.go rename to internal/intmath/math_test.go index da7a637..5e745bf 100644 --- a/api/math_test.go +++ b/internal/intmath/math_test.go @@ -1,8 +1,8 @@ -package api +package intmath import "testing" -func Test_min(t *testing.T) { +func TestMin(t *testing.T) { testCases := []struct { name string a, b int @@ -30,7 +30,7 @@ func Test_min(t *testing.T) { for _, c := range testCases { t.Run(c.name, func(t *testing.T) { - actual := min(c.a, c.b) + actual := Min(c.a, c.b) if c.expect != actual { t.Errorf("expected min(%d, %d) to return %d, but did %d", c.a, c.b, c.expect, actual) }