This repository was archived by the owner on Jun 26, 2023. It is now read-only.
forked from eFishery/NeMo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.go
More file actions
111 lines (102 loc) · 2.81 KB
/
parser.go
File metadata and controls
111 lines (102 loc) · 2.81 KB
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
package main
import (
"encoding/json"
"fmt"
req "github.com/imroc/req"
"io/ioutil"
"os"
"strings"
)
var (
// TODO use struct for key priority to allow setting config via file
// that might need extra config validation though
defSupportedRespKeys = map[string]int{
"message": 1,
}
defSupportedRespKeysConfig = "SUPPORTED_KEYS_CONFIG"
// TODO handle error differently
// these are only for visualization purpose
errReqErr = func(dst string) string { return fmt.Sprintf("[req error: %s]", dst) }
errRespNotSupported = func(dst string) string { return fmt.Sprintf("[resp not supported error: %s]", dst) }
)
// nemoParser parses pesan for URL in {{url}} format
// if no URL is found in pesan, pesan is returned as is
// if URL is found, try POST request to url
// currently only supports JSON response with message key
func nemoParser(pesan string, Sessions Session) (string, error) {
urlCount := strings.Count(pesan, "{{")
if urlCount == 0 {
return pesan, nil
}
for i := 0; i < urlCount; i++ {
url := between(pesan, "{{", "}}")
r, err := req.Post(url, req.BodyJSON(Sessions))
if err != nil {
pesan = strings.Replace(pesan, fmt.Sprintf("{{%s}}", url), errReqErr(url), -1)
}
var m map[string]interface{}
r.ToJSON(&m)
// TODO maybe calls this in main to setup
sk := supportKey(os.Getenv(defSupportedRespKeysConfig))
k := lookupKey(m, sk)
if k == "" {
pesan = strings.Replace(pesan, fmt.Sprintf("{{%s}}", url), errRespNotSupported(url), -1)
continue
}
if m[k] != "" {
pesan = strings.Replace(pesan, fmt.Sprintf("{{%s}}", url), fmt.Sprintf("%v", m[k]), -1)
}
}
return pesan, nil
}
// lookupKey looks up if key exists in a map based on priority
// return empty string if key is not supported
// currently only supports first level key
// might want to improve the algorithm
// i.e return once a supported key is found with the assumption
// that response body has commonly used keys to denote response message
// it also does not care if the config sets multiple keys with the same
// priority, it uses whichever is found first
func lookupKey(m map[string]interface{}, sup map[string]int) string {
var key string
var prio int
for k, _ := range m {
for v, p := range sup {
if k != v {
continue
}
if key == "" {
key = v
prio = p
continue
} else if p < prio {
key = v
prio = p
continue
}
}
}
return key
}
// supportKey sets up keys support from JSON config file
// TODO validate config file
func supportKey(file string) map[string]int {
kp := make(map[string]int)
if file == "" {
return defSupportedRespKeys
}
b, err := ioutil.ReadFile(file)
if err != nil {
return defSupportedRespKeys
}
var m map[string]interface{}
json.Unmarshal(b, &m)
for k, v := range m {
pp, ok := v.(float64)
if !ok {
continue
}
kp[k] = int(pp)
}
return kp
}