Skip to content

Commit 753855b

Browse files
authored
Merge pull request #1316 from crazy-max/telegram-align-chatids-format
telegram: align chatIDs and chatIDsFile format handling
2 parents 8678b19 + a27d3f5 commit 753855b

File tree

2 files changed

+30
-54
lines changed

2 files changed

+30
-54
lines changed

internal/notif/telegram/client.go

+22-34
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,7 @@ func (c *Client) Send(entry model.NotifEntry) error {
4949
return errors.Wrap(err, "cannot retrieve token secret for Telegram notifier")
5050
}
5151

52-
var cids []interface{}
53-
for _, cid := range c.cfg.ChatIDs {
54-
cids = append(cids, cid)
55-
}
52+
cids := c.cfg.ChatIDs
5653
cidsRaw, err := utl.GetSecret("", c.cfg.ChatIDsFile)
5754
if err != nil {
5855
return errors.Wrap(err, "cannot retrieve chat IDs secret for Telegram notifier")
@@ -124,41 +121,32 @@ func (c *Client) Send(entry model.NotifEntry) error {
124121
return nil
125122
}
126123

127-
func parseChatIDs(entries []interface{}) ([]chatID, error) {
124+
func parseChatIDs(entries []string) ([]chatID, error) {
128125
var chatIDs []chatID
129126
for _, entry := range entries {
130-
switch v := entry.(type) {
131-
case int:
132-
chatIDs = append(chatIDs, chatID{id: int64(v)})
133-
case int64:
134-
chatIDs = append(chatIDs, chatID{id: v})
135-
case string:
136-
parts := strings.Split(v, ":")
137-
if len(parts) < 1 || len(parts) > 2 {
138-
return nil, errors.Errorf("invalid chat ID %q", v)
139-
}
140-
id, err := strconv.ParseInt(parts[0], 10, 64)
141-
if err != nil {
142-
return nil, errors.Wrap(err, "invalid chat ID")
143-
}
144-
var topics []int64
145-
if len(parts) == 2 {
146-
topicParts := strings.Split(parts[1], ";")
147-
for _, topicPart := range topicParts {
148-
topic, err := strconv.ParseInt(topicPart, 10, 64)
149-
if err != nil {
150-
return nil, errors.Wrapf(err, "invalid topic %q for chat ID %d", topicPart, id)
151-
}
152-
topics = append(topics, topic)
127+
parts := strings.Split(entry, ":")
128+
if len(parts) < 1 || len(parts) > 2 {
129+
return nil, errors.Errorf("invalid chat ID %q", entry)
130+
}
131+
id, err := strconv.ParseInt(parts[0], 10, 64)
132+
if err != nil {
133+
return nil, errors.Wrap(err, "invalid chat ID")
134+
}
135+
var topics []int64
136+
if len(parts) == 2 {
137+
topicParts := strings.Split(parts[1], ";")
138+
for _, topicPart := range topicParts {
139+
topic, err := strconv.ParseInt(topicPart, 10, 64)
140+
if err != nil {
141+
return nil, errors.Wrapf(err, "invalid topic %q for chat ID %d", topicPart, id)
153142
}
143+
topics = append(topics, topic)
154144
}
155-
chatIDs = append(chatIDs, chatID{
156-
id: id,
157-
topics: topics,
158-
})
159-
default:
160-
return nil, errors.Errorf("invalid chat ID %v (type=%T)", entry, entry)
161145
}
146+
chatIDs = append(chatIDs, chatID{
147+
id: id,
148+
topics: topics,
149+
})
162150
}
163151
return chatIDs, nil
164152
}

internal/notif/telegram/client_test.go

+8-20
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ import (
1010
func TestParseChatIDs(t *testing.T) {
1111
tests := []struct {
1212
name string
13-
entries []interface{}
13+
entries []string
1414
expected []chatID
1515
err error
1616
}{
1717
{
18-
name: "valid integers",
19-
entries: []interface{}{8547439, 1234567},
18+
name: "valid chat IDS",
19+
entries: []string{"8547439", "1234567"},
2020
expected: []chatID{
2121
{id: 8547439},
2222
{id: 1234567},
@@ -25,7 +25,7 @@ func TestParseChatIDs(t *testing.T) {
2525
},
2626
{
2727
name: "valid strings with topics",
28-
entries: []interface{}{"567891234:25", "891256734:25;12"},
28+
entries: []string{"567891234:25", "891256734:25;12"},
2929
expected: []chatID{
3030
{id: 567891234, topics: []int64{25}},
3131
{id: 891256734, topics: []int64{25, 12}},
@@ -34,37 +34,25 @@ func TestParseChatIDs(t *testing.T) {
3434
},
3535
{
3636
name: "invalid format",
37-
entries: []interface{}{"invalid_format"},
37+
entries: []string{"invalid_format"},
3838
expected: nil,
3939
err: errors.New(`invalid chat ID: strconv.ParseInt: parsing "invalid_format": invalid syntax`),
4040
},
41-
{
42-
name: "invalid type",
43-
entries: []interface{}{true},
44-
expected: nil,
45-
err: errors.New("invalid chat ID true (type=bool)"),
46-
},
4741
{
4842
name: "empty string",
49-
entries: []interface{}{""},
43+
entries: []string{""},
5044
expected: nil,
5145
err: errors.New(`invalid chat ID: strconv.ParseInt: parsing "": invalid syntax`),
5246
},
5347
{
5448
name: "string with invalid topic",
55-
entries: []interface{}{"567891234:invalid"},
49+
entries: []string{"567891234:invalid"},
5650
expected: nil,
5751
err: errors.New(`invalid topic "invalid" for chat ID 567891234: strconv.ParseInt: parsing "invalid": invalid syntax`),
5852
},
59-
{
60-
name: "mixed valid and invalid entries",
61-
entries: []interface{}{8547439, "567891234:25", "invalid_format", true},
62-
expected: nil,
63-
err: errors.New(`invalid chat ID: strconv.ParseInt: parsing "invalid_format": invalid syntax`),
64-
},
6553
{
6654
name: "invalid format with too many parts",
67-
entries: []interface{}{"567891234:25:extra"},
55+
entries: []string{"567891234:25:extra"},
6856
expected: nil,
6957
err: errors.New(`invalid chat ID "567891234:25:extra"`),
7058
},

0 commit comments

Comments
 (0)