1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Disable HTML escaping for JSON in formatter

Signed-off-by: Boaz Shuster <ripcurld.github@gmail.com>
This commit is contained in:
Boaz Shuster 2017-03-27 19:50:53 +03:00
parent 7ca86796c9
commit 5766317e33
2 changed files with 18 additions and 2 deletions

View file

@ -1,6 +1,7 @@
package templates package templates
import ( import (
"bytes"
"encoding/json" "encoding/json"
"strings" "strings"
"text/template" "text/template"
@ -10,8 +11,12 @@ import (
// functions provided to every template. // functions provided to every template.
var basicFunctions = template.FuncMap{ var basicFunctions = template.FuncMap{
"json": func(v interface{}) string { "json": func(v interface{}) string {
a, _ := json.Marshal(v) buf := &bytes.Buffer{}
return string(a) enc := json.NewEncoder(buf)
enc.SetEscapeHTML(false)
enc.Encode(v)
// Remove the trailing new line added by the encoder
return strings.TrimSpace(buf.String())
}, },
"split": strings.Split, "split": strings.Split,
"join": strings.Join, "join": strings.Join,

View file

@ -7,6 +7,17 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
// Github #32120
func TestParseJSONFunctions(t *testing.T) {
tm, err := Parse(`{{json .Ports}}`)
assert.NoError(t, err)
var b bytes.Buffer
assert.NoError(t, tm.Execute(&b, map[string]string{"Ports": "0.0.0.0:2->8/udp"}))
want := "\"0.0.0.0:2->8/udp\""
assert.Equal(t, want, b.String())
}
func TestParseStringFunctions(t *testing.T) { func TestParseStringFunctions(t *testing.T) {
tm, err := Parse(`{{join (split . ":") "/"}}`) tm, err := Parse(`{{join (split . ":") "/"}}`)
assert.NoError(t, err) assert.NoError(t, err)