fix codec.Frame in case of flatten struct tag
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
a0580da649
commit
3895c6b703
2
go.mod
2
go.mod
@ -4,6 +4,6 @@ go 1.16
|
||||
|
||||
require (
|
||||
github.com/segmentio/encoding v0.2.21
|
||||
github.com/unistack-org/micro/v3 v3.7.1
|
||||
github.com/unistack-org/micro/v3 v3.7.4
|
||||
google.golang.org/protobuf v1.27.1
|
||||
)
|
||||
|
9
go.sum
9
go.sum
@ -1,4 +1,3 @@
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/ef-ds/deque v1.0.4/go.mod h1:gXDnTC3yqvBcHbq2lcExjtAcVrOnJCbMcZXmuj8Z4tg=
|
||||
github.com/golang-jwt/jwt/v4 v4.0.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg=
|
||||
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
||||
@ -8,18 +7,15 @@ github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH
|
||||
github.com/klauspost/cpuid/v2 v2.0.6 h1:dQ5ueTiftKxp0gyjKSx5+8BtPWkyQbd95m8Gys/RarI=
|
||||
github.com/klauspost/cpuid/v2 v2.0.6/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/segmentio/asm v1.0.1 h1:g9VK62hXylgXI4yJV+dLTu/1j7kTxG9bkUSYBxL9dpg=
|
||||
github.com/segmentio/asm v1.0.1/go.mod h1:4EUJGaKsB8ImLUwOGORVsNd9vTRDeh44JGsY4aKp5I4=
|
||||
github.com/segmentio/encoding v0.2.21 h1:hlRQz3Pv+/mBj+jqr46TVqqv6AuTwvP5aAxQ0Usd4gY=
|
||||
github.com/segmentio/encoding v0.2.21/go.mod h1:kF1db1oBuYxMvLR3RXrZJchRdBKrS+1J/hL63p5hekI=
|
||||
github.com/silas/dag v0.0.0-20210121180416-41cf55125c34/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/unistack-org/micro-proto v0.0.9 h1:KrWLS4FUX7UAWNAilQf70uad6ZPf/0EudeddCXllRVc=
|
||||
github.com/unistack-org/micro-proto v0.0.9/go.mod h1:Cckwmzd89gvS7ThxzZp9kQR/EOdksFQcsTAtDDyKwrg=
|
||||
github.com/unistack-org/micro/v3 v3.7.1 h1:gjCon1U8i9upNgw9+iEgbZh2LCeBizDYotQ+THHV0lo=
|
||||
github.com/unistack-org/micro/v3 v3.7.1/go.mod h1:gBoY6gvzeFiJTZ4FgDttGNSs4Y1+1PRg2cV1yTRMSlg=
|
||||
github.com/unistack-org/micro/v3 v3.7.4 h1:nRk7f58v0VsiW7aKL0UQaFCQxFoursHQLoWdeZ1RYbg=
|
||||
github.com/unistack-org/micro/v3 v3.7.4/go.mod h1:78vy7ggElJD+ayx8ruhJsY+SkGUupfR32LXrg1UObvg=
|
||||
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=
|
||||
@ -33,4 +29,3 @@ google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+Rur
|
||||
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
@ -3,8 +3,40 @@ package json
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
"github.com/unistack-org/micro/v3/codec"
|
||||
)
|
||||
|
||||
func TestFrame(t *testing.T) {
|
||||
s := &codec.Frame{Data: []byte("test")}
|
||||
|
||||
buf, err := NewCodec().Marshal(s)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(buf, []byte(`test`)) {
|
||||
t.Fatalf("bytes not equal %s != %s", buf, `test`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFrameFlatten(t *testing.T) {
|
||||
s := &struct {
|
||||
One string
|
||||
Name *codec.Frame `json:"name" codec:"flatten"`
|
||||
}{
|
||||
One: "xx",
|
||||
Name: &codec.Frame{Data: []byte("test")},
|
||||
}
|
||||
|
||||
buf, err := NewCodec().Marshal(s)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(buf, []byte(`test`)) {
|
||||
t.Fatalf("bytes not equal %s != %s", buf, `test`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadBody(t *testing.T) {
|
||||
s := &struct {
|
||||
Name string
|
||||
|
169
json/json.go
169
json/json.go
@ -10,24 +10,29 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
JsonMarshaler = &Marshaler{}
|
||||
DefaultMarshalOptions = JsonMarshalOptions{
|
||||
EscapeHTML: true,
|
||||
SortMapKeys: true,
|
||||
}
|
||||
|
||||
JsonUnmarshaler = &Unmarshaler{
|
||||
DefaultUnmarshalOptions = JsonUnmarshalOptions{
|
||||
ZeroCopy: true,
|
||||
}
|
||||
)
|
||||
|
||||
var _ codec.Codec = &jsonCodec{}
|
||||
|
||||
const (
|
||||
flattenTag = "flatten"
|
||||
)
|
||||
|
||||
type Marshaler struct {
|
||||
type JsonMarshalOptions struct {
|
||||
EscapeHTML bool
|
||||
SortMapKeys bool
|
||||
TrustRawMessage bool
|
||||
}
|
||||
|
||||
type Unmarshaler struct {
|
||||
type JsonUnmarshalOptions struct {
|
||||
DisallowUnknownFields bool
|
||||
DontCopyNumber bool
|
||||
DontCopyRawMessage bool
|
||||
@ -38,44 +43,121 @@ type Unmarshaler struct {
|
||||
}
|
||||
|
||||
type jsonCodec struct {
|
||||
opts codec.Options
|
||||
encodeFlags json.AppendFlags
|
||||
decodeFlags json.ParseFlags
|
||||
}
|
||||
|
||||
func (c *jsonCodec) Marshal(v interface{}) ([]byte, error) {
|
||||
switch m := v.(type) {
|
||||
case nil:
|
||||
func getMarshalFlags(o JsonMarshalOptions) json.AppendFlags {
|
||||
var encodeFlags json.AppendFlags
|
||||
|
||||
if o.EscapeHTML {
|
||||
encodeFlags |= json.EscapeHTML
|
||||
}
|
||||
|
||||
if o.SortMapKeys {
|
||||
encodeFlags |= json.SortMapKeys
|
||||
}
|
||||
|
||||
if o.TrustRawMessage {
|
||||
encodeFlags |= json.TrustRawMessage
|
||||
}
|
||||
|
||||
return encodeFlags
|
||||
}
|
||||
|
||||
func getUnmarshalFlags(o JsonUnmarshalOptions) json.ParseFlags {
|
||||
var decodeFlags json.ParseFlags
|
||||
|
||||
if o.DisallowUnknownFields {
|
||||
decodeFlags |= json.DisallowUnknownFields
|
||||
}
|
||||
|
||||
if o.DontCopyNumber {
|
||||
decodeFlags |= json.DontCopyNumber
|
||||
}
|
||||
|
||||
if o.DontCopyRawMessage {
|
||||
decodeFlags |= json.DontCopyRawMessage
|
||||
}
|
||||
|
||||
if o.DontCopyString {
|
||||
decodeFlags |= json.DontCopyString
|
||||
}
|
||||
|
||||
if o.DontMatchCaseInsensitiveStructFields {
|
||||
decodeFlags |= json.DontMatchCaseInsensitiveStructFields
|
||||
}
|
||||
|
||||
if o.UseNumber {
|
||||
decodeFlags |= json.UseNumber
|
||||
}
|
||||
|
||||
if o.ZeroCopy {
|
||||
decodeFlags |= json.ZeroCopy
|
||||
}
|
||||
|
||||
return decodeFlags
|
||||
}
|
||||
|
||||
func (c *jsonCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error) {
|
||||
if v == nil {
|
||||
return nil, nil
|
||||
case *codec.Frame:
|
||||
}
|
||||
|
||||
options := c.opts
|
||||
for _, o := range opts {
|
||||
o(&options)
|
||||
}
|
||||
|
||||
if nv, err := rutil.StructFieldByTag(v, options.TagName, flattenTag); err == nil {
|
||||
v = nv
|
||||
}
|
||||
|
||||
if m, ok := v.(*codec.Frame); ok {
|
||||
return m.Data, nil
|
||||
}
|
||||
|
||||
if nv, nerr := rutil.StructFieldByTag(v, codec.DefaultTagName, flattenTag); nerr == nil {
|
||||
v = nv
|
||||
marshalOptions := DefaultMarshalOptions
|
||||
if options.Context != nil {
|
||||
if f, ok := options.Context.Value(marshalOptionsKey{}).(JsonMarshalOptions); ok {
|
||||
marshalOptions = f
|
||||
}
|
||||
}
|
||||
|
||||
var err error
|
||||
var buf []byte
|
||||
buf, err = json.Append(buf, v, c.encodeFlags)
|
||||
buf, err = json.Append(buf, v, getMarshalFlags(marshalOptions))
|
||||
return buf, err
|
||||
}
|
||||
|
||||
func (c *jsonCodec) Unmarshal(b []byte, v interface{}) error {
|
||||
if len(b) == 0 {
|
||||
func (c *jsonCodec) Unmarshal(b []byte, v interface{}, opts ...codec.Option) error {
|
||||
if len(b) == 0 || v == nil {
|
||||
return nil
|
||||
}
|
||||
switch m := v.(type) {
|
||||
case nil:
|
||||
return nil
|
||||
case *codec.Frame:
|
||||
|
||||
options := c.opts
|
||||
for _, o := range opts {
|
||||
o(&options)
|
||||
}
|
||||
|
||||
if nv, err := rutil.StructFieldByTag(v, options.TagName, flattenTag); err == nil {
|
||||
v = nv
|
||||
}
|
||||
|
||||
if m, ok := v.(*codec.Frame); ok {
|
||||
m.Data = b
|
||||
return nil
|
||||
}
|
||||
|
||||
if nv, nerr := rutil.StructFieldByTag(v, codec.DefaultTagName, flattenTag); nerr == nil {
|
||||
v = nv
|
||||
unmarshalOptions := DefaultUnmarshalOptions
|
||||
if options.Context != nil {
|
||||
if f, ok := options.Context.Value(unmarshalOptionsKey{}).(JsonUnmarshalOptions); ok {
|
||||
unmarshalOptions = f
|
||||
}
|
||||
}
|
||||
_, err := json.Parse(b, v, c.decodeFlags)
|
||||
|
||||
_, err := json.Parse(b, v, getUnmarshalFlags(unmarshalOptions))
|
||||
return err
|
||||
}
|
||||
|
||||
@ -117,49 +199,6 @@ func (c *jsonCodec) String() string {
|
||||
return "json"
|
||||
}
|
||||
|
||||
func NewCodec() codec.Codec {
|
||||
var encodeFlags json.AppendFlags
|
||||
var decodeFlags json.ParseFlags
|
||||
|
||||
if JsonMarshaler.EscapeHTML {
|
||||
encodeFlags |= json.EscapeHTML
|
||||
}
|
||||
|
||||
if JsonMarshaler.SortMapKeys {
|
||||
encodeFlags |= json.SortMapKeys
|
||||
}
|
||||
|
||||
if JsonMarshaler.TrustRawMessage {
|
||||
encodeFlags |= json.TrustRawMessage
|
||||
}
|
||||
|
||||
if JsonUnmarshaler.DisallowUnknownFields {
|
||||
decodeFlags |= json.DisallowUnknownFields
|
||||
}
|
||||
|
||||
if JsonUnmarshaler.DontCopyNumber {
|
||||
decodeFlags |= json.DontCopyNumber
|
||||
}
|
||||
|
||||
if JsonUnmarshaler.DontCopyRawMessage {
|
||||
decodeFlags |= json.DontCopyRawMessage
|
||||
}
|
||||
|
||||
if JsonUnmarshaler.DontCopyString {
|
||||
decodeFlags |= json.DontCopyString
|
||||
}
|
||||
|
||||
if JsonUnmarshaler.DontMatchCaseInsensitiveStructFields {
|
||||
decodeFlags |= json.DontMatchCaseInsensitiveStructFields
|
||||
}
|
||||
|
||||
if JsonUnmarshaler.UseNumber {
|
||||
decodeFlags |= json.UseNumber
|
||||
}
|
||||
|
||||
if JsonUnmarshaler.ZeroCopy {
|
||||
decodeFlags |= json.ZeroCopy
|
||||
}
|
||||
|
||||
return &jsonCodec{encodeFlags: encodeFlags, decodeFlags: decodeFlags}
|
||||
func NewCodec(opts ...codec.Option) *jsonCodec {
|
||||
return &jsonCodec{opts: codec.NewOptions(opts...)}
|
||||
}
|
||||
|
17
json/options.go
Normal file
17
json/options.go
Normal file
@ -0,0 +1,17 @@
|
||||
package json
|
||||
|
||||
import (
|
||||
codec "github.com/unistack-org/micro/v3/codec"
|
||||
)
|
||||
|
||||
type unmarshalOptionsKey struct{}
|
||||
|
||||
func UnmarshalOptions(o JsonUnmarshalOptions) codec.Option {
|
||||
return codec.SetOption(unmarshalOptionsKey{}, o)
|
||||
}
|
||||
|
||||
type marshalOptionsKey struct{}
|
||||
|
||||
func MarshalOptions(o JsonMarshalOptions) codec.Option {
|
||||
return codec.SetOption(marshalOptionsKey{}, o)
|
||||
}
|
@ -3,8 +3,40 @@ package proto
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
"github.com/unistack-org/micro/v3/codec"
|
||||
)
|
||||
|
||||
func TestFrame(t *testing.T) {
|
||||
s := &codec.Frame{Data: []byte("test")}
|
||||
|
||||
buf, err := NewCodec().Marshal(s)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(buf, []byte(`test`)) {
|
||||
t.Fatalf("bytes not equal %s != %s", buf, `test`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFrameFlatten(t *testing.T) {
|
||||
s := &struct {
|
||||
One string
|
||||
Name *codec.Frame `json:"name" codec:"flatten"`
|
||||
}{
|
||||
One: "xx",
|
||||
Name: &codec.Frame{Data: []byte("test")},
|
||||
}
|
||||
|
||||
buf, err := NewCodec().Marshal(s)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(buf, []byte(`test`)) {
|
||||
t.Fatalf("bytes not equal %s != %s", buf, `test`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadBody(t *testing.T) {
|
||||
t.Skip("no proto generated")
|
||||
s := &struct {
|
||||
|
@ -10,52 +10,71 @@ import (
|
||||
newproto "google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
type protoCodec struct{}
|
||||
type protoCodec struct {
|
||||
opts codec.Options
|
||||
}
|
||||
|
||||
var _ codec.Codec = &protoCodec{}
|
||||
|
||||
const (
|
||||
flattenTag = "flatten"
|
||||
)
|
||||
|
||||
func (c *protoCodec) Marshal(v interface{}) ([]byte, error) {
|
||||
switch m := v.(type) {
|
||||
case nil:
|
||||
func (c *protoCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error) {
|
||||
if v == nil {
|
||||
return nil, nil
|
||||
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
|
||||
|
||||
options := c.opts
|
||||
for _, o := range opts {
|
||||
o(&options)
|
||||
}
|
||||
|
||||
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, flattenTag); nerr == nil {
|
||||
v = nv
|
||||
}
|
||||
|
||||
if m, ok := v.(*codec.Frame); ok {
|
||||
return m.Data, nil
|
||||
}
|
||||
|
||||
switch v.(type) {
|
||||
case proto.Message, newproto.Message:
|
||||
break
|
||||
default:
|
||||
return nil, codec.ErrInvalidMessage
|
||||
}
|
||||
|
||||
return proto.Marshal(v.(proto.Message))
|
||||
}
|
||||
|
||||
func (c *protoCodec) Unmarshal(d []byte, v interface{}) error {
|
||||
if d == nil {
|
||||
func (c *protoCodec) Unmarshal(d []byte, v interface{}, opts ...codec.Option) error {
|
||||
if v == nil || len(d) == 0 {
|
||||
return nil
|
||||
}
|
||||
switch m := v.(type) {
|
||||
case nil:
|
||||
return nil
|
||||
case *codec.Frame:
|
||||
|
||||
options := c.opts
|
||||
for _, o := range opts {
|
||||
o(&options)
|
||||
}
|
||||
|
||||
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, flattenTag); nerr == nil {
|
||||
v = nv
|
||||
}
|
||||
|
||||
if m, ok := v.(*codec.Frame); ok {
|
||||
m.Data = d
|
||||
return 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.Unmarshal(d, m)
|
||||
}
|
||||
return codec.ErrInvalidMessage
|
||||
|
||||
switch v.(type) {
|
||||
case proto.Message, newproto.Message:
|
||||
break
|
||||
default:
|
||||
return codec.ErrInvalidMessage
|
||||
}
|
||||
|
||||
return proto.Unmarshal(d, v.(proto.Message))
|
||||
}
|
||||
|
||||
func (c *protoCodec) ReadHeader(conn io.Reader, m *codec.Message, t codec.MessageType) error {
|
||||
@ -95,6 +114,6 @@ func (c *protoCodec) String() string {
|
||||
return "proto"
|
||||
}
|
||||
|
||||
func NewCodec() codec.Codec {
|
||||
return &protoCodec{}
|
||||
func NewCodec(opts ...codec.Option) *protoCodec {
|
||||
return &protoCodec{opts: codec.NewOptions(opts...)}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user