Skip to content

Commit

Permalink
Merge pull request #13 from MarhyCZ/main
Browse files Browse the repository at this point in the history
allow notifications to be added as another event
  • Loading branch information
donseba committed Mar 16, 2024
2 parents 44d9b4b + a153440 commit f6327e3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 21 deletions.
55 changes: 37 additions & 18 deletions trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,42 @@ func (t *Trigger) AddEventObject(event string, details map[string]any) *Trigger
return t.add(eventContent{event: event, data: details})
}

func (t *Trigger) AddSuccess(message string, vars ...map[string]any) {
t.addNotifyObject(notificationSuccess, message, vars...)
}

func (t *Trigger) AddInfo(message string, vars ...map[string]any) {
t.addNotifyObject(notificationInfo, message, vars...)
}

func (t *Trigger) AddWarning(message string, vars ...map[string]any) {
t.addNotifyObject(notificationWarning, message, vars...)
}

func (t *Trigger) AddError(message string, vars ...map[string]any) {
t.addNotifyObject(notificationError, message, vars...)
}

func (t *Trigger) addNotifyObject(nt notificationType, message string, vars ...map[string]any) *Trigger {
details := map[string]any{
notificationKeyLevel: nt,
notificationKeyMessage: message,
}

if len(vars) > 0 {
for _, m := range vars {
for k, v := range m {
if k == notificationKeyLevel || k == notificationKeyMessage {
k = "_" + k
}
details[k] = v
}
}
}

return t.AddEventObject(DefaultNotificationKey, details)
}

// String returns the string representation of the Trigger set
func (t *Trigger) String() string {
if t.onlySimple {
Expand Down Expand Up @@ -90,24 +126,7 @@ func (n *notificationType) String() string {
}

func (h *Handler) notifyObject(nt notificationType, message string, vars ...map[string]any) {
details := map[string]any{
notificationKeyLevel: nt,
notificationKeyMessage: message,
}

if len(vars) > 0 {
for _, m := range vars {
for k, v := range m {
if k == notificationKeyLevel || k == notificationKeyMessage {
k = "_" + k
}
details[k] = v
}
}
}

t := NewTrigger().AddEventObject(DefaultNotificationKey, details)

t := NewTrigger().addNotifyObject(nt, message, vars...)
h.TriggerWithObject(t)
}

Expand Down
5 changes: 2 additions & 3 deletions trigger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ func TestNewTriggerMixedNested(t *testing.T) {
trigger.AddEvent("foo").
AddEventDetailed("bar", "baz").
AddEventDetailed("qux", "quux").
AddEventObject("corge", map[string]any{"grault": "garply", "waldo": "fred", "plugh": "xyzzy", "thud": map[string]any{"foo": "bar", "baz": "qux"}})

expected := `{"bar":"baz","corge":{"grault":"garply","plugh":"xyzzy","thud":{"baz":"qux","foo":"bar"},"waldo":"fred"},"foo":"","qux":"quux"}`
AddEventObject("corge", map[string]any{"grault": "garply", "waldo": "fred", "plugh": "xyzzy", "thud": map[string]any{"foo": "bar", "baz": "qux"}}).AddSuccess("successfully tested", map[string]any{"foo": "bar", "baz": "qux"})
expected := `{"bar":"baz","corge":{"grault":"garply","plugh":"xyzzy","thud":{"baz":"qux","foo":"bar"},"waldo":"fred"},"foo":"","qux":"quux","showMessage":{"baz":"qux","foo":"bar","level":"success","message":"successfully tested"}}`

if trigger.String() != expected {
t.Errorf("expected trigger to be %v, got %v", expected, trigger.String())
Expand Down

0 comments on commit f6327e3

Please sign in to comment.