@ -59,6 +59,8 @@ func funcMap() template.FuncMap {
"fromYamlArray" : fromYAMLArray ,
"fromYamlArray" : fromYAMLArray ,
"toJson" : toJSON ,
"toJson" : toJSON ,
"mustToJson" : mustToJSON ,
"mustToJson" : mustToJSON ,
"toPrettyJson" : toPrettyJSON ,
"mustToPrettyJson" : mustToPrettyJSON ,
"fromJson" : fromJSON ,
"fromJson" : fromJSON ,
"fromJsonArray" : fromJSONArray ,
"fromJsonArray" : fromJSONArray ,
@ -220,6 +222,48 @@ func mustToJSON(v any) string {
return string ( data )
return string ( data )
}
}
// encodePrettyJSON encodes v as indented JSON without HTML-escaping special
// characters (&, <, >). It uses two-space indentation to match the behavior
// of Sprig's toPrettyJson.
func encodePrettyJSON ( v any ) ( string , error ) {
var buf bytes . Buffer
enc := json . NewEncoder ( & buf )
enc . SetEscapeHTML ( false )
enc . SetIndent ( "" , " " )
if err := enc . Encode ( v ) ; err != nil {
return "" , err
}
return strings . TrimSuffix ( buf . String ( ) , "\n" ) , nil
}
// toPrettyJSON takes an interface, marshals it to indented JSON without
// HTML-escaping special characters (&, <, >), and returns a string. It will
// always return a string, even on marshal error (empty string).
//
// This is designed to be called from a template.
func toPrettyJSON ( v any ) string {
s , err := encodePrettyJSON ( v )
if err != nil {
// Swallow errors inside of a template.
return ""
}
return s
}
// mustToPrettyJSON takes an interface, marshals it to indented JSON without
// HTML-escaping special characters (&, <, >), and returns a string.
// It will panic if there is an error.
//
// This is designed to be called from a template when you need to ensure that
// the output JSON is valid.
func mustToPrettyJSON ( v any ) string {
s , err := encodePrettyJSON ( v )
if err != nil {
panic ( err )
}
return s
}
// fromJSON converts a JSON document into a map[string]interface{}.
// fromJSON converts a JSON document into a map[string]interface{}.
//
//
// This is not a general-purpose JSON parser, and will not parse all valid
// This is not a general-purpose JSON parser, and will not parse all valid