Compare commits

..

4 Commits
v3 ... master

Author SHA1 Message Date
2c13fb150f Merge pull request 'Update workflows' (#95) from atolstikhin/micro-codec-json:master into master
All checks were successful
test / test (push) Successful in 13m19s
Reviewed-on: #95
2024-12-14 15:51:36 +03:00
Aleksandr Tolstikhin
dc7f20944b Update workflows 2024-12-14 01:12:56 +07:00
734dfbc3d2 Merge pull request 'move to micro v4' (#93) from v4 into master
Reviewed-on: #93
2023-04-30 11:00:59 +03:00
4e33d996f1 move to micro v4
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-04-30 11:00:37 +03:00
5 changed files with 83 additions and 61 deletions

View File

@ -4,19 +4,9 @@ import (
"bytes"
"testing"
"go.unistack.org/micro/v3/broker"
"go.unistack.org/micro/v3/codec"
"go.unistack.org/micro/v4/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")}
@ -38,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)
}
@ -54,13 +44,13 @@ func TestStructByTag(t *testing.T) {
val := &Str{Name: []string{"first", "second"}}
c := NewCodec(codec.Flatten(true))
c := NewCodec()
buf, err := c.Marshal(val)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(buf, []byte(`["first","second"]`)) {
t.Fatalf("invalid marshal: %s != %s", buf, []byte(`["first","second"]`))
t.Fatalf("invalid marshal: %v != %v", buf, []byte(`["first","second"]`))
}
err = c.Unmarshal([]byte(`["1","2"]`), val)
@ -72,3 +62,15 @@ 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,15 +1,10 @@
module go.unistack.org/micro-codec-json/v3
module go.unistack.org/micro-codec-json/v4
go 1.21
toolchain go1.23.4
go 1.19
require (
go.unistack.org/micro-proto/v3 v3.4.1
go.unistack.org/micro/v3 v3.10.88
go.unistack.org/micro-proto/v4 v4.0.0
go.unistack.org/micro/v4 v4.0.1
)
require (
github.com/google/go-cmp v0.6.0 // indirect
google.golang.org/protobuf v1.35.2 // indirect
)
require google.golang.org/protobuf v1.28.1 // indirect

18
go.sum
View File

@ -1,6 +1,12 @@
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=
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=

79
json.go
View File

@ -1,18 +1,16 @@
// Package json provides a json codec
package json
package json // import "go.unistack.org/micro-codec-json/v4"
import (
"bytes"
"encoding/json"
"reflect"
"io"
pb "go.unistack.org/micro-proto/v3/codec"
"go.unistack.org/micro/v3/codec"
rutil "go.unistack.org/micro/v3/util/reflect"
pb "go.unistack.org/micro-proto/v4/codec"
"go.unistack.org/micro/v4/codec"
rutil "go.unistack.org/micro/v4/util/reflect"
)
var _ codec.Codec = (*jsonCodec)(nil)
var (
DefaultMarshalOptions = JsonMarshalOptions{
EscapeHTML: true,
@ -37,6 +35,10 @@ 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
@ -47,10 +49,8 @@ func (c *jsonCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error)
o(&options)
}
if options.Flatten {
if nv, err := rutil.StructFieldByTag(v, options.TagName, "flatten"); err == nil {
v = nv
}
if nv, err := rutil.StructFieldByTag(v, options.TagName, flattenTag); err == nil {
v = nv
}
switch m := v.(type) {
@ -58,10 +58,6 @@ 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
@ -93,15 +89,8 @@ func (c *jsonCodec) Unmarshal(b []byte, v interface{}, opts ...codec.Option) err
o(&options)
}
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()
}
}
if nv, err := rutil.StructFieldByTag(v, options.TagName, flattenTag); err == nil {
v = nv
}
switch m := v.(type) {
@ -111,12 +100,6 @@ 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
@ -141,6 +124,42 @@ 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/v3/codec"
codec "go.unistack.org/micro/v4/codec"
)
type unmarshalOptionsKey struct{}