* codec: Rename codec and marshaler interfaces, ref: 8413 * codec: remove BinaryBare * changelog update * Update comments and documentation * adding doc string comments * Update CHANGELOG.md Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * Update codec/codec.go Co-authored-by: Marko <marbar3778@yahoo.com> Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> Co-authored-by: Amaury <1293565+amaurym@users.noreply.github.com> Co-authored-by: Marko <marbar3778@yahoo.com>
30 lines
817 B
Go
30 lines
817 B
Go
package codec
|
|
|
|
import (
|
|
"encoding/json"
|
|
|
|
"github.com/gogo/protobuf/proto"
|
|
"gopkg.in/yaml.v2"
|
|
)
|
|
|
|
// MarshalYAML marshals toPrint using jsonMarshaler to leverage specialized MarshalJSON methods
|
|
// (usually related to serialize data with protobuf or amin depending on a configuration).
|
|
// This involves additional roundtrip through JSON.
|
|
func MarshalYAML(jsonMarshaler JSONCodec, toPrint proto.Message) ([]byte, error) {
|
|
// We are OK with the performance hit of the additional JSON roundtip. MarshalYAML is not
|
|
// used in any critical parts of the system.
|
|
bz, err := jsonMarshaler.MarshalJSON(toPrint)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// generate YAML by decoding JSON and re-encoding to YAML
|
|
var j interface{}
|
|
err = json.Unmarshal(bz, &j)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return yaml.Marshal(j)
|
|
}
|