allow to flatten struct
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
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
|
||||
|
Reference in New Issue
Block a user