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")},
}
buf, err := NewCodec(codec.Flatten(true)).Marshal(s)
buf, err := NewCodec().Marshal(s)
if err != nil {
t.Fatal(err)
}
@ -36,3 +36,16 @@ func TestFrameFlatten(t *testing.T) {
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)
}
}

155
jsonpb.go
View File

@ -1,7 +1,9 @@
// Package jsonpb provides a json protobuf codec
package jsonpb
// Package jsonpb provides a json codec
package jsonpb // import "go.unistack.org/micro-codec-jsonpb/v3"
import (
"io"
pb "go.unistack.org/micro-proto/v3/codec"
"go.unistack.org/micro/v3/codec"
rutil "go.unistack.org/micro/v3/util/reflect"
@ -27,13 +29,8 @@ type jsonpbCodec struct {
opts codec.Options
}
type jsonpbCodecV2 struct {
opts codec.Options
}
var (
_ codec.Codec = (*jsonpbCodec)(nil)
_ codec.CodecV2 = (*jsonpbCodecV2)(nil)
const (
flattenTag = "flatten"
)
func (c *jsonpbCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error) {
@ -46,28 +43,29 @@ func (c *jsonpbCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, erro
o(&options)
}
if options.Flatten {
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, "flatten"); nerr == nil {
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, flattenTag); 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:
}
if _, ok := v.(proto.Message); !ok {
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(m)
default:
return nil, codec.ErrInvalidMessage
}
return marshalOptions.Marshal(v.(proto.Message))
}
func (c *jsonpbCodec) Unmarshal(d []byte, v interface{}, opts ...codec.Option) error {
@ -80,11 +78,9 @@ func (c *jsonpbCodec) Unmarshal(d []byte, v interface{}, opts ...codec.Option) e
o(&options)
}
if options.Flatten {
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, "flatten"); nerr == nil {
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, flattenTag); nerr == nil {
v = nv
}
}
switch m := v.(type) {
case *codec.Frame:
@ -93,17 +89,52 @@ func (c *jsonpbCodec) Unmarshal(d []byte, v interface{}, opts ...codec.Option) e
case *pb.Frame:
m.Data = d
return nil
case proto.Message:
}
if _, ok := v.(proto.Message); !ok {
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, m)
default:
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 {
@ -113,81 +144,3 @@ func (c *jsonpbCodec) String() string {
func NewCodec(opts ...codec.Option) codec.Codec {
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...)}
}