-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
75 lines (57 loc) · 1.56 KB
/
response.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
package main
import (
"log"
"regexp"
"strings"
)
const (
ResolverGenerator = "generate"
ResolverCopy = "copy"
ResolverCall = "call"
)
type Response struct {
Label string `json:"label"`
Body string `json:"body"`
Latency int64 `json:"latency"`
StatusCode int `json:"status_code"`
Headers map[string]string `json:"headers"`
}
func (r Response) construct(data collector) Response {
log.Println("constructing response for label: ", r.Label)
placeholders := regexp.MustCompile(`(?m)({{.+?}})`).FindAllString(r.Body, -1)
for _, placeholder := range placeholders {
result := resolvePlaceholder(placeholder, data)
r.Body = strings.Replace(r.Body, placeholder, result, 1)
}
return r
}
func resolvePlaceholder(key string, data collector) string {
tokens := tokenize(key)
return resolve(tokens[0], tokens[1:], data)
}
func tokenize(str string) []string {
tokens := regexp.MustCompile(`(?m)(\s*\S+\s*)?`).FindAllString(str, -1)
tokens = tokens[1 : len(tokens)-1]
for i, v := range tokens {
tokens[i] = strings.TrimSpace(v)
}
return tokens
}
func resolve(key string, tokens []string, data collector) string {
switch key {
case ResolverGenerator:
return generate(tokens)
case ResolverCopy:
return copyFrom(tokens, data)
case ResolverCall:
return customCall(data, tokens)
}
return ""
}
func copyFrom(tokens []string, data collector) string {
if len(tokens) != 2 {
log.Println("error: invalid number of arguments in copy")
return ""
}
return data.get(tokens[0], tokens[1])
}