-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmarshal.go
68 lines (51 loc) · 1.36 KB
/
marshal.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
package caddyyaml
import (
"encoding/json"
"fmt"
"strings"
"github.com/ghodss/yaml"
)
func yamlToJSON(b []byte) ([]byte, error) {
var tmp map[string]interface{}
if err := yaml.Unmarshal(b, &tmp); err != nil {
return nil, err
}
// discard all entries with x- prefix
for key := range tmp {
if strings.HasPrefix(key, "x-") {
// this is safe to do
// https://stackoverflow.com/a/23230406/524060
delete(tmp, key)
}
}
return json.Marshal(tmp)
}
func varsFromBody(b []byte) (map[string]interface{}, error) {
var tmp map[string]interface{}
var vars map[string]interface{}
varsBytes, err := extractVariables(b)
if err != nil {
return nil, err
}
varsBytes, err = applyTemplate(varsBytes, nil)
if err != nil {
return nil, err
}
if err := yaml.Unmarshal(varsBytes, &tmp); err != nil {
return nil, err
}
vars = make(map[string]interface{})
for xkey, val := range tmp {
key := xkey[2:] // discard x- prefix
// go template prohibits hyphen `-` in field names.
if strings.Index(key, "-") > 0 {
return nil, fmt.Errorf("template: apart from 'x-' prefix, '-' is not permitted in extension field name for %s", xkey)
}
// go template uses dot `.` for nesting.
if strings.Index(key, ".") > 0 {
return nil, fmt.Errorf("template: '.' is not permitted in extension field name for %s", xkey)
}
vars[key] = val
}
return vars, nil
}