-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnotification.go
52 lines (41 loc) · 1.03 KB
/
notification.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
//Copyright (c) 2017 Phil
package apollo
import (
"encoding/json"
"sync"
)
type notification struct {
NamespaceName string `json:"namespaceName,omitempty"`
NotificationID int `json:"notificationId,omitempty"`
}
type notificationRepo struct {
notifications sync.Map
}
func (n *notificationRepo) setNotificationID(namesapce string, notificationID int) {
n.notifications.Store(namesapce, notificationID)
}
func (n *notificationRepo) getNotificationID(namespace string) (int, bool) {
if val, ok := n.notifications.Load(namespace); ok {
if ret, ok := val.(int); ok {
return ret, true
}
}
return defaultNotificationID, false
}
func (n *notificationRepo) toString() string {
var notifications []*notification
n.notifications.Range(func(key, val interface{}) bool {
k, _ := key.(string)
v, _ := val.(int)
notifications = append(notifications, ¬ification{
NamespaceName: k,
NotificationID: v,
})
return true
})
bts, err := json.Marshal(¬ifications)
if err != nil {
return ""
}
return string(bts)
}