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 <julien@riou.xyz>
This commit is contained in:
parent
40b0ba999a
commit
4342f65211
4 changed files with 26 additions and 24 deletions
10
twitter.go
10
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 ""
|
||||
|
|
Reference in a new issue