add *codec.Frame and *proto.Frame support

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
2021-03-07 19:20:40 +03:00
parent 45b4311fa2
commit 6f26dd1caa
3 changed files with 19 additions and 373 deletions

View File

@@ -10,6 +10,7 @@ import (
oldjsonpb "github.com/golang/protobuf/jsonpb"
// nolint: staticcheck
oldproto "github.com/golang/protobuf/proto"
mproto "github.com/unistack-org/micro-proto/proto"
"github.com/unistack-org/micro/v3/codec"
jsonpb "google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
@@ -44,6 +45,8 @@ func (c *jsonpbCodec) Marshal(v interface{}) ([]byte, error) {
switch m := v.(type) {
case nil:
return nil, nil
case *mproto.Frame:
return m.Data, nil
case *codec.Frame:
return m.Data, nil
case proto.Message:
@@ -62,6 +65,9 @@ func (c *jsonpbCodec) Unmarshal(d []byte, v interface{}) error {
switch m := v.(type) {
case nil:
return nil
case *mproto.Frame:
m.Data = d
return nil
case *codec.Frame:
m.Data = d
return nil
@@ -80,6 +86,13 @@ func (c *jsonpbCodec) ReadBody(conn io.Reader, b interface{}) error {
switch m := b.(type) {
case nil:
return nil
case *mproto.Frame:
buf, err := ioutil.ReadAll(conn)
if err != nil {
return err
}
m.Data = buf
return nil
case *codec.Frame:
buf, err := ioutil.ReadAll(conn)
if err != nil {
@@ -103,6 +116,9 @@ func (c *jsonpbCodec) Write(conn io.Writer, m *codec.Message, b interface{}) err
switch m := b.(type) {
case nil:
return nil
case *mproto.Frame:
_, err := conn.Write(m.Data)
return err
case *codec.Frame:
_, err := conn.Write(m.Data)
return err