-
Notifications
You must be signed in to change notification settings - Fork 13
/
marshal_text.go
33 lines (29 loc) · 917 Bytes
/
marshal_text.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package csproto
import (
"encoding"
"fmt"
gogo "github.com/gogo/protobuf/proto"
googlev1 "github.com/golang/protobuf/proto" //nolint: staticcheck // we're using this deprecated package intentionally"
"google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/proto"
)
// MarshalText converts the specified message to prototext string format
func MarshalText(msg interface{}) (string, error) {
if tm, ok := msg.(encoding.TextMarshaler); ok {
res, err := tm.MarshalText()
if err != nil {
return "", err
}
return string(res), nil
}
switch MsgType(msg) {
case MessageTypeGoogle:
return prototext.Format(msg.(proto.Message)), nil
case MessageTypeGoogleV1:
return googlev1.MarshalTextString(msg.(googlev1.Message)), nil
case MessageTypeGogo:
return gogo.MarshalTextString(msg.(gogo.Message)), nil
default:
return "", fmt.Errorf("unsupported message type: %T", msg)
}
}