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:
parent
7ca86796c9
commit
5766317e33
2 changed files with 18 additions and 2 deletions
|
@ -1,6 +1,7 @@
|
|||
package templates
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
@ -10,8 +11,12 @@ import (
|
|||
// functions provided to every template.
|
||||
var basicFunctions = template.FuncMap{
|
||||
"json": func(v interface{}) string {
|
||||
a, _ := json.Marshal(v)
|
||||
return string(a)
|
||||
buf := &bytes.Buffer{}
|
||||
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,
|
||||
"join": strings.Join,
|
||||
|
|
|
@ -7,6 +7,17 @@ import (
|
|||
"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) {
|
||||
tm, err := Parse(`{{join (split . ":") "/"}}`)
|
||||
assert.NoError(t, err)
|
||||
|
|
Loading…
Reference in a new issue