Skip to content

Commit 803dee9

Browse files
committed
merge MessageV1 and V2 case
1 parent a13b009 commit 803dee9

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

internal/pretty/pretty.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,37 +34,37 @@ const jsonIndent = " "
3434
//
3535
// If marshal fails, it falls back to fmt.Sprintf("%+v").
3636
func ToJSON(e any) string {
37+
var ret []byte
38+
var err error
39+
40+
mm := protojson.MarshalOptions{
41+
Indent: jsonIndent,
42+
Multiline: true,
43+
}
44+
45+
if ee, ok := e.(protoadapt.MessageV1); ok {
46+
e = protoadapt.MessageV2Of(ee)
47+
}
48+
3749
switch ee := e.(type) {
38-
case protoadapt.MessageV1:
39-
mm := protojson.MarshalOptions{Indent: jsonIndent}
40-
ret, err := mm.Marshal(protoadapt.MessageV2Of(ee))
41-
if err != nil {
42-
// This may fail for proto.Anys, e.g. for xDS v2, LDS, the v2
43-
// messages are not imported, and this will fail because the message
44-
// is not found.
45-
return fmt.Sprintf("%+v", ee)
46-
}
47-
return string(ret)
4850
case protoadapt.MessageV2:
49-
mm := protojson.MarshalOptions{
50-
Multiline: true,
51-
Indent: jsonIndent,
52-
}
53-
ret, err := mm.Marshal(ee)
51+
ret, err = mm.Marshal(ee)
52+
5453
if err != nil {
5554
// This may fail for proto.Anys, e.g. for xDS v2, LDS, the v2
5655
// messages are not imported, and this will fail because the message
5756
// is not found.
5857
return fmt.Sprintf("%+v", ee)
5958
}
60-
return string(ret)
6159
default:
62-
ret, err := json.MarshalIndent(ee, "", jsonIndent)
60+
ret, err = json.MarshalIndent(ee, "", jsonIndent)
61+
6362
if err != nil {
6463
return fmt.Sprintf("%+v", ee)
6564
}
66-
return string(ret)
6765
}
66+
67+
return string(ret)
6868
}
6969

7070
// FormatJSON formats the input json bytes with indentation.

0 commit comments

Comments
 (0)