forked from Azure/acs-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
58 lines (52 loc) · 1.53 KB
/
json.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
package acsengine
import (
"encoding/json"
"strings"
)
// PrettyPrintArmTemplate will pretty print the arm template ensuring ordered by params, vars, resources, and outputs
func PrettyPrintArmTemplate(template string) (string, error) {
translateParams := [][]string{
{"\"parameters\"", "\"dparameters\""},
{"\"variables\"", "\"evariables\""},
{"\"resources\"", "\"fresources\""},
{"\"outputs\"", "\"zoutputs\""},
// there is a bug in ARM where it doesn't correctly translate back '\u003e' (>)
{">", "GREATERTHAN"},
{"<", "LESSTHAN"},
{"&", "AMPERSAND"},
}
template = translateJSON(template, translateParams, false)
var err error
if template, err = PrettyPrintJSON(template); err != nil {
return "", err
}
template = translateJSON(template, translateParams, true)
return template, nil
}
// PrettyPrintJSON will pretty print the json into
func PrettyPrintJSON(content string) (string, error) {
var data map[string]interface{}
if err := json.Unmarshal([]byte(content), &data); err != nil {
return "", err
}
prettyprint, err := json.MarshalIndent(data, "", " ")
if err != nil {
return "", err
}
return string(prettyprint), nil
}
func translateJSON(content string, translateParams [][]string, reverseTranslate bool) string {
for _, tuple := range translateParams {
if len(tuple) != 2 {
panic("string tuples must be of size 2")
}
a := tuple[0]
b := tuple[1]
if reverseTranslate {
content = strings.Replace(content, b, a, -1)
} else {
content = strings.Replace(content, a, b, -1)
}
}
return content
}