summaryrefslogtreecommitdiff
path: root/plugins/grpc/codec/codec.go
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/grpc/codec/codec.go')
-rw-r--r--plugins/grpc/codec/codec.go44
1 files changed, 44 insertions, 0 deletions
diff --git a/plugins/grpc/codec/codec.go b/plugins/grpc/codec/codec.go
new file mode 100644
index 00000000..a9d89ac5
--- /dev/null
+++ b/plugins/grpc/codec/codec.go
@@ -0,0 +1,44 @@
+package codec
+
+import "google.golang.org/grpc/encoding"
+
+type RawMessage []byte
+
+// By default, gRPC registers and uses the "proto" codec, so it is not necessary to do this in your own code to send and receive proto messages.
+// https://github.com/grpc/grpc-go/blob/master/Documentation/encoding.md#using-a-codec
+const cName string = "proto"
+const rm string = "rawMessage"
+
+func (r RawMessage) Reset() {}
+func (RawMessage) ProtoMessage() {}
+func (RawMessage) String() string { return rm }
+
+type Codec struct{ base encoding.Codec }
+
+// Marshal returns the wire format of v. rawMessages would be returned without encoding.
+func (c *Codec) Marshal(v interface{}) ([]byte, error) {
+ if raw, ok := v.(RawMessage); ok {
+ return raw, nil
+ }
+
+ return c.base.Marshal(v)
+}
+
+// Unmarshal parses the wire format into v. rawMessages would not be unmarshalled.
+func (c *Codec) Unmarshal(data []byte, v interface{}) error {
+ if raw, ok := v.(*RawMessage); ok {
+ *raw = data
+ return nil
+ }
+
+ return c.base.Unmarshal(data, v)
+}
+
+func (c *Codec) Name() string {
+ return cName
+}
+
+// String return codec name.
+func (c *Codec) String() string {
+ return "raw:" + c.base.Name()
+}