fix codec.Frame in case of flatten struct tag
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
		| @@ -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 TestStructByTag(t *testing.T) { | ||||
| 	type Str struct { | ||||
| 		Name []string `json:"name" codec:"flatten"` | ||||
|   | ||||
							
								
								
									
										18
									
								
								json.go
									
									
									
									
									
								
							
							
						
						
									
										18
									
								
								json.go
									
									
									
									
									
								
							| @@ -43,10 +43,6 @@ func (c *jsonCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error) | ||||
| 		return nil, nil | ||||
| 	} | ||||
|  | ||||
| 	if m, ok := v.(*codec.Frame); ok { | ||||
| 		return m.Data, nil | ||||
| 	} | ||||
|  | ||||
| 	options := c.opts | ||||
| 	for _, o := range opts { | ||||
| 		o(&options) | ||||
| @@ -56,6 +52,10 @@ func (c *jsonCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error) | ||||
| 		v = nv | ||||
| 	} | ||||
|  | ||||
| 	if m, ok := v.(*codec.Frame); ok { | ||||
| 		return m.Data, nil | ||||
| 	} | ||||
|  | ||||
| 	marshalOptions := DefaultMarshalOptions | ||||
| 	if options.Context != nil { | ||||
| 		if f, ok := options.Context.Value(marshalOptionsKey{}).(JsonMarshalOptions); ok { | ||||
| @@ -80,11 +80,6 @@ func (c *jsonCodec) Unmarshal(b []byte, v interface{}, opts ...codec.Option) err | ||||
| 		return nil | ||||
| 	} | ||||
|  | ||||
| 	if m, ok := v.(*codec.Frame); ok { | ||||
| 		m.Data = b | ||||
| 		return nil | ||||
| 	} | ||||
|  | ||||
| 	options := c.opts | ||||
| 	for _, o := range opts { | ||||
| 		o(&options) | ||||
| @@ -94,6 +89,11 @@ func (c *jsonCodec) Unmarshal(b []byte, v interface{}, opts ...codec.Option) err | ||||
| 		v = nv | ||||
| 	} | ||||
|  | ||||
| 	if m, ok := v.(*codec.Frame); ok { | ||||
| 		m.Data = b | ||||
| 		return nil | ||||
| 	} | ||||
|  | ||||
| 	unmarshalOptions := DefaultUnmarshalOptions | ||||
| 	if options.Context != nil { | ||||
| 		if f, ok := options.Context.Value(unmarshalOptionsKey{}).(JsonUnmarshalOptions); ok { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user