-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathtrigger.go
151 lines (120 loc) · 3.87 KB
/
trigger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package htmx
import (
"encoding/json"
"strings"
)
type eventContent struct {
event string
data any
}
type Trigger struct {
triggers []eventContent
onlySimple bool
}
// NewTrigger returns a new Trigger set
func NewTrigger() *Trigger {
return &Trigger{
triggers: make([]eventContent, 0),
onlySimple: true,
}
}
// add adds a trigger to the Trigger set
func (t *Trigger) add(trigger eventContent) *Trigger {
t.triggers = append(t.triggers, trigger)
return t
}
func (t *Trigger) AddEvent(event string) *Trigger {
return t.add(eventContent{event: event, data: ""})
}
// AddEventDetailed adds a trigger to the Trigger set
func (t *Trigger) AddEventDetailed(event, message string) *Trigger {
t.onlySimple = false
return t.add(eventContent{event: event, data: message})
}
// AddEventObject adds a trigger to the Trigger set
func (t *Trigger) AddEventObject(event string, details map[string]any) *Trigger {
t.onlySimple = false
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 {
data := make([]string, len(t.triggers))
for i, trigger := range t.triggers {
data[i] = trigger.event
}
return strings.Join(data, ", ")
}
triggerMap := make(map[string]any)
for _, tr := range t.triggers {
triggerMap[tr.event] = tr.data
}
data, _ := json.Marshal(triggerMap)
return string(data)
}
const (
// notificationSuccess is the success notification type
notificationSuccess notificationType = "success"
// notificationInfo is the info notification type
notificationInfo notificationType = "info"
// notificationWarning is the warning notification type
notificationWarning notificationType = "warning"
// notificationError is the error notification type
notificationError notificationType = "error"
// notificationKeyLevel is the notification level key
notificationKeyLevel = "level"
// notificationKeyMessage is the notification message key
notificationKeyMessage = "message"
)
type notificationType string
func (n *notificationType) String() string {
return string(*n)
}
func (h *Handler) notifyObject(nt notificationType, message string, vars ...map[string]any) {
t := NewTrigger().addNotifyObject(nt, message, vars...)
h.TriggerWithObject(t)
}
func (h *Handler) TriggerSuccess(message string, vars ...map[string]any) {
h.notifyObject(notificationSuccess, message, vars...)
}
func (h *Handler) TriggerInfo(message string, vars ...map[string]any) {
h.notifyObject(notificationInfo, message, vars...)
}
func (h *Handler) TriggerWarning(message string, vars ...map[string]any) {
h.notifyObject(notificationWarning, message, vars...)
}
func (h *Handler) TriggerError(message string, vars ...map[string]any) {
h.notifyObject(notificationError, message, vars...)
}
func (h *Handler) TriggerCustom(custom, message string, vars ...map[string]any) {
h.notifyObject(notificationType(custom), message, vars...)
}