-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new MarshalText() API (#72)
adds `MarshalText(interface{}) (string, error)` and associated unit tests
- Loading branch information
Dylan Bourque
authored
Sep 16, 2022
1 parent
ff9e54b
commit 8d9f2fa
Showing
7 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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) | ||
} | ||
} |