docs: Update the code.MarshalYAML docs (#7496)

This commit is contained in:
Robert Zaremba 2020-10-09 16:08:02 +02:00 committed by GitHub
parent c5320bcda0
commit 257eff3f6c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 7 deletions

View File

@ -7,20 +7,19 @@ import (
"gopkg.in/yaml.v2"
)
// MarshalYAML marshals the provided toPrint content with the provided JSON marshaler
// by encoding JSON, decoding JSON, and then encoding YAML.
// 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 JSONMarshaler, toPrint proto.Message) ([]byte, error) {
// only the JSONMarshaler has full context as to how the JSON
// mashalling should look (which may be different for amino & proto codecs)
// so we need to use it to marshal toPrint first
// 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 and re-encoding JSON as YAML
// generate YAML by decoding JSON and re-encoding to YAML
var j interface{}
err = json.Unmarshal(bz, &j)
if err != nil {
return nil, err