allow to flatten struct
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
ec7c757c2a
commit
858003c4b9
2
go.mod
2
go.mod
@ -3,6 +3,6 @@ module github.com/unistack-org/micro-codec-jsonpb/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
|
||||
)
|
||||
|
6
go.sum
6
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=
|
||||
|
45
jsonpb.go
45
jsonpb.go
@ -3,9 +3,9 @@ package jsonpb
|
||||
|
||||
import (
|
||||
"io"
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/unistack-org/micro/v3/codec"
|
||||
rutil "github.com/unistack-org/micro/v3/util/reflect"
|
||||
jsonpb "google.golang.org/protobuf/encoding/protojson"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
@ -26,6 +26,10 @@ var (
|
||||
|
||||
type jsonpbCodec struct{}
|
||||
|
||||
const (
|
||||
flattenTag = "flatten"
|
||||
)
|
||||
|
||||
func (c *jsonpbCodec) Marshal(v interface{}) ([]byte, error) {
|
||||
switch m := v.(type) {
|
||||
case nil:
|
||||
@ -33,6 +37,11 @@ func (c *jsonpbCodec) Marshal(v interface{}) ([]byte, error) {
|
||||
case *codec.Frame:
|
||||
return m.Data, nil
|
||||
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 nil, codec.ErrInvalidMessage
|
||||
@ -49,6 +58,11 @@ func (c *jsonpbCodec) Unmarshal(d []byte, v interface{}) error {
|
||||
m.Data = d
|
||||
return nil
|
||||
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 codec.ErrInvalidMessage
|
||||
@ -57,12 +71,12 @@ func (c *jsonpbCodec) ReadHeader(conn io.Reader, m *codec.Message, t codec.Messa
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *jsonpbCodec) ReadBody(conn io.Reader, b interface{}) error {
|
||||
switch m := b.(type) {
|
||||
func (c *jsonpbCodec) 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 {
|
||||
@ -71,28 +85,43 @@ func (c *jsonpbCodec) 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(m, codec.DefaultTagName, flattenTag); nerr == nil {
|
||||
if nm, ok := nv.(proto.Message); ok {
|
||||
return JsonpbUnmarshaler.Unmarshal(buf, nm)
|
||||
}
|
||||
}
|
||||
return JsonpbUnmarshaler.Unmarshal(buf, m)
|
||||
}
|
||||
return codec.ErrInvalidMessage
|
||||
}
|
||||
|
||||
func (c *jsonpbCodec) Write(conn io.Writer, m *codec.Message, b interface{}) error {
|
||||
switch m := b.(type) {
|
||||
func (c *jsonpbCodec) 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 := 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 {
|
||||
return err
|
||||
} else if len(buf) == 0 {
|
||||
return codec.ErrInvalidMessage
|
||||
}
|
||||
_, err = conn.Write(buf)
|
||||
return err
|
||||
|
Loading…
Reference in New Issue
Block a user