Compare commits

..

5 Commits
master ... v3

Author SHA1 Message Date
03713a136f add codec.RawMessage support
All checks were successful
test / test (push) Successful in 2m38s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-12-23 20:38:59 +03:00
aaedc8569a add codec.RawMessage support
All checks were successful
test / test (push) Successful in 1m36s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-12-23 19:22:26 +03:00
f6d61d4cc8 Merge pull request 'Update workflows' (#94) from atolstikhin/micro-codec-json:v3 into v3
All checks were successful
test / test (push) Successful in 13m25s
Reviewed-on: #94
2024-12-14 15:51:46 +03:00
Aleksandr Tolstikhin
1077a306ee Update workflows 2024-12-14 01:13:53 +07:00
2f33ef006e update codec to allow reuse buffers
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-09-17 00:38:06 +03:00
5 changed files with 61 additions and 83 deletions

View File

@ -4,9 +4,19 @@ import (
"bytes"
"testing"
"go.unistack.org/micro/v4/codec"
"go.unistack.org/micro/v3/broker"
"go.unistack.org/micro/v3/codec"
)
func TestRawMessage(t *testing.T) {
b := &broker.Message{}
buf, err := NewCodec().Marshal(b)
if err != nil {
panic(err)
}
_ = buf
}
func TestFrame(t *testing.T) {
s := &codec.Frame{Data: []byte("test")}
@ -28,7 +38,7 @@ func TestFrameFlatten(t *testing.T) {
Name: &codec.Frame{Data: []byte("test")},
}
buf, err := NewCodec().Marshal(s)
buf, err := NewCodec(codec.Flatten(true)).Marshal(s)
if err != nil {
t.Fatal(err)
}
@ -44,13 +54,13 @@ func TestStructByTag(t *testing.T) {
val := &Str{Name: []string{"first", "second"}}
c := NewCodec()
c := NewCodec(codec.Flatten(true))
buf, err := c.Marshal(val)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(buf, []byte(`["first","second"]`)) {
t.Fatalf("invalid marshal: %v != %v", buf, []byte(`["first","second"]`))
t.Fatalf("invalid marshal: %s != %s", buf, []byte(`["first","second"]`))
}
err = c.Unmarshal([]byte(`["1","2"]`), val)
@ -62,15 +72,3 @@ func TestStructByTag(t *testing.T) {
t.Fatalf("invalid unmarshal: %v", val)
}
}
func TestReadBody(t *testing.T) {
s := &struct {
Name string
}{}
c := NewCodec()
b := bytes.NewReader(nil)
err := c.ReadBody(b, s)
if err != nil {
t.Fatal(err)
}
}

15
go.mod
View File

@ -1,10 +1,15 @@
module go.unistack.org/micro-codec-json/v4
module go.unistack.org/micro-codec-json/v3
go 1.19
go 1.21
toolchain go1.23.4
require (
go.unistack.org/micro-proto/v4 v4.0.0
go.unistack.org/micro/v4 v4.0.1
go.unistack.org/micro-proto/v3 v3.4.1
go.unistack.org/micro/v3 v3.10.88
)
require google.golang.org/protobuf v1.28.1 // indirect
require (
github.com/google/go-cmp v0.6.0 // indirect
google.golang.org/protobuf v1.35.2 // indirect
)

18
go.sum
View File

@ -1,12 +1,6 @@
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
go.unistack.org/micro-proto/v4 v4.0.0 h1:+TAwKSlx48eLsNO2E2cO0JLE8we1KU2AuWe9Q310FuU=
go.unistack.org/micro-proto/v4 v4.0.0/go.mod h1:ArmK7o+uFvxSY3dbJhKBBX4Pm1rhWdLEFf3LxBrMtec=
go.unistack.org/micro/v4 v4.0.1 h1:xo1IxbVfgh8i0eY0VeYa3cbb13u5n/Mxnp3FOgWD4Jo=
go.unistack.org/micro/v4 v4.0.1/go.mod h1:p/J5UcSJjfHsWGT31uKoghQ5rUQZzQJBAFy+Z4+ZVMs=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
go.unistack.org/micro-proto/v3 v3.4.1 h1:UTjLSRz2YZuaHk9iSlVqqsA50JQNAEK2ZFboGqtEa9Q=
go.unistack.org/micro-proto/v3 v3.4.1/go.mod h1:okx/cnOhzuCX0ggl/vToatbCupi0O44diiiLLsZ93Zo=
go.unistack.org/micro/v3 v3.10.88 h1:MxlzP+77Y6Kphb3lzHxROL4XfE/WdCQMQpnPv4D9Z8U=
go.unistack.org/micro/v3 v3.10.88/go.mod h1:erMgt3Bl7vQQ0e9UpQyR5NlLiZ9pKeEJ9+1tfYFaqUg=
google.golang.org/protobuf v1.35.2 h1:8Ar7bF+apOIoThw1EdZl0p1oWvMqTHmpA2fRTyZO8io=

79
json.go
View File

@ -1,16 +1,18 @@
// Package json provides a json codec
package json // import "go.unistack.org/micro-codec-json/v4"
package json
import (
"bytes"
"encoding/json"
"io"
"reflect"
pb "go.unistack.org/micro-proto/v4/codec"
"go.unistack.org/micro/v4/codec"
rutil "go.unistack.org/micro/v4/util/reflect"
pb "go.unistack.org/micro-proto/v3/codec"
"go.unistack.org/micro/v3/codec"
rutil "go.unistack.org/micro/v3/util/reflect"
)
var _ codec.Codec = (*jsonCodec)(nil)
var (
DefaultMarshalOptions = JsonMarshalOptions{
EscapeHTML: true,
@ -35,10 +37,6 @@ type jsonCodec struct {
opts codec.Options
}
const (
flattenTag = "flatten"
)
func (c *jsonCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error) {
if v == nil {
return nil, nil
@ -49,8 +47,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) {
@ -58,6 +58,10 @@ func (c *jsonCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error)
return m.Data, nil
case *pb.Frame:
return m.Data, nil
case codec.RawMessage:
return []byte(m), nil
case *codec.RawMessage:
return []byte(*m), nil
}
marshalOptions := DefaultMarshalOptions
@ -89,8 +93,15 @@ 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
rv := reflect.ValueOf(v)
if rv.Kind() != reflect.Pointer &&
rv.Kind() != reflect.Map {
v = reflect.New(rv.Type()).Interface()
}
}
}
switch m := v.(type) {
@ -100,6 +111,12 @@ func (c *jsonCodec) Unmarshal(b []byte, v interface{}, opts ...codec.Option) err
case *pb.Frame:
m.Data = b
return nil
case *codec.RawMessage:
*m = append((*m)[0:0], b...)
return nil
case codec.RawMessage:
copy(m, b)
return nil
}
unmarshalOptions := DefaultUnmarshalOptions
@ -124,42 +141,6 @@ func (c *jsonCodec) Unmarshal(b []byte, v interface{}, opts ...codec.Option) err
return json.Unmarshal(b, v)
}
func (c *jsonCodec) ReadHeader(conn io.Reader, m *codec.Message, t codec.MessageType) error {
return nil
}
func (c *jsonCodec) 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
}
if nv, nerr := rutil.StructFieldByTag(v, codec.DefaultTagName, flattenTag); nerr == nil {
v = nv
}
return c.Unmarshal(buf, v)
}
func (c *jsonCodec) 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
}
_, err = conn.Write(buf)
return err
}
func (c *jsonCodec) String() string {
return "json"
}

View File

@ -1,7 +1,7 @@
package json
import (
codec "go.unistack.org/micro/v4/codec"
codec "go.unistack.org/micro/v3/codec"
)
type unmarshalOptionsKey struct{}