-
Notifications
You must be signed in to change notification settings - Fork 77
/
alerts.go
56 lines (53 loc) · 1.09 KB
/
alerts.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
package main
import (
"encoding/json"
"io"
"os"
)
// return ret
func GetActiveAlert(lang string) ([]map[string]interface{}, error) {
alertsFile, err := os.Open("alerts.json")
if err != nil {
return nil, err
}
defer alertsFile.Close()
byteValue, err := io.ReadAll(alertsFile)
if err != nil {
return nil, err
}
var alerts []map[string]interface{}
err = json.Unmarshal(byteValue, &alerts)
if err != nil {
return nil, err
}
ret := []map[string]interface{}{}
for _, a := range alerts {
// convert to bool
active := a["active"].(bool)
if active {
if lang == "id" {
if _, ok := a["iDD"]; ok {
lang = "iDD"
}
} else if _, ok := a[lang]; !ok {
lang = "en"
}
retItem := map[string]interface{}{
"id": a["id"],
"priority": a["priority"],
"active": a["active"],
}
if _, ok := a["timestamp"]; ok {
retItem["timestamp"] = a["timestamp"]
}
if _, ok := a["link"]; ok {
retItem["link"] = a["link"]
}
for k, v := range a[lang].(map[string]interface{}) {
retItem[k] = v
}
ret = append(ret, retItem)
}
}
return ret, nil
}