From 4342f6521106278d6cd000a4c5914b874adb8f3c Mon Sep 17 00:00:00 2001 From: Julien Riou Date: Sat, 27 Feb 2021 18:31:11 +0100 Subject: [PATCH] Use array of key/value for Twitter hashtags (#1) With Go, maps are not ordered the way they are declared. Keys can be read at any order. When a pattern is too large ("rtx 3060"), when placed first, it can match a name of another product ("rtx 3060 ti"). When placed second, the good hashtag is chosen. This commit uses an array of maps because arrays are ordered. Signed-off-by: Julien Riou --- README.md | 2 +- config.go | 10 +++++----- twitter.go | 10 ++++++---- twitter_test.go | 28 ++++++++++++++-------------- 4 files changed, 26 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index dca53a4..d675b50 100644 --- a/README.md +++ b/README.md @@ -72,7 +72,7 @@ Options: * `consumer_secret`: API secret of your Twitter application * `access_token`: authentication token generated for your Twitter account * `access_token_secret`: authentication token secret generated for your Twitter account - * `hashtags`: map of key/values used to append hashtags to each tweet. Key is the pattern to match in the product name, value is the string to append to the tweet. For example, `{"twitter": {"hashtags": {"rtx 3090": "#nvidia #rtx3090"}}}` will detect `rtx 3090` to append `#nvidia #rtx3090` at the end of the tweet. + * `hashtags`: list of key/value used to append hashtags to each tweet. Key is the pattern to match in the product name, value is the string to append to the tweet. For example, `{"twitter": {"hashtags": [{"rtx 3090": "#nvidia #rtx3090"}]}}` will detect `rtx 3090` to append `#nvidia #rtx3090` at the end of the tweet. * `include_regex` (optional): include products with a name matching this regexp * `exclude_regex` (optional): exclude products with a name matching this regexp diff --git a/config.go b/config.go index 0fa79a8..c19c223 100644 --- a/config.go +++ b/config.go @@ -16,11 +16,11 @@ type Config struct { // TwitterConfig to store Twitter API secrets type TwitterConfig struct { - ConsumerKey string `json:"consumer_key"` - ConsumerSecret string `json:"consumer_secret"` - AccessToken string `json:"access_token"` - AccessTokenSecret string `json:"access_token_secret"` - Hashtags map[string]string `json:"hashtags"` + ConsumerKey string `json:"consumer_key"` + ConsumerSecret string `json:"consumer_secret"` + AccessToken string `json:"access_token"` + AccessTokenSecret string `json:"access_token_secret"` + Hashtags []map[string]string `json:"hashtags"` } // NewConfig creates a Config struct diff --git a/twitter.go b/twitter.go index 4ad0d88..cdfbf08 100644 --- a/twitter.go +++ b/twitter.go @@ -25,7 +25,7 @@ type TwitterNotifier struct { db *gorm.DB client *twitter.Client user *twitter.User - hashtagsMap map[string]string + hashtagsMap []map[string]string } // NewTwitterNotifier creates a TwitterNotifier @@ -80,9 +80,11 @@ func (c *TwitterNotifier) replyToTweet(tweetID int64, message string) (int64, er // parse product name to build a list of hashtags func (c *TwitterNotifier) buildHashtags(productName string) string { productName = strings.ToLower(productName) - for pattern, value := range c.hashtagsMap { - if ok, _ := regexp.MatchString(pattern, productName); ok { - return value + for _, rule := range c.hashtagsMap { + for pattern, value := range rule { + if ok, _ := regexp.MatchString(pattern, productName); ok { + return value + } } } return "" diff --git a/twitter_test.go b/twitter_test.go index c583ce4..2c652f7 100644 --- a/twitter_test.go +++ b/twitter_test.go @@ -27,20 +27,20 @@ func TestBuildHashtags(t *testing.T) { client: nil, user: nil, db: nil, - hashtagsMap: map[string]string{ - "rtx 3060 ti": "#nvidia #rtx3060ti", - "rtx 3060ti": "#nvidia #rtx3060ti", - "rtx 3060": "#nvidia #rtx3060", - "rtx 3070": "#nvidia #rtx3070", - "rtx 3080": "#nvidia #rtx3080", - "rtx 3090": "#nvidia #rtx3090", - "rx 6800 xt": "#amd #rx6800xt", - "rx 6800xt": "#amd #rx6800xt", - "rx 6900xt": "#amd #rx6900xt", - "rx 6900 xt": "#amd #rx6900xt", - "rx 6800": "#amd #rx6800", - "rx 5700 xt": "#amd #rx5700xt", - "rx 5700xt": "#amd #rx5700xt", + hashtagsMap: []map[string]string{ + {"rtx 3060 ti": "#nvidia #rtx3060ti"}, + {"rtx 3060ti": "#nvidia #rtx3060ti"}, + {"rtx 3060": "#nvidia #rtx3060"}, + {"rtx 3070": "#nvidia #rtx3070"}, + {"rtx 3080": "#nvidia #rtx3080"}, + {"rtx 3090": "#nvidia #rtx3090"}, + {"rx 6800 xt": "#amd #rx6800xt"}, + {"rx 6800xt": "#amd #rx6800xt"}, + {"rx 6900xt": "#amd #rx6900xt"}, + {"rx 6900 xt": "#amd #rx6900xt"}, + {"rx 6800": "#amd #rx6800"}, + {"rx 5700 xt": "#amd #rx5700xt"}, + {"rx 5700xt": "#amd #rx5700xt"}, }, }