Skip to content

Commit d917470

Browse files
committed
no int casting
1 parent b2a0439 commit d917470

File tree

4 files changed

+16
-26
lines changed

4 files changed

+16
-26
lines changed

almanax.go

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"fmt"
88
"log"
99
"net/http"
10-
"strconv"
1110
"strings"
1211
"time"
1312

@@ -266,7 +265,7 @@ func handleCreateAlmanax(w http.ResponseWriter, r *http.Request) {
266265
}
267266

268267
requestedBonuses := map[string]*Set[string]{}
269-
requestedMentions := map[string]*Set[uint64]{}
268+
requestedMentions := map[string]*Set[json.Number]{}
270269

271270
if createWebhook.Mentions != nil {
272271

@@ -280,7 +279,7 @@ func handleCreateAlmanax(w http.ResponseWriter, r *http.Request) {
280279
requestedBonuses[bonusId] = NewSet[string]()
281280
}
282281
if _, ok := requestedMentions[bonusId]; !ok {
283-
requestedMentions[bonusId] = NewSet[uint64]()
282+
requestedMentions[bonusId] = NewSet[json.Number]()
284283
}
285284
if !possibleBonuses.Has(bonusId) {
286285
http.Error(w, "Unknown almanax bonus id: "+bonusId+".", http.StatusBadRequest)
@@ -298,12 +297,7 @@ func handleCreateAlmanax(w http.ResponseWriter, r *http.Request) {
298297
return
299298
}
300299
}
301-
discordId, err := mention.DiscordId.Int64()
302-
if err != nil {
303-
http.Error(w, "Invalid discord id.", http.StatusBadRequest)
304-
return
305-
}
306-
requestedMentions[bonusId].Add(uint64(discordId))
300+
requestedMentions[bonusId].Add(mention.DiscordId)
307301
}
308302
}
309303
}
@@ -940,11 +934,7 @@ func buildDiscordHookAlmanax(almanaxSend AlmanaxSend) ([]PreparedHook, error) {
940934
if mentions, ok := hookMentions[almBonusType.GetId()]; ok {
941935
var mentionStrings []string
942936
for _, mention := range mentions {
943-
discordId, err := mention.DiscordId.Int64()
944-
if err != nil {
945-
return nil, err
946-
}
947-
idStr := strconv.FormatInt(discordId, 10)
937+
idStr := mention.DiscordId.String()
948938
found := false
949939
for _, alreadyInsertedMention := range mentionStrings {
950940
if strings.Contains(alreadyInsertedMention, idStr) {
@@ -982,11 +972,7 @@ func buildDiscordHookAlmanax(almanaxSend AlmanaxSend) ([]PreparedHook, error) {
982972

983973
var mentionStrings []string
984974
for _, mention := range mentions {
985-
discordId, err := mention.DiscordId.Int64()
986-
if err != nil {
987-
return nil, err
988-
}
989-
idStr := strconv.FormatInt(discordId, 10)
975+
idStr := mention.DiscordId.String()
990976
if mention.IsRole {
991977
mentionStrings = append(mentionStrings, "<@&"+idStr+">")
992978
} else {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ toolchain go1.23.0
66

77
require (
88
github.com/JohannesKaufmann/html-to-markdown v1.6.0
9-
github.com/dofusdude/dodugo v1.0.0-rc.9
9+
github.com/dofusdude/dodugo v1.0.0
1010
github.com/go-chi/chi/v5 v5.2.0
1111
github.com/google/uuid v1.6.0
1212
github.com/jackc/pgx/v5 v5.7.2

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XL
1919
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2020
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2121
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
22-
github.com/dofusdude/dodugo v1.0.0-rc.9 h1:4aEEjipVjoKyiSuY1YRNF625lqnTIdiqarbtGFRITt4=
23-
github.com/dofusdude/dodugo v1.0.0-rc.9/go.mod h1:R/MZWCsB/+GpFctfckhOWmqpCMgmGkB+YC3N3TS3n6Y=
22+
github.com/dofusdude/dodugo v1.0.0 h1:wjNw2YmiaNYneGn9ZMlnHzJZIP5EurugNs7LmC3zrXc=
23+
github.com/dofusdude/dodugo v1.0.0/go.mod h1:R/MZWCsB/+GpFctfckhOWmqpCMgmGkB+YC3N3TS3n6Y=
2424
github.com/go-chi/chi/v5 v5.2.0 h1:Aj1EtB0qR2Rdo2dG4O94RIU35w2lvQSj6BRA4+qwFL0=
2525
github.com/go-chi/chi/v5 v5.2.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
2626
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=

repository.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package main
22

33
import (
44
"context"
5+
"encoding/json"
56
"errors"
7+
"fmt"
68
"log"
79
"sync"
810
"time"
@@ -676,10 +678,12 @@ func (r *Repository) GetAlmanaxDiscordMentions(id uuid.UUID) (map[string][]Menti
676678
var bonusId string
677679

678680
var mention MentionDTO
679-
err = rows.Scan(&bonusId, &mention.DiscordId, &mention.IsRole, &mention.PingDaysBefore)
681+
var discordId uint64
682+
err = rows.Scan(&bonusId, discordId, &mention.IsRole, &mention.PingDaysBefore)
680683
if err != nil {
681684
return res, err
682685
}
686+
mention.DiscordId = json.Number(fmt.Sprint(discordId))
683687

684688
res[bonusId] = append(res[bonusId], mention)
685689
}
@@ -723,12 +727,12 @@ func (r *Repository) CreateAlmanaxHook(createHook CreateAlmanaxHook) (uuid.UUID,
723727
if createHook.Mentions != nil {
724728
for bonus, mentions := range *createHook.Mentions {
725729
for _, mention := range mentions {
726-
var discordId uuid.UUID
727-
err = r.conn.QueryRow(r.ctx, "insert into discord_mentions (discord_id, is_role) values ($1, $2) returning id", mention.DiscordId, mention.IsRole).Scan(&discordId)
730+
var discordMentionId uuid.UUID
731+
err = r.conn.QueryRow(r.ctx, "insert into discord_mentions (discord_id, is_role) values ($1, $2) returning id", mention.DiscordId.String(), mention.IsRole).Scan(&discordMentionId)
728732
if err != nil {
729733
return id, err
730734
}
731-
_, err = r.conn.Exec(r.ctx, "insert into almanax_mentions (almanax_webhook_id, almanax_bonus_id, discord_mention_id, ping_days_before) values ($1, $2, $3, $4)", id, bonus, discordId, mention.PingDaysBefore)
735+
_, err = r.conn.Exec(r.ctx, "insert into almanax_mentions (almanax_webhook_id, almanax_bonus_id, discord_mention_id, ping_days_before) values ($1, $2, $3, $4)", id, bonus, discordMentionId, mention.PingDaysBefore)
732736
if err != nil {
733737
return id, err
734738
}

0 commit comments

Comments
 (0)