allow to flatten struct

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-05-25 23:18:06 +03:00
parent ea5328b2a9
commit c84813aa20
3 changed files with 43 additions and 12 deletions

2
go.mod
View File

@ -3,6 +3,6 @@ module github.com/unistack-org/micro-codec-proto/v3
go 1.16 go 1.16
require ( require (
github.com/unistack-org/micro/v3 v3.3.17 github.com/unistack-org/micro/v3 v3.3.19
google.golang.org/protobuf v1.26.0 google.golang.org/protobuf v1.26.0
) )

6
go.sum
View File

@ -7,9 +7,9 @@ github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/silas/dag v0.0.0-20210121180416-41cf55125c34/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I= github.com/silas/dag v0.0.0-20210121180416-41cf55125c34/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I=
github.com/unistack-org/micro/v3 v3.3.17 h1:WcyS7InP0DlS/JpRQGLh5sG6VstkdHJbgpMp+gmHmwg= github.com/unistack-org/micro/v3 v3.3.19 h1:x0bl4H+IZvdHHKfA8qMcox9W92+e7rtbqgRfQPoP79w=
github.com/unistack-org/micro/v3 v3.3.17/go.mod h1:022EOEZZ789hZY3yB5ZSMXU6jLiadBgcNB/cpediV3c= github.com/unistack-org/micro/v3 v3.3.19/go.mod h1:LXmPfbJnJNvL0kQs8HfnkV3Wya2Wb+C7keVq++RCZnk=
golang.org/x/net v0.0.0-20210423184538-5f58ad60dda6/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk= golang.org/x/net v0.0.0-20210510120150-4163338589ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=

View File

@ -3,14 +3,18 @@ package proto
import ( import (
"io" "io"
"io/ioutil"
"github.com/unistack-org/micro/v3/codec" "github.com/unistack-org/micro/v3/codec"
rutil "github.com/unistack-org/micro/v3/util/reflect"
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
) )
type protoCodec struct{} type protoCodec struct{}
const (
flattenTag = "flatten"
)
func (c *protoCodec) Marshal(v interface{}) ([]byte, error) { func (c *protoCodec) Marshal(v interface{}) ([]byte, error) {
switch m := v.(type) { switch m := v.(type) {
case nil: case nil:
@ -18,6 +22,11 @@ func (c *protoCodec) Marshal(v interface{}) ([]byte, error) {
case *codec.Frame: case *codec.Frame:
return m.Data, nil return m.Data, nil
case proto.Message: case proto.Message:
if nv, nerr := rutil.StructFieldByTag(v, codec.DefaultTagName, flattenTag); nerr == nil {
if nm, ok := nv.(proto.Message); ok {
return proto.Marshal(nm)
}
}
return proto.Marshal(m) return proto.Marshal(m)
} }
return nil, codec.ErrInvalidMessage return nil, codec.ErrInvalidMessage
@ -33,6 +42,11 @@ func (c *protoCodec) Unmarshal(d []byte, v interface{}) error {
case *codec.Frame: case *codec.Frame:
m.Data = d m.Data = d
case proto.Message: case proto.Message:
if nv, nerr := rutil.StructFieldByTag(v, codec.DefaultTagName, flattenTag); nerr == nil {
if nm, ok := nv.(proto.Message); ok {
return proto.Unmarshal(d, nm)
}
}
return proto.Unmarshal(d, m) return proto.Unmarshal(d, m)
} }
return codec.ErrInvalidMessage return codec.ErrInvalidMessage
@ -42,12 +56,12 @@ func (c *protoCodec) ReadHeader(conn io.Reader, m *codec.Message, t codec.Messag
return nil return nil
} }
func (c *protoCodec) ReadBody(conn io.Reader, b interface{}) error { func (c *protoCodec) ReadBody(conn io.Reader, v interface{}) error {
switch m := b.(type) { switch m := v.(type) {
case nil: case nil:
return nil return nil
case *codec.Frame: case *codec.Frame:
buf, err := ioutil.ReadAll(conn) buf, err := io.ReadAll(conn)
if err != nil { if err != nil {
return err return err
} else if len(buf) == 0 { } else if len(buf) == 0 {
@ -56,28 +70,45 @@ func (c *protoCodec) ReadBody(conn io.Reader, b interface{}) error {
m.Data = buf m.Data = buf
return nil return nil
case proto.Message: case proto.Message:
buf, err := ioutil.ReadAll(conn) buf, err := io.ReadAll(conn)
if err != nil { if err != nil {
return err return err
} else if len(buf) == 0 { } else if len(buf) == 0 {
return nil return nil
} }
if nv, nerr := rutil.StructFieldByTag(v, codec.DefaultTagName, flattenTag); nerr == nil {
if nm, ok := nv.(proto.Message); ok {
return proto.Unmarshal(buf, nm)
}
}
return proto.Unmarshal(buf, m) return proto.Unmarshal(buf, m)
} }
return codec.ErrInvalidMessage return codec.ErrInvalidMessage
} }
func (c *protoCodec) Write(conn io.Writer, m *codec.Message, b interface{}) error { func (c *protoCodec) Write(conn io.Writer, m *codec.Message, v interface{}) error {
switch m := b.(type) { switch m := v.(type) {
case nil: case nil:
return nil return nil
case *codec.Frame: case *codec.Frame:
_, err := conn.Write(m.Data) _, err := conn.Write(m.Data)
return err return err
case proto.Message: case proto.Message:
buf, err := proto.Marshal(m) var buf []byte
var err error
if nv, nerr := rutil.StructFieldByTag(v, codec.DefaultTagName, flattenTag); nerr == nil {
if nm, ok := nv.(proto.Message); ok {
buf, err = proto.Marshal(nm)
}
} else {
buf, err = proto.Marshal(m)
}
if err != nil { if err != nil {
return err return err
} else if len(buf) == 0 {
return codec.ErrInvalidMessage
} }
_, err = conn.Write(buf) _, err = conn.Write(buf)
return err return err