-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinjector.go
55 lines (48 loc) · 1.24 KB
/
injector.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
package main
import "github.com/Kong/go-pdk"
type Injector struct {
Context map[string]map[string]Template `json:"context" yaml:"contexts"`
Headers map[string]Template `json:"headers" yaml:"headers"`
QueryParams map[string]Template `json:"query_params" yaml:"query_params"`
}
type configInjector struct {
Context map[string]map[string]string `json:"context" yaml:"context"`
Headers map[string]string `json:"headers" yaml:"headers"`
QueryParams map[string]string `json:"query_params" yaml:"query_params"`
}
func (i Injector) Inject(kong *pdk.PDK, data Data) (err error) {
for k, v := range i.Context {
m := map[string]interface{}{}
for k1, v1 := range v {
s, err := v1.ToString(data)
if err != nil {
return err
}
m[k1] = s
}
kong.Ctx.SetShared(k, m)
}
h := map[string][]string{}
for k, v := range i.Headers {
s, err := v.ToString(data)
if err != nil {
return err
}
h[k] = []string{s}
}
if len(h) > 0 {
kong.ServiceRequest.SetHeaders(h)
}
q := map[string][]string{}
for k, v := range i.QueryParams {
s, err := v.ToString(data)
if err != nil {
return err
}
q[k] = []string{s}
}
if len(q) > 0 {
kong.ServiceRequest.SetQuery(q)
}
return
}