package internal import ( "bytes" "encoding/json" "fmt" "google.golang.org/protobuf/encoding/protojson" "google.golang.org/protobuf/proto" yaml "gopkg.in/yaml.v3" ) // ConvertProtoToYAML converts any protobuf message to a YAML string with 2-space indentation func ConvertProtoToYAML(msg proto.Message) (string, error) { // 1. Marshal protobuf to JSON jsonBytes, err := protojson.Marshal(msg) if err != nil { return "", fmt.Errorf("failed to marshal proto to JSON: %w", err) } // 2. Unmarshal JSON into a generic Go map var obj interface{} if err := json.Unmarshal(jsonBytes, &obj); err != nil { return "", fmt.Errorf("failed to unmarshal JSON: %w", err) } // 3. Marshal the map to YAML with 2-space indentation var buf bytes.Buffer encoder := yaml.NewEncoder(&buf) encoder.SetIndent(2) // set indentation to 2 spaces if err := encoder.Encode(obj); err != nil { return "", fmt.Errorf("failed to encode YAML: %w", err) } encoder.Close() return buf.String(), nil }