allow to flatten struct

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

2
go.mod
View File

@ -3,6 +3,6 @@ module github.com/unistack-org/micro-codec-jsonpb/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,9 +3,9 @@ package jsonpb
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"
jsonpb "google.golang.org/protobuf/encoding/protojson" jsonpb "google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
) )
@ -26,6 +26,10 @@ var (
type jsonpbCodec struct{} type jsonpbCodec struct{}
const (
flattenTag = "flatten"
)
func (c *jsonpbCodec) Marshal(v interface{}) ([]byte, error) { func (c *jsonpbCodec) Marshal(v interface{}) ([]byte, error) {
switch m := v.(type) { switch m := v.(type) {
case nil: case nil:
@ -33,6 +37,11 @@ func (c *jsonpbCodec) 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(m, codec.DefaultTagName, flattenTag); nerr == nil {
if nm, ok := nv.(proto.Message); ok {
return JsonpbMarshaler.Marshal(nm)
}
}
return JsonpbMarshaler.Marshal(m) return JsonpbMarshaler.Marshal(m)
} }
return nil, codec.ErrInvalidMessage return nil, codec.ErrInvalidMessage
@ -49,6 +58,11 @@ func (c *jsonpbCodec) Unmarshal(d []byte, v interface{}) error {
m.Data = d m.Data = d
return nil return nil
case proto.Message: case proto.Message:
if nv, nerr := rutil.StructFieldByTag(m, codec.DefaultTagName, flattenTag); nerr == nil {
if nm, ok := nv.(proto.Message); ok {
return JsonpbUnmarshaler.Unmarshal(d, nm)
}
}
return JsonpbUnmarshaler.Unmarshal(d, m) return JsonpbUnmarshaler.Unmarshal(d, m)
} }
return codec.ErrInvalidMessage return codec.ErrInvalidMessage
@ -57,12 +71,12 @@ func (c *jsonpbCodec) ReadHeader(conn io.Reader, m *codec.Message, t codec.Messa
return nil return nil
} }
func (c *jsonpbCodec) ReadBody(conn io.Reader, b interface{}) error { func (c *jsonpbCodec) 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 {
@ -71,28 +85,43 @@ func (c *jsonpbCodec) 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(m, codec.DefaultTagName, flattenTag); nerr == nil {
if nm, ok := nv.(proto.Message); ok {
return JsonpbUnmarshaler.Unmarshal(buf, nm)
}
}
return JsonpbUnmarshaler.Unmarshal(buf, m) return JsonpbUnmarshaler.Unmarshal(buf, m)
} }
return codec.ErrInvalidMessage return codec.ErrInvalidMessage
} }
func (c *jsonpbCodec) Write(conn io.Writer, m *codec.Message, b interface{}) error { func (c *jsonpbCodec) 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 := JsonpbMarshaler.Marshal(m) var buf []byte
var err error
if nv, nerr := rutil.StructFieldByTag(m, codec.DefaultTagName, flattenTag); nerr == nil {
if nm, ok := nv.(proto.Message); ok {
buf, err = JsonpbMarshaler.Marshal(nm)
}
} else {
buf, err = JsonpbMarshaler.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