diff options
Diffstat (limited to 'plugins/grpc/codec')
-rw-r--r-- | plugins/grpc/codec/codec.go | 44 | ||||
-rw-r--r-- | plugins/grpc/codec/codec_test.go | 79 |
2 files changed, 0 insertions, 123 deletions
diff --git a/plugins/grpc/codec/codec.go b/plugins/grpc/codec/codec.go deleted file mode 100644 index a9d89ac5..00000000 --- a/plugins/grpc/codec/codec.go +++ /dev/null @@ -1,44 +0,0 @@ -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() -} diff --git a/plugins/grpc/codec/codec_test.go b/plugins/grpc/codec/codec_test.go deleted file mode 100644 index 60efb072..00000000 --- a/plugins/grpc/codec/codec_test.go +++ /dev/null @@ -1,79 +0,0 @@ -package codec - -import ( - "testing" - - json "github.com/json-iterator/go" - "github.com/stretchr/testify/assert" -) - -type jsonCodec struct{} - -func (jsonCodec) Marshal(v interface{}) ([]byte, error) { - return json.Marshal(v) -} - -func (jsonCodec) Unmarshal(data []byte, v interface{}) error { - return json.Unmarshal(data, v) -} - -func (jsonCodec) Name() string { - return "json" -} - -func TestCodec_String(t *testing.T) { - c := Codec{jsonCodec{}} - - assert.Equal(t, "raw:json", c.String()) - - r := RawMessage{} - r.Reset() - r.ProtoMessage() - assert.Equal(t, "rawMessage", r.String()) -} - -func TestCodec_Unmarshal_ByPass(t *testing.T) { - c := Codec{jsonCodec{}} - - s := struct { - Name string - }{} - - assert.NoError(t, c.Unmarshal([]byte(`{"name":"name"}`), &s)) - assert.Equal(t, "name", s.Name) -} - -func TestCodec_Marshal_ByPass(t *testing.T) { - c := Codec{jsonCodec{}} - - s := struct { - Name string - }{ - Name: "name", - } - - d, err := c.Marshal(s) - assert.NoError(t, err) - - assert.Equal(t, `{"Name":"name"}`, string(d)) -} - -func TestCodec_Unmarshal_Raw(t *testing.T) { - c := Codec{jsonCodec{}} - - s := RawMessage{} - - assert.NoError(t, c.Unmarshal([]byte(`{"name":"name"}`), &s)) - assert.Equal(t, `{"name":"name"}`, string(s)) -} - -func TestCodec_Marshal_Raw(t *testing.T) { - c := Codec{jsonCodec{}} - - s := RawMessage(`{"Name":"name"}`) - - d, err := c.Marshal(s) - assert.NoError(t, err) - - assert.Equal(t, `{"Name":"name"}`, string(d)) -} |