Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-05-26 00:58:47 +03:00
parent f9fca87e92
commit b4721030eb
4 changed files with 80 additions and 73 deletions

4
go.mod
View File

@ -2,10 +2,8 @@ module github.com/unistack-org/micro-codec-segmentio/v3
go 1.16 go 1.16
// github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c
require ( require (
github.com/segmentio/encoding v0.2.17 github.com/segmentio/encoding v0.2.17
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

@ -11,9 +11,9 @@ github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTK
github.com/segmentio/encoding v0.2.17 h1:cgfmPc44u1po1lz5bSgF00gLCROBjDNc7h+H7I20zpc= github.com/segmentio/encoding v0.2.17 h1:cgfmPc44u1po1lz5bSgF00gLCROBjDNc7h+H7I20zpc=
github.com/segmentio/encoding v0.2.17/go.mod h1:7E68jTSWMnNoYhHi1JbLd7NBSB6XfE4vzqhR88hDBQc= github.com/segmentio/encoding v0.2.17/go.mod h1:7E68jTSWMnNoYhHi1JbLd7NBSB6XfE4vzqhR88hDBQc=
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,10 +3,10 @@ package json
import ( import (
"io" "io"
"io/ioutil"
"github.com/segmentio/encoding/json" "github.com/segmentio/encoding/json"
"github.com/unistack-org/micro/v3/codec" "github.com/unistack-org/micro/v3/codec"
rutil "github.com/unistack-org/micro/v3/util/reflect"
) )
var ( var (
@ -17,6 +17,10 @@ var (
} }
) )
const (
flattenTag = "flatten"
)
type Marshaler struct { type Marshaler struct {
EscapeHTML bool EscapeHTML bool
SortMapKeys bool SortMapKeys bool
@ -38,17 +42,21 @@ type jsonCodec struct {
decodeFlags json.ParseFlags decodeFlags json.ParseFlags
} }
func (c *jsonCodec) Marshal(b interface{}) ([]byte, error) { func (c *jsonCodec) Marshal(v interface{}) ([]byte, error) {
switch m := b.(type) { switch m := v.(type) {
case nil: case nil:
return nil, nil return nil, nil
case *codec.Frame: case *codec.Frame:
return m.Data, nil return m.Data, nil
} }
if nv, nerr := rutil.StructFieldByTag(v, codec.DefaultTagName, flattenTag); nerr == nil {
v = nv
}
var err error var err error
var buf []byte var buf []byte
buf, err = json.Append(buf, b, c.encodeFlags) buf, err = json.Append(buf, v, c.encodeFlags)
return buf, err return buf, err
} }
@ -64,6 +72,9 @@ func (c *jsonCodec) Unmarshal(b []byte, v interface{}) error {
return nil return nil
} }
if nv, nerr := rutil.StructFieldByTag(v, codec.DefaultTagName, flattenTag); nerr == nil {
v = nv
}
_, err := json.Parse(b, v, c.decodeFlags) _, err := json.Parse(b, v, c.decodeFlags)
return err return err
} }
@ -72,38 +83,34 @@ func (c *jsonCodec) ReadHeader(conn io.Reader, m *codec.Message, t codec.Message
return nil return nil
} }
func (c *jsonCodec) ReadBody(conn io.Reader, b interface{}) error { func (c *jsonCodec) ReadBody(conn io.Reader, v interface{}) error {
switch m := b.(type) { if v == nil {
case nil:
return nil
case *codec.Frame:
buf, err := ioutil.ReadAll(conn)
if err != nil {
return err
} else if len(buf) == 0 {
return nil
}
m.Data = buf
return nil return nil
} }
err := json.NewDecoder(conn).Decode(b) buf, err := io.ReadAll(conn)
if err == io.EOF { if err != nil {
return err
} else if len(buf) == 0 {
return nil return nil
} }
return err return c.Unmarshal(buf, v)
} }
func (c *jsonCodec) Write(conn io.Writer, m *codec.Message, b interface{}) error { func (c *jsonCodec) Write(conn io.Writer, m *codec.Message, v interface{}) error {
switch m := b.(type) { if v == nil {
case nil:
return nil return nil
case *codec.Frame:
_, err := conn.Write(m.Data)
return err
} }
return json.NewEncoder(conn).Encode(b) buf, err := c.Marshal(v)
if err != nil {
return err
} else if len(buf) == 0 {
return codec.ErrInvalidMessage
}
_, err = conn.Write(buf)
return err
} }
func (c *jsonCodec) String() string { func (c *jsonCodec) String() string {

View File

@ -3,15 +3,19 @@ package proto
import ( import (
"io" "io"
"io/ioutil"
"github.com/segmentio/encoding/proto" "github.com/segmentio/encoding/proto"
"github.com/unistack-org/micro/v3/codec" "github.com/unistack-org/micro/v3/codec"
rutil "github.com/unistack-org/micro/v3/util/reflect"
newproto "google.golang.org/protobuf/proto" newproto "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:
@ -19,13 +23,19 @@ func (c *protoCodec) Marshal(v interface{}) ([]byte, error) {
case *codec.Frame: case *codec.Frame:
return m.Data, nil return m.Data, nil
case newproto.Message, proto.Message: case newproto.Message, proto.Message:
if nv, nerr := rutil.StructFieldByTag(v, codec.DefaultTagName, flattenTag); nerr == nil {
if nm, ok := nv.(newproto.Message); ok {
m = nm
} else if nm, ok := nv.(proto.Message); ok {
m = nm
}
}
return proto.Marshal(m) return proto.Marshal(m)
} }
return nil, codec.ErrInvalidMessage return nil, codec.ErrInvalidMessage
} }
func (c *protoCodec) Unmarshal(d []byte, v interface{}) error { func (c *protoCodec) Unmarshal(d []byte, v interface{}) error {
var err error
if d == nil { if d == nil {
return nil return nil
} }
@ -34,59 +44,51 @@ func (c *protoCodec) Unmarshal(d []byte, v interface{}) error {
return nil return nil
case *codec.Frame: case *codec.Frame:
m.Data = d m.Data = d
return nil
case newproto.Message, proto.Message: case newproto.Message, proto.Message:
err = proto.Unmarshal(d, m) if nv, nerr := rutil.StructFieldByTag(v, codec.DefaultTagName, flattenTag); nerr == nil {
default: if nm, ok := nv.(newproto.Message); ok {
err = codec.ErrInvalidMessage m = nm
} else if nm, ok := nv.(proto.Message); ok {
m = nm
}
}
return proto.Unmarshal(d, m)
} }
return err return codec.ErrInvalidMessage
} }
func (c *protoCodec) ReadHeader(conn io.Reader, m *codec.Message, t codec.MessageType) error { func (c *protoCodec) ReadHeader(conn io.Reader, m *codec.Message, t codec.MessageType) error {
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) { if v == nil {
case nil:
return nil return nil
case *codec.Frame:
buf, err := ioutil.ReadAll(conn)
if err != nil {
return err
} else if len(buf) == 0 {
return nil
}
m.Data = buf
return nil
case newproto.Message, proto.Message:
buf, err := ioutil.ReadAll(conn)
if err != nil {
return err
} else if len(buf) == 0 {
return nil
}
return proto.Unmarshal(buf, m)
} }
return codec.ErrInvalidMessage buf, err := io.ReadAll(conn)
if err != nil {
return err
} else if len(buf) == 0 {
return nil
}
return c.Unmarshal(buf, v)
} }
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) { if v == nil {
case nil:
return nil return nil
case *codec.Frame:
_, err := conn.Write(m.Data)
return err
case newproto.Message, proto.Message:
buf, err := proto.Marshal(m)
if err != nil {
return err
}
_, err = conn.Write(buf)
return err
} }
return codec.ErrInvalidMessage
buf, err := c.Marshal(v)
if err != nil {
return err
} else if len(buf) == 0 {
return codec.ErrInvalidMessage
}
_, err = conn.Write(buf)
return err
} }
func (c *protoCodec) String() string { func (c *protoCodec) String() string {