@ -385,7 +385,14 @@ func OpensMenuStyle(str string) string {
return ColoredString ( fmt . Sprintf ( "%s..." , str ) , color . FgMagenta )
}
func MarshalIntoFormat ( data interface { } , format string ) ( [ ] byte , error ) {
// MarshalIntoYaml gets any json-tagged data and marshal it into yaml saving original json structure.
// Useful for structs from 3rd-party libs without yaml tags.
func MarshalIntoYaml ( data interface { } ) ( [ ] byte , error ) {
return marshalIntoFormat ( data , "yaml" )
}
func marshalIntoFormat ( data interface { } , format string ) ( [ ] byte , error ) {
// First marshal struct->json to get the resulting structure declared by json tags
dataJSON , err := json . MarshalIndent ( data , "" , " " )
if err != nil {
return nil , err
@ -394,7 +401,7 @@ func MarshalIntoFormat(data interface{}, format string) ([]byte, error) {
case "json" :
return dataJSON , err
case "yaml" :
// Use Unmarshal->Marshal hack to convert vendor-locked objects to YAML with same structure as JSON
// Use Unmarshal->Marshal hack to convert json into yaml with the original structure preserved
var dataMirror yaml . MapSlice
if err := yaml . Unmarshal ( dataJSON , & dataMirror ) ; err != nil {
return nil , err