Skip to content

Commit 956e7cd

Browse files
committed
Turn a possibly empty field into a pointer
1 parent dd7351b commit 956e7cd

File tree

6 files changed

+35
-30
lines changed

6 files changed

+35
-30
lines changed

bot.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type Bot struct {
1717
Token string
1818

1919
// The bot's User info, as returned by Bot.GetMe. Populated when created through the NewBot method.
20-
User
20+
*User
2121
// The bot client to use to make requests
2222
BotClient
2323
}
@@ -75,7 +75,7 @@ func NewBot(token string, opts *BotOpts) (*Bot, error) {
7575
if err != nil {
7676
return nil, fmt.Errorf("failed to check bot token: %w", err)
7777
}
78-
b.User = *botUser
78+
b.User = botUser
7979
}
8080

8181
return &b, nil

ext/botmapping_test.go

+2-12
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,11 @@ package ext
22

33
import (
44
"testing"
5-
6-
"github.com/PaulSonOfLars/gotgbot/v2"
75
)
86

97
func Test_botMapping(t *testing.T) {
108
bm := botMapping{}
11-
b := &gotgbot.Bot{
12-
User: gotgbot.User{},
13-
Token: "SOME_TOKEN",
14-
BotClient: &gotgbot.BaseBotClient{},
15-
}
9+
b := NewTestBot()
1610

1711
var origBdata *botData
1812
t.Run("addBot", func(t *testing.T) {
@@ -78,11 +72,7 @@ func Test_botMapping(t *testing.T) {
7872

7973
func Test_botData_isUpdateChannelStopped(t *testing.T) {
8074
bm := botMapping{}
81-
b := &gotgbot.Bot{
82-
User: gotgbot.User{},
83-
Token: "SOME_TOKEN",
84-
BotClient: &gotgbot.BaseBotClient{},
85-
}
75+
b := NewTestBot()
8676

8777
bData, err := bm.addBot(b, "", "")
8878
if err != nil {

ext/common_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package ext
22

33
import (
44
"errors"
5+
"math/rand"
6+
"net/http"
7+
"net/http/httptest"
58

69
"github.com/PaulSonOfLars/gotgbot/v2"
710
)
@@ -37,3 +40,24 @@ func (u *Updater) InjectUpdate(token string, upd gotgbot.Update) error {
3740
}
3841
return d.ProcessUpdate(bData.bot, &upd, nil)
3942
}
43+
44+
func NewTestBot() *gotgbot.Bot {
45+
server := httptest.NewServer(nil)
46+
return &gotgbot.Bot{
47+
Token: "use-me",
48+
User: &gotgbot.User{
49+
Id: rand.Int63(),
50+
IsBot: false,
51+
FirstName: "gobot",
52+
LastName: "",
53+
Username: "gotgbot",
54+
},
55+
BotClient: &gotgbot.BaseBotClient{
56+
Client: http.Client{},
57+
DefaultRequestOpts: &gotgbot.RequestOpts{
58+
Timeout: 0,
59+
APIURL: server.URL,
60+
},
61+
},
62+
}
63+
}

ext/context.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type Context struct {
1313
// We store the info of the Bot that received this update so we can keep track of update ownership.
1414
// We do NOT store full gotgbot.Bot struct, as that would leak the bot token and bot client information in what
1515
// should be a data-only struct.
16-
Bot gotgbot.User
16+
Bot *gotgbot.User
1717
// Data represents update-local storage.
1818
// This can be used to pass data across handlers - for example, to cache operations relevant to the current update,
1919
// such as admin checks.

ext/handlers/common_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ func NewTestBot() *gotgbot.Bot {
1616
server := httptest.NewServer(nil)
1717
return &gotgbot.Bot{
1818
Token: "use-me",
19-
User: gotgbot.User{
20-
Id: 0,
19+
User: &gotgbot.User{
20+
Id: rand.Int63(),
2121
IsBot: false,
2222
FirstName: "gobot",
2323
LastName: "",

ext/updater_test.go

+4-13
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,7 @@ import (
2121
)
2222

2323
func TestUpdaterThrowsErrorWhenSameWebhookAddedTwice(t *testing.T) {
24-
b := &gotgbot.Bot{
25-
User: gotgbot.User{},
26-
Token: "SOME_TOKEN",
27-
BotClient: &gotgbot.BaseBotClient{},
28-
}
24+
b := ext.NewTestBot()
2925

3026
d := ext.NewDispatcher(&ext.DispatcherOpts{})
3127
u := ext.NewUpdater(d, nil)
@@ -45,12 +41,7 @@ func TestUpdaterThrowsErrorWhenSameWebhookAddedTwice(t *testing.T) {
4541
}
4642

4743
func TestUpdaterSupportsWebhookReAdding(t *testing.T) {
48-
b := &gotgbot.Bot{
49-
User: gotgbot.User{},
50-
Token: "SOME_TOKEN",
51-
BotClient: &gotgbot.BaseBotClient{},
52-
}
53-
44+
b := ext.NewTestBot()
5445
d := ext.NewDispatcher(&ext.DispatcherOpts{})
5546
u := ext.NewUpdater(d, nil)
5647

@@ -117,7 +108,7 @@ func concurrentTest(t *testing.T) {
117108
}
118109

119110
b := &gotgbot.Bot{
120-
User: gotgbot.User{},
111+
User: &gotgbot.User{},
121112
Token: "SOME_TOKEN",
122113
BotClient: &gotgbot.BaseBotClient{},
123114
}
@@ -449,7 +440,7 @@ func TestUpdaterSupportsLongPollReAdding(t *testing.T) {
449440
}
450441

451442
b := &gotgbot.Bot{
452-
User: gotgbot.User{},
443+
User: &gotgbot.User{},
453444
Token: "SOME_TOKEN",
454445
BotClient: &gotgbot.BaseBotClient{
455446
DefaultRequestOpts: reqOpts,

0 commit comments

Comments
 (0)