@ -51,10 +51,12 @@ func funcMap() template.FuncMap {
"toToml" : toTOML ,
"toToml" : toTOML ,
"fromToml" : fromTOML ,
"fromToml" : fromTOML ,
"toYaml" : toYAML ,
"toYaml" : toYAML ,
"mustToYaml" : mustToYAML ,
"toYamlPretty" : toYAMLPretty ,
"toYamlPretty" : toYAMLPretty ,
"fromYaml" : fromYAML ,
"fromYaml" : fromYAML ,
"fromYamlArray" : fromYAMLArray ,
"fromYamlArray" : fromYAMLArray ,
"toJson" : toJSON ,
"toJson" : toJSON ,
"mustToJson" : mustToJSON ,
"fromJson" : fromJSON ,
"fromJson" : fromJSON ,
"fromJsonArray" : fromJSONArray ,
"fromJsonArray" : fromJSONArray ,
@ -91,6 +93,19 @@ func toYAML(v interface{}) string {
return strings . TrimSuffix ( string ( data ) , "\n" )
return strings . TrimSuffix ( string ( data ) , "\n" )
}
}
// mustToYAML takes an interface, marshals it to yaml, and returns a string.
// It will panic if there is an error.
//
// This is designed to be called from a template when need to ensure that the
// output YAML is valid.
func mustToYAML ( v interface { } ) string {
data , err := yaml . Marshal ( v )
if err != nil {
panic ( err )
}
return strings . TrimSuffix ( string ( data ) , "\n" )
}
func toYAMLPretty ( v interface { } ) string {
func toYAMLPretty ( v interface { } ) string {
var data bytes . Buffer
var data bytes . Buffer
encoder := goYaml . NewEncoder ( & data )
encoder := goYaml . NewEncoder ( & data )
@ -176,6 +191,19 @@ func toJSON(v interface{}) string {
return string ( data )
return string ( data )
}
}
// mustToJSON takes an interface, marshals it to json, and returns a string.
// It will panic if there is an error.
//
// This is designed to be called from a template when need to ensure that the
// output JSON is valid.
func mustToJSON ( v interface { } ) string {
data , err := json . Marshal ( v )
if err != nil {
panic ( err )
}
return string ( data )
}
// 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