1 // Copyright 2020 The Kubernetes Authors. 2 // SPDX-License-Identifier: Apache-2.0 3 4 package object 5 6 import ( 7 "fmt" 8 9 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" 10 "sigs.k8s.io/yaml" 11 ) 12 13 // YamlStringer delays YAML marshalling for logging until String() is called. 14 type YamlStringer struct { 15 O *unstructured.Unstructured 16 } 17 18 // String marshals the wrapped object to a YAML string. If serializing errors, 19 // the error string will be returned instead. This is primarily for use with 20 // verbose logging. 21 func (ys YamlStringer) String() string { 22 yamlBytes, err := yaml.Marshal(ys.O) 23 if err != nil { 24 return fmt.Sprintf("<<failed to serialize as yaml: %s>>", err) 25 } 26 return string(yamlBytes) 27 } 28