update to latest micro, optimize flatten

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
2024-09-17 12:28:35 +03:00
parent 8a90c1c723
commit 47feb209a4
6 changed files with 104 additions and 107 deletions

View File

@@ -2,8 +2,6 @@
package json // import "go.unistack.org/micro-codec-segmentio/v3/json"
import (
"io"
"github.com/segmentio/encoding/json"
pb "go.unistack.org/micro-proto/v3/codec"
"go.unistack.org/micro/v3/codec"
@@ -21,10 +19,9 @@ var (
}
)
var _ codec.Codec = &jsonCodec{}
const (
flattenTag = "flatten"
var (
_ codec.Codec = (*jsonCodec)()
_ codec.CodecV2 = (*jsonCodecV2)()
)
type JsonMarshalOptions struct {
@@ -49,6 +46,12 @@ type jsonCodec struct {
decodeFlags json.ParseFlags
}
type jsonCodecV2 struct {
opts codec.Options
encodeFlags json.AppendFlags
decodeFlags json.ParseFlags
}
func getMarshalFlags(o JsonMarshalOptions) json.AppendFlags {
var encodeFlags json.AppendFlags
@@ -111,8 +114,10 @@ func (c *jsonCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error)
o(&options)
}
if nv, err := rutil.StructFieldByTag(v, options.TagName, flattenTag); err == nil {
v = nv
if options.Flatten {
if nv, err := rutil.StructFieldByTag(v, options.TagName, "flatten"); err == nil {
v = nv
}
}
switch m := v.(type) {
@@ -145,8 +150,10 @@ func (c *jsonCodec) Unmarshal(b []byte, v interface{}, opts ...codec.Option) err
o(&options)
}
if nv, err := rutil.StructFieldByTag(v, options.TagName, flattenTag); err == nil {
v = nv
if options.Flatten {
if nv, err := rutil.StructFieldByTag(v, options.TagName, "flatten"); err == nil {
v = nv
}
}
switch m := v.(type) {
@@ -169,41 +176,82 @@ func (c *jsonCodec) Unmarshal(b []byte, v interface{}, opts ...codec.Option) err
return err
}
func (c *jsonCodec) ReadHeader(conn io.Reader, m *codec.Message, t codec.MessageType) error {
return nil
func (c *jsonCodec) String() string {
return "json"
}
func (c *jsonCodec) ReadBody(conn io.Reader, v interface{}) error {
func (c *jsonCodecV2) Marshal(b []byte, v interface{}, opts ...codec.Option) ([]byte, error) {
if v == nil {
return nil
return nil, nil
}
buf, err := io.ReadAll(conn)
if err != nil {
return err
} else if len(buf) == 0 {
return nil
options := c.opts
for _, o := range opts {
o(&options)
}
return c.Unmarshal(buf, v)
if options.Flatten {
if nv, err := rutil.StructFieldByTag(v, options.TagName, "flatten"); err == nil {
v = nv
}
}
switch m := v.(type) {
case *codec.Frame:
return m.Data, nil
case *pb.Frame:
return m.Data, nil
}
marshalOptions := DefaultMarshalOptions
if options.Context != nil {
if f, ok := options.Context.Value(marshalOptionsKey{}).(JsonMarshalOptions); ok {
marshalOptions = f
}
}
var err error
b, err = json.Append(b, v, getMarshalFlags(marshalOptions))
return b, err
}
func (c *jsonCodec) Write(conn io.Writer, m *codec.Message, v interface{}) error {
if v == nil {
func (c *jsonCodecV2) Unmarshal(b []byte, v interface{}, opts ...codec.Option) error {
if len(b) == 0 || v == nil {
return nil
}
buf, err := c.Marshal(v)
if err != nil {
return err
} else if len(buf) == 0 {
return codec.ErrInvalidMessage
options := c.opts
for _, o := range opts {
o(&options)
}
_, err = conn.Write(buf)
if options.Flatten {
if nv, err := rutil.StructFieldByTag(v, options.TagName, "flatten"); err == nil {
v = nv
}
}
switch m := v.(type) {
case *codec.Frame:
m.Data = b
return nil
case *pb.Frame:
m.Data = b
return nil
}
unmarshalOptions := DefaultUnmarshalOptions
if options.Context != nil {
if f, ok := options.Context.Value(unmarshalOptionsKey{}).(JsonUnmarshalOptions); ok {
unmarshalOptions = f
}
}
_, err := json.Parse(b, v, getUnmarshalFlags(unmarshalOptions))
return err
}
func (c *jsonCodec) String() string {
func (c *jsonCodecV2) String() string {
return "json"
}