summaryrefslogtreecommitdiff
path: root/plugins/grpc/codec/codec.go
blob: a9d89ac5f7504fd8c587c296f1aed53502416479 (plain)
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
34
35
36
37
38
39
40
41
42
43
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()
}