From a27d3f50191be3ae725114eda7f0d9f9d66e8b47 Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Fri, 20 Dec 2024 18:25:34 +0100 Subject: [PATCH] telegram: align chatIDs and chatIDsFile format handling --- internal/notif/telegram/client.go | 56 ++++++++++---------------- internal/notif/telegram/client_test.go | 28 ++++--------- 2 files changed, 30 insertions(+), 54 deletions(-) diff --git a/internal/notif/telegram/client.go b/internal/notif/telegram/client.go index 3b884e2e..c1329e5c 100644 --- a/internal/notif/telegram/client.go +++ b/internal/notif/telegram/client.go @@ -49,10 +49,7 @@ func (c *Client) Send(entry model.NotifEntry) error { return errors.Wrap(err, "cannot retrieve token secret for Telegram notifier") } - var cids []interface{} - for _, cid := range c.cfg.ChatIDs { - cids = append(cids, cid) - } + cids := c.cfg.ChatIDs cidsRaw, err := utl.GetSecret("", c.cfg.ChatIDsFile) if err != nil { return errors.Wrap(err, "cannot retrieve chat IDs secret for Telegram notifier") @@ -124,41 +121,32 @@ func (c *Client) Send(entry model.NotifEntry) error { return nil } -func parseChatIDs(entries []interface{}) ([]chatID, error) { +func parseChatIDs(entries []string) ([]chatID, error) { var chatIDs []chatID for _, entry := range entries { - switch v := entry.(type) { - case int: - chatIDs = append(chatIDs, chatID{id: int64(v)}) - case int64: - chatIDs = append(chatIDs, chatID{id: v}) - case string: - parts := strings.Split(v, ":") - if len(parts) < 1 || len(parts) > 2 { - return nil, errors.Errorf("invalid chat ID %q", v) - } - id, err := strconv.ParseInt(parts[0], 10, 64) - if err != nil { - return nil, errors.Wrap(err, "invalid chat ID") - } - var topics []int64 - if len(parts) == 2 { - topicParts := strings.Split(parts[1], ";") - for _, topicPart := range topicParts { - topic, err := strconv.ParseInt(topicPart, 10, 64) - if err != nil { - return nil, errors.Wrapf(err, "invalid topic %q for chat ID %d", topicPart, id) - } - topics = append(topics, topic) + parts := strings.Split(entry, ":") + if len(parts) < 1 || len(parts) > 2 { + return nil, errors.Errorf("invalid chat ID %q", entry) + } + id, err := strconv.ParseInt(parts[0], 10, 64) + if err != nil { + return nil, errors.Wrap(err, "invalid chat ID") + } + var topics []int64 + if len(parts) == 2 { + topicParts := strings.Split(parts[1], ";") + for _, topicPart := range topicParts { + topic, err := strconv.ParseInt(topicPart, 10, 64) + if err != nil { + return nil, errors.Wrapf(err, "invalid topic %q for chat ID %d", topicPart, id) } + topics = append(topics, topic) } - chatIDs = append(chatIDs, chatID{ - id: id, - topics: topics, - }) - default: - return nil, errors.Errorf("invalid chat ID %v (type=%T)", entry, entry) } + chatIDs = append(chatIDs, chatID{ + id: id, + topics: topics, + }) } return chatIDs, nil } diff --git a/internal/notif/telegram/client_test.go b/internal/notif/telegram/client_test.go index 29fbdca5..cbaccb8b 100644 --- a/internal/notif/telegram/client_test.go +++ b/internal/notif/telegram/client_test.go @@ -10,13 +10,13 @@ import ( func TestParseChatIDs(t *testing.T) { tests := []struct { name string - entries []interface{} + entries []string expected []chatID err error }{ { - name: "valid integers", - entries: []interface{}{8547439, 1234567}, + name: "valid chat IDS", + entries: []string{"8547439", "1234567"}, expected: []chatID{ {id: 8547439}, {id: 1234567}, @@ -25,7 +25,7 @@ func TestParseChatIDs(t *testing.T) { }, { name: "valid strings with topics", - entries: []interface{}{"567891234:25", "891256734:25;12"}, + entries: []string{"567891234:25", "891256734:25;12"}, expected: []chatID{ {id: 567891234, topics: []int64{25}}, {id: 891256734, topics: []int64{25, 12}}, @@ -34,37 +34,25 @@ func TestParseChatIDs(t *testing.T) { }, { name: "invalid format", - entries: []interface{}{"invalid_format"}, + entries: []string{"invalid_format"}, expected: nil, err: errors.New(`invalid chat ID: strconv.ParseInt: parsing "invalid_format": invalid syntax`), }, - { - name: "invalid type", - entries: []interface{}{true}, - expected: nil, - err: errors.New("invalid chat ID true (type=bool)"), - }, { name: "empty string", - entries: []interface{}{""}, + entries: []string{""}, expected: nil, err: errors.New(`invalid chat ID: strconv.ParseInt: parsing "": invalid syntax`), }, { name: "string with invalid topic", - entries: []interface{}{"567891234:invalid"}, + entries: []string{"567891234:invalid"}, expected: nil, err: errors.New(`invalid topic "invalid" for chat ID 567891234: strconv.ParseInt: parsing "invalid": invalid syntax`), }, - { - name: "mixed valid and invalid entries", - entries: []interface{}{8547439, "567891234:25", "invalid_format", true}, - expected: nil, - err: errors.New(`invalid chat ID: strconv.ParseInt: parsing "invalid_format": invalid syntax`), - }, { name: "invalid format with too many parts", - entries: []interface{}{"567891234:25:extra"}, + entries: []string{"567891234:25:extra"}, expected: nil, err: errors.New(`invalid chat ID "567891234:25:extra"`), },