-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mattermost): Add emoji support (#1028)
- Loading branch information
Showing
5 changed files
with
167 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package emoji_provider | ||
|
||
import ( | ||
"fmt" | ||
"maps" | ||
|
||
"github.com/moira-alert/moira" | ||
) | ||
|
||
var defaultStateEmoji = map[moira.State]string{ | ||
moira.StateOK: ":moira-state-ok:", | ||
moira.StateWARN: ":moira-state-warn:", | ||
moira.StateERROR: ":moira-state-error:", | ||
moira.StateNODATA: ":moira-state-nodata:", | ||
moira.StateEXCEPTION: ":moira-state-exception:", | ||
moira.StateTEST: ":moira-state-test:", | ||
} | ||
|
||
// emojiProvider is struct for get emoji by trigger State. | ||
type emojiProvider struct { | ||
defaultValue string | ||
stateEmojiMap map[moira.State]string | ||
} | ||
|
||
// StateEmojiGetter is interface for emojiProvider. | ||
type StateEmojiGetter interface { | ||
GetStateEmoji(subjectState moira.State) string | ||
} | ||
|
||
// NewEmojiProvider is construct for emojiProvider. | ||
func NewEmojiProvider(defaultValue string, stateEmojiMap map[string]string) (StateEmojiGetter, error) { | ||
emojiMap := maps.Clone(defaultStateEmoji) | ||
|
||
for state, emoji := range stateEmojiMap { | ||
converted := moira.State(state) | ||
if _, ok := emojiMap[converted]; !ok { | ||
return nil, fmt.Errorf("undefined Moira's state: %s", state) | ||
} | ||
emojiMap[converted] = emoji | ||
} | ||
|
||
return &emojiProvider{ | ||
defaultValue: defaultValue, | ||
stateEmojiMap: emojiMap, | ||
}, nil | ||
} | ||
|
||
// GetStateEmoji returns corresponding state emoji. | ||
func (em *emojiProvider) GetStateEmoji(subjectState moira.State) string { | ||
if emoji, ok := em.stateEmojiMap[subjectState]; ok { | ||
return emoji | ||
} | ||
|
||
return em.defaultValue | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package emoji_provider | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/moira-alert/moira" | ||
|
||
. "github.com/smartystreets/goconvey/convey" | ||
) | ||
|
||
func TestNewEmojiProvider(t *testing.T) { | ||
Convey("Successful creation, no errors", t, func() { | ||
em, err := NewEmojiProvider( | ||
"default", | ||
map[string]string{ | ||
"OK": "super_ok", | ||
"WARN": "super_warn", | ||
"ERROR": "super_error", | ||
"TEST": "super_test", | ||
"EXCEPTION": "super_exception", | ||
"NODATA": "super_nodata", | ||
}, | ||
) | ||
So(err, ShouldBeNil) | ||
expected := &emojiProvider{ | ||
defaultValue: "default", | ||
stateEmojiMap: map[moira.State]string{ | ||
"OK": "super_ok", | ||
"WARN": "super_warn", | ||
"ERROR": "super_error", | ||
"TEST": "super_test", | ||
"EXCEPTION": "super_exception", | ||
"NODATA": "super_nodata", | ||
}, | ||
} | ||
So(em, ShouldResemble, expected) | ||
}) | ||
|
||
Convey("Unsuccessful creation, has error", t, func() { | ||
em, err := NewEmojiProvider( | ||
"default", | ||
map[string]string{ | ||
"OK": "super_ok", | ||
"dfgdf": "super_warn", | ||
"ERROR": "super_error", | ||
"TEST": "super_test", | ||
"EXCEPTION": "super_exception", | ||
"NODATA": "super_nodata", | ||
}, | ||
) | ||
So(err.Error(), ShouldResemble, "undefined Moira's state: dfgdf") | ||
So(em, ShouldBeNil) | ||
}) | ||
} | ||
|
||
func TestEmojiProvider_GetStateEmoji(t *testing.T) { | ||
Convey("Check state emoji", t, func() { | ||
em := &emojiProvider{stateEmojiMap: defaultStateEmoji, defaultValue: "default_value"} | ||
So(em.GetStateEmoji(moira.StateOK), ShouldResemble, ":moira-state-ok:") | ||
So(em.GetStateEmoji(moira.StateWARN), ShouldResemble, ":moira-state-warn:") | ||
So(em.GetStateEmoji(moira.StateERROR), ShouldResemble, ":moira-state-error:") | ||
So(em.GetStateEmoji(moira.StateNODATA), ShouldResemble, ":moira-state-nodata:") | ||
So(em.GetStateEmoji(moira.StateEXCEPTION), ShouldResemble, ":moira-state-exception:") | ||
So(em.GetStateEmoji(moira.StateTEST), ShouldResemble, ":moira-state-test:") | ||
So(em.GetStateEmoji("dfdfdf"), ShouldResemble, "default_value") | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters