simplify
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
f9fca87e92
commit
b4721030eb
4
go.mod
4
go.mod
@ -2,10 +2,8 @@ module github.com/unistack-org/micro-codec-segmentio/v3
|
||||
|
||||
go 1.16
|
||||
|
||||
// github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c
|
||||
|
||||
require (
|
||||
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
|
||||
)
|
||||
|
6
go.sum
6
go.sum
@ -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/go.mod h1:7E68jTSWMnNoYhHi1JbLd7NBSB6XfE4vzqhR88hDBQc=
|
||||
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=
|
||||
|
57
json/json.go
57
json/json.go
@ -3,10 +3,10 @@ package json
|
||||
|
||||
import (
|
||||
"io"
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/segmentio/encoding/json"
|
||||
"github.com/unistack-org/micro/v3/codec"
|
||||
rutil "github.com/unistack-org/micro/v3/util/reflect"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -17,6 +17,10 @@ var (
|
||||
}
|
||||
)
|
||||
|
||||
const (
|
||||
flattenTag = "flatten"
|
||||
)
|
||||
|
||||
type Marshaler struct {
|
||||
EscapeHTML bool
|
||||
SortMapKeys bool
|
||||
@ -38,17 +42,21 @@ type jsonCodec struct {
|
||||
decodeFlags json.ParseFlags
|
||||
}
|
||||
|
||||
func (c *jsonCodec) Marshal(b interface{}) ([]byte, error) {
|
||||
switch m := b.(type) {
|
||||
func (c *jsonCodec) Marshal(v interface{}) ([]byte, error) {
|
||||
switch m := v.(type) {
|
||||
case nil:
|
||||
return nil, nil
|
||||
case *codec.Frame:
|
||||
return m.Data, nil
|
||||
}
|
||||
|
||||
if nv, nerr := rutil.StructFieldByTag(v, codec.DefaultTagName, flattenTag); nerr == nil {
|
||||
v = nv
|
||||
}
|
||||
|
||||
var err error
|
||||
var buf []byte
|
||||
buf, err = json.Append(buf, b, c.encodeFlags)
|
||||
buf, err = json.Append(buf, v, c.encodeFlags)
|
||||
return buf, err
|
||||
}
|
||||
|
||||
@ -64,6 +72,9 @@ func (c *jsonCodec) Unmarshal(b []byte, v interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
if nv, nerr := rutil.StructFieldByTag(v, codec.DefaultTagName, flattenTag); nerr == nil {
|
||||
v = nv
|
||||
}
|
||||
_, err := json.Parse(b, v, c.decodeFlags)
|
||||
return err
|
||||
}
|
||||
@ -72,38 +83,34 @@ func (c *jsonCodec) ReadHeader(conn io.Reader, m *codec.Message, t codec.Message
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *jsonCodec) ReadBody(conn io.Reader, b interface{}) error {
|
||||
switch m := b.(type) {
|
||||
case nil:
|
||||
func (c *jsonCodec) ReadBody(conn io.Reader, v interface{}) error {
|
||||
if v == 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 {
|
||||
return nil
|
||||
}
|
||||
m.Data = buf
|
||||
return nil
|
||||
}
|
||||
|
||||
err := json.NewDecoder(conn).Decode(b)
|
||||
if err == io.EOF {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
return c.Unmarshal(buf, v)
|
||||
}
|
||||
|
||||
func (c *jsonCodec) Write(conn io.Writer, m *codec.Message, b interface{}) error {
|
||||
switch m := b.(type) {
|
||||
case nil:
|
||||
func (c *jsonCodec) Write(conn io.Writer, m *codec.Message, v interface{}) error {
|
||||
if v == 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 {
|
||||
|
@ -3,15 +3,19 @@ package proto
|
||||
|
||||
import (
|
||||
"io"
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/segmentio/encoding/proto"
|
||||
"github.com/unistack-org/micro/v3/codec"
|
||||
rutil "github.com/unistack-org/micro/v3/util/reflect"
|
||||
newproto "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:
|
||||
@ -19,13 +23,19 @@ func (c *protoCodec) Marshal(v interface{}) ([]byte, error) {
|
||||
case *codec.Frame:
|
||||
return m.Data, nil
|
||||
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 nil, codec.ErrInvalidMessage
|
||||
}
|
||||
|
||||
func (c *protoCodec) Unmarshal(d []byte, v interface{}) error {
|
||||
var err error
|
||||
if d == nil {
|
||||
return nil
|
||||
}
|
||||
@ -34,59 +44,51 @@ func (c *protoCodec) Unmarshal(d []byte, v interface{}) error {
|
||||
return nil
|
||||
case *codec.Frame:
|
||||
m.Data = d
|
||||
return nil
|
||||
case newproto.Message, proto.Message:
|
||||
err = proto.Unmarshal(d, m)
|
||||
default:
|
||||
err = codec.ErrInvalidMessage
|
||||
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 err
|
||||
}
|
||||
return proto.Unmarshal(d, m)
|
||||
}
|
||||
return codec.ErrInvalidMessage
|
||||
}
|
||||
|
||||
func (c *protoCodec) ReadHeader(conn io.Reader, m *codec.Message, t codec.MessageType) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *protoCodec) ReadBody(conn io.Reader, b interface{}) error {
|
||||
switch m := b.(type) {
|
||||
case nil:
|
||||
func (c *protoCodec) ReadBody(conn io.Reader, v interface{}) error {
|
||||
if v == 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 {
|
||||
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
|
||||
return c.Unmarshal(buf, v)
|
||||
}
|
||||
|
||||
func (c *protoCodec) Write(conn io.Writer, m *codec.Message, b interface{}) error {
|
||||
switch m := b.(type) {
|
||||
case nil:
|
||||
func (c *protoCodec) Write(conn io.Writer, m *codec.Message, v interface{}) error {
|
||||
if v == nil {
|
||||
return nil
|
||||
case *codec.Frame:
|
||||
_, err := conn.Write(m.Data)
|
||||
return err
|
||||
case newproto.Message, proto.Message:
|
||||
buf, err := proto.Marshal(m)
|
||||
}
|
||||
|
||||
buf, err := c.Marshal(v)
|
||||
if err != nil {
|
||||
return err
|
||||
} else if len(buf) == 0 {
|
||||
return codec.ErrInvalidMessage
|
||||
}
|
||||
|
||||
_, err = conn.Write(buf)
|
||||
return err
|
||||
}
|
||||
return codec.ErrInvalidMessage
|
||||
}
|
||||
|
||||
func (c *protoCodec) String() string {
|
||||
|
Loading…
Reference in New Issue
Block a user