Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow unescaped json output #1432

Merged
merged 3 commits into from
Apr 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ this functionality might prove useful.
- [timestamp](#timestamp)
- [toJSON](#tojson)
- [toJSONPretty](#tojsonpretty)
- [toUnescapedJSON](#tounescapedjson)
eikenb marked this conversation as resolved.
Show resolved Hide resolved
- [toUnescapedJSONPretty](#tounescapedjsonpretty)
- [toLower](#tolower)
- [toTitle](#totitle)
- [toTOML](#totoml)
Expand Down Expand Up @@ -1970,7 +1972,41 @@ renders
}
```

Note: Consul stores all KV data as strings. Thus true is "true", 1 is "1", etc.
##### `toUnescapedJSON`

Takes the result from a `tree` or `ls` call and converts it into a JSON object without HTML escaping. This function comes in handy when working with db connection strings or URIs containing query parameters.

```liquid
{{ tree "config" | explode | toUnescapedJSON }}
```

renders

```javascript
{"admin":{"port":"1234"},"maxconns":"5","minconns":"2", "queryparams": "a?b=c&d=e"}
```

##### `toUnescapedJSONPretty`

Takes the result from a `tree` or `ls` call and converts it into a
pretty-printed JSON object without HTML escaping, indented by two spaces.

```liquid
{{ tree "config" | explode | toUnescapedJSONPretty }}
```

renders

```javascript
{
"admin": {
"port": "1234"
},
"maxconns": "5",
"minconns": "2",
"queryparams": "a?b=c&d=e"
}
```

##### `toLower`

Expand Down
24 changes: 24 additions & 0 deletions template/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1121,6 +1121,30 @@ func toJSONPretty(m map[string]interface{}) (string, error) {
return string(bytes.TrimSpace(result)), err
}

// toUnescapedJSON converts the given structure into a deeply nested JSON string without HTML escaping.
func toUnescapedJSON(i interface{}) (string, error) {
buf := &bytes.Buffer{}
encoder := json.NewEncoder(buf)
encoder.SetEscapeHTML(false)
if err := encoder.Encode(i); err != nil {
return "", errors.Wrap(err, "toUnescapedJSON")
}
return strings.TrimRight(buf.String(), "\r\n"), nil
}

// toUnescapedJSONPretty converts the given structure into a deeply nested pretty JSON
// string without HTML escaping.
func toUnescapedJSONPretty(m map[string]interface{}) (string, error) {
buf := &bytes.Buffer{}
encoder := json.NewEncoder(buf)
encoder.SetEscapeHTML(false)
encoder.SetIndent("", " ")
if err := encoder.Encode(m); err != nil {
return "", errors.Wrap(err, "toUnescapedJSONPretty")
}
return strings.TrimRight(buf.String(), "\r\n"), nil
}

// toTitle converts the given string (usually by a pipe) to titlecase.
func toTitle(s string) (string, error) {
return strings.Title(s), nil
Expand Down
86 changes: 44 additions & 42 deletions template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,48 +248,50 @@ func funcMap(i *funcMapInput) template.FuncMap {
"scratch": func() *Scratch { return &scratch },

// Helper functions
"base64Decode": base64Decode,
"base64Encode": base64Encode,
"base64URLDecode": base64URLDecode,
"base64URLEncode": base64URLEncode,
"byKey": byKey,
"byTag": byTag,
"contains": contains,
"containsAll": containsSomeFunc(true, true),
"containsAny": containsSomeFunc(false, false),
"containsNone": containsSomeFunc(true, false),
"containsNotAll": containsSomeFunc(false, true),
"env": envFunc(i.env),
"executeTemplate": executeTemplateFunc(i.t),
"explode": explode,
"explodeMap": explodeMap,
"in": in,
"indent": indent,
"loop": loop,
"join": join,
"trimSpace": trimSpace,
"parseBool": parseBool,
"parseFloat": parseFloat,
"parseInt": parseInt,
"parseJSON": parseJSON,
"parseUint": parseUint,
"parseYAML": parseYAML,
"plugin": plugin,
"regexReplaceAll": regexReplaceAll,
"regexMatch": regexMatch,
"replaceAll": replaceAll,
"sha256Hex": sha256Hex,
"timestamp": timestamp,
"toLower": toLower,
"toJSON": toJSON,
"toJSONPretty": toJSONPretty,
"toTitle": toTitle,
"toTOML": toTOML,
"toUpper": toUpper,
"toYAML": toYAML,
"split": split,
"byMeta": byMeta,
"sockaddr": sockaddr,
"base64Decode": base64Decode,
"base64Encode": base64Encode,
"base64URLDecode": base64URLDecode,
"base64URLEncode": base64URLEncode,
"byKey": byKey,
"byTag": byTag,
"contains": contains,
"containsAll": containsSomeFunc(true, true),
"containsAny": containsSomeFunc(false, false),
"containsNone": containsSomeFunc(true, false),
"containsNotAll": containsSomeFunc(false, true),
"env": envFunc(i.env),
"executeTemplate": executeTemplateFunc(i.t),
"explode": explode,
"explodeMap": explodeMap,
"in": in,
"indent": indent,
"loop": loop,
"join": join,
"trimSpace": trimSpace,
"parseBool": parseBool,
"parseFloat": parseFloat,
"parseInt": parseInt,
"parseJSON": parseJSON,
"parseUint": parseUint,
"parseYAML": parseYAML,
"plugin": plugin,
"regexReplaceAll": regexReplaceAll,
"regexMatch": regexMatch,
"replaceAll": replaceAll,
"sha256Hex": sha256Hex,
"timestamp": timestamp,
"toLower": toLower,
"toJSON": toJSON,
"toJSONPretty": toJSONPretty,
"toUnescapedJSON": toUnescapedJSON,
"toUnescapedJSONPretty": toUnescapedJSONPretty,
"toTitle": toTitle,
"toTOML": toTOML,
"toUpper": toUpper,
"toYAML": toYAML,
"split": split,
"byMeta": byMeta,
"sockaddr": sockaddr,
// Math functions
"add": add,
"subtract": subtract,
Expand Down
38 changes: 38 additions & 0 deletions template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1547,6 +1547,44 @@ func TestTemplate_Execute(t *testing.T) {
"[\"a\",\"b\",\"c\"]",
false,
},
{
"helper_toUnescapedJSON",
&NewTemplateInput{
Contents: `{{ "a?b&c,x?y&z" | split "," | toUnescapedJSON }}`,
},
&ExecuteInput{
Brain: NewBrain(),
},
"[\"a?b&c\",\"x?y&z\"]",
false,
},
{
"helper_toUnescapedJSONPretty",
&NewTemplateInput{
Contents: `{{ tree "key" | explode | toUnescapedJSONPretty }}`,
},
&ExecuteInput{
Brain: func() *Brain {
b := NewBrain()
d, err := dep.NewKVListQuery("key")
if err != nil {
t.Fatal(err)
}
b.Remember(d, []*dep.KeyPair{
&dep.KeyPair{Key: "a", Value: "b&c"},
&dep.KeyPair{Key: "x", Value: "y&z"},
&dep.KeyPair{Key: "k", Value: "<>&&"},
})
return b
}(),
},
`{
"a": "b&c",
"k": "<>&&",
"x": "y&z"
}`,
false,
},
{
"helper_toLower",
&NewTemplateInput{
Expand Down