From c84813aa204da78d61aae0cd26780955e81b9816 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Tue, 25 May 2021 23:18:06 +0300 Subject: [PATCH] allow to flatten struct Signed-off-by: Vasiliy Tolstov --- go.mod | 2 +- go.sum | 6 +++--- proto.go | 47 +++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 43 insertions(+), 12 deletions(-) diff --git a/go.mod b/go.mod index bc1f84d..e7cacce 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,6 @@ module github.com/unistack-org/micro-codec-proto/v3 go 1.16 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 ) diff --git a/go.sum b/go.sum index cb3cb20..aba4cb4 100644 --- a/go.sum +++ b/go.sum @@ -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/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/unistack-org/micro/v3 v3.3.17 h1:WcyS7InP0DlS/JpRQGLh5sG6VstkdHJbgpMp+gmHmwg= -github.com/unistack-org/micro/v3 v3.3.17/go.mod h1:022EOEZZ789hZY3yB5ZSMXU6jLiadBgcNB/cpediV3c= -golang.org/x/net v0.0.0-20210423184538-5f58ad60dda6/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk= +github.com/unistack-org/micro/v3 v3.3.19 h1:x0bl4H+IZvdHHKfA8qMcox9W92+e7rtbqgRfQPoP79w= +github.com/unistack-org/micro/v3 v3.3.19/go.mod h1:LXmPfbJnJNvL0kQs8HfnkV3Wya2Wb+C7keVq++RCZnk= +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-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= diff --git a/proto.go b/proto.go index 86b0031..88f6e0b 100644 --- a/proto.go +++ b/proto.go @@ -3,14 +3,18 @@ package proto import ( "io" - "io/ioutil" "github.com/unistack-org/micro/v3/codec" + rutil "github.com/unistack-org/micro/v3/util/reflect" "google.golang.org/protobuf/proto" ) type protoCodec struct{} +const ( + flattenTag = "flatten" +) + func (c *protoCodec) Marshal(v interface{}) ([]byte, error) { switch m := v.(type) { case nil: @@ -18,6 +22,11 @@ func (c *protoCodec) Marshal(v interface{}) ([]byte, error) { case *codec.Frame: return m.Data, nil 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 nil, codec.ErrInvalidMessage @@ -33,6 +42,11 @@ func (c *protoCodec) Unmarshal(d []byte, v interface{}) error { case *codec.Frame: m.Data = d 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 codec.ErrInvalidMessage @@ -42,12 +56,12 @@ func (c *protoCodec) ReadHeader(conn io.Reader, m *codec.Message, t codec.Messag return nil } -func (c *protoCodec) ReadBody(conn io.Reader, b interface{}) error { - switch m := b.(type) { +func (c *protoCodec) ReadBody(conn io.Reader, v interface{}) error { + switch m := v.(type) { case nil: return nil case *codec.Frame: - buf, err := ioutil.ReadAll(conn) + buf, err := io.ReadAll(conn) if err != nil { return err } else if len(buf) == 0 { @@ -56,28 +70,45 @@ func (c *protoCodec) ReadBody(conn io.Reader, b interface{}) error { m.Data = buf return nil case proto.Message: - buf, err := ioutil.ReadAll(conn) + buf, err := io.ReadAll(conn) if err != nil { return err } else if len(buf) == 0 { 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 codec.ErrInvalidMessage } -func (c *protoCodec) Write(conn io.Writer, m *codec.Message, b interface{}) error { - switch m := b.(type) { +func (c *protoCodec) Write(conn io.Writer, m *codec.Message, v interface{}) error { + switch m := v.(type) { case nil: return nil case *codec.Frame: _, err := conn.Write(m.Data) return err 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 { return err + } else if len(buf) == 0 { + return codec.ErrInvalidMessage } _, err = conn.Write(buf) return err