Compare commits

..

No commits in common. "v3" and "v3.10.1" have entirely different histories.
v3 ... v3.10.1

2 changed files with 80 additions and 114 deletions

View File

@ -28,7 +28,7 @@ func TestFrameFlatten(t *testing.T) {
Name: &codec.Frame{Data: []byte("test")}, Name: &codec.Frame{Data: []byte("test")},
} }
buf, err := NewCodec(codec.Flatten(true)).Marshal(s) buf, err := NewCodec().Marshal(s)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -36,3 +36,16 @@ func TestFrameFlatten(t *testing.T) {
t.Fatalf("bytes not equal %s != %s", buf, `test`) t.Fatalf("bytes not equal %s != %s", buf, `test`)
} }
} }
func TestReadBody(t *testing.T) {
t.Skip("skip without proto message")
s := &struct {
Name string
}{}
c := NewCodec()
b := bytes.NewReader(nil)
err := c.ReadBody(b, s)
if err != nil {
t.Fatal(err)
}
}

179
jsonpb.go
View File

@ -1,7 +1,9 @@
// Package jsonpb provides a json protobuf codec // Package jsonpb provides a json codec
package jsonpb package jsonpb // import "go.unistack.org/micro-codec-jsonpb/v3"
import ( import (
"io"
pb "go.unistack.org/micro-proto/v3/codec" pb "go.unistack.org/micro-proto/v3/codec"
"go.unistack.org/micro/v3/codec" "go.unistack.org/micro/v3/codec"
rutil "go.unistack.org/micro/v3/util/reflect" rutil "go.unistack.org/micro/v3/util/reflect"
@ -27,13 +29,8 @@ type jsonpbCodec struct {
opts codec.Options opts codec.Options
} }
type jsonpbCodecV2 struct { const (
opts codec.Options flattenTag = "flatten"
}
var (
_ codec.Codec = (*jsonpbCodec)(nil)
_ codec.CodecV2 = (*jsonpbCodecV2)(nil)
) )
func (c *jsonpbCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error) { func (c *jsonpbCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error) {
@ -46,10 +43,8 @@ func (c *jsonpbCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, erro
o(&options) o(&options)
} }
if options.Flatten { if nv, nerr := rutil.StructFieldByTag(v, options.TagName, flattenTag); nerr == nil {
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, "flatten"); nerr == nil { v = nv
v = nv
}
} }
switch m := v.(type) { switch m := v.(type) {
@ -57,17 +52,20 @@ func (c *jsonpbCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, erro
return m.Data, nil return m.Data, nil
case *pb.Frame: case *pb.Frame:
return m.Data, nil return m.Data, nil
case proto.Message: }
marshalOptions := DefaultMarshalOptions
if options.Context != nil { if _, ok := v.(proto.Message); !ok {
if f, ok := options.Context.Value(marshalOptionsKey{}).(jsonpb.MarshalOptions); ok {
marshalOptions = f
}
}
return marshalOptions.Marshal(m)
default:
return nil, codec.ErrInvalidMessage return nil, codec.ErrInvalidMessage
} }
marshalOptions := DefaultMarshalOptions
if options.Context != nil {
if f, ok := options.Context.Value(marshalOptionsKey{}).(jsonpb.MarshalOptions); ok {
marshalOptions = f
}
}
return marshalOptions.Marshal(v.(proto.Message))
} }
func (c *jsonpbCodec) Unmarshal(d []byte, v interface{}, opts ...codec.Option) error { func (c *jsonpbCodec) Unmarshal(d []byte, v interface{}, opts ...codec.Option) error {
@ -80,10 +78,8 @@ func (c *jsonpbCodec) Unmarshal(d []byte, v interface{}, opts ...codec.Option) e
o(&options) o(&options)
} }
if options.Flatten { if nv, nerr := rutil.StructFieldByTag(v, options.TagName, flattenTag); nerr == nil {
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, "flatten"); nerr == nil { v = nv
v = nv
}
} }
switch m := v.(type) { switch m := v.(type) {
@ -93,17 +89,52 @@ func (c *jsonpbCodec) Unmarshal(d []byte, v interface{}, opts ...codec.Option) e
case *pb.Frame: case *pb.Frame:
m.Data = d m.Data = d
return nil return nil
case proto.Message: }
unmarshalOptions := DefaultUnmarshalOptions
if options.Context != nil { if _, ok := v.(proto.Message); !ok {
if f, ok := options.Context.Value(unmarshalOptionsKey{}).(jsonpb.UnmarshalOptions); ok {
unmarshalOptions = f
}
}
return unmarshalOptions.Unmarshal(d, m)
default:
return codec.ErrInvalidMessage return codec.ErrInvalidMessage
} }
unmarshalOptions := DefaultUnmarshalOptions
if options.Context != nil {
if f, ok := options.Context.Value(unmarshalOptionsKey{}).(jsonpb.UnmarshalOptions); ok {
unmarshalOptions = f
}
}
return unmarshalOptions.Unmarshal(d, v.(proto.Message))
}
func (c *jsonpbCodec) ReadHeader(conn io.Reader, m *codec.Message, t codec.MessageType) error {
return nil
}
func (c *jsonpbCodec) ReadBody(conn io.Reader, v interface{}) error {
if v == nil {
return nil
}
buf, err := io.ReadAll(conn)
if err != nil {
return err
} else if len(buf) == 0 {
return nil
}
return c.Unmarshal(buf, v)
}
func (c *jsonpbCodec) Write(conn io.Writer, m *codec.Message, v interface{}) error {
if v == nil {
return nil
}
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 *jsonpbCodec) String() string { func (c *jsonpbCodec) String() string {
@ -113,81 +144,3 @@ func (c *jsonpbCodec) String() string {
func NewCodec(opts ...codec.Option) codec.Codec { func NewCodec(opts ...codec.Option) codec.Codec {
return &jsonpbCodec{opts: codec.NewOptions(opts...)} return &jsonpbCodec{opts: codec.NewOptions(opts...)}
} }
func (c *jsonpbCodecV2) Marshal(d []byte, v interface{}, opts ...codec.Option) ([]byte, error) {
if v == nil {
return nil, nil
}
options := c.opts
for _, o := range opts {
o(&options)
}
if options.Flatten {
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, "flatten"); nerr == nil {
v = nv
}
}
switch m := v.(type) {
case *codec.Frame:
return m.Data, nil
case *pb.Frame:
return m.Data, nil
case proto.Message:
marshalOptions := DefaultMarshalOptions
if options.Context != nil {
if f, ok := options.Context.Value(marshalOptionsKey{}).(jsonpb.MarshalOptions); ok {
marshalOptions = f
}
}
return marshalOptions.MarshalAppend(d[:0], m)
default:
return nil, codec.ErrInvalidMessage
}
}
func (c *jsonpbCodecV2) Unmarshal(d []byte, v interface{}, opts ...codec.Option) error {
if v == nil || len(d) == 0 {
return nil
}
options := c.opts
for _, o := range opts {
o(&options)
}
if options.Flatten {
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, "flatten"); nerr == nil {
v = nv
}
}
switch m := v.(type) {
case *codec.Frame:
m.Data = d
return nil
case *pb.Frame:
m.Data = d
return nil
case proto.Message:
unmarshalOptions := DefaultUnmarshalOptions
if options.Context != nil {
if f, ok := options.Context.Value(unmarshalOptionsKey{}).(jsonpb.UnmarshalOptions); ok {
unmarshalOptions = f
}
}
return unmarshalOptions.Unmarshal(d, m)
default:
return codec.ErrInvalidMessage
}
}
func (c *jsonpbCodecV2) String() string {
return "jsonpb"
}
func NewCodecV2(opts ...codec.Option) codec.CodecV2 {
return &jsonpbCodecV2{opts: codec.NewOptions(opts...)}
}