update to latest micro

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-09-22 02:05:09 +03:00
parent be6d3b806f
commit cddee4ddb0
5 changed files with 96 additions and 17 deletions

View File

@ -18,7 +18,7 @@ func TestStructByTag(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
if !bytes.Equal(buf, []byte(`["first","second"]`)) { if !bytes.Equal(buf, []byte(`["first","second"]`)) {
t.Fatalf("invalid marshal: %s", buf) t.Fatalf("invalid marshal: %v != %v", buf, []byte(`["first","second"]`))
} }
err = c.Unmarshal([]byte(`["1","2"]`), val) err = c.Unmarshal([]byte(`["1","2"]`), val)
@ -29,7 +29,6 @@ func TestStructByTag(t *testing.T) {
if len(val.Name) != 2 { if len(val.Name) != 2 {
t.Fatalf("invalid unmarshal: %v", val) t.Fatalf("invalid unmarshal: %v", val)
} }
} }
func TestReadBody(t *testing.T) { func TestReadBody(t *testing.T) {

2
go.mod
View File

@ -2,4 +2,4 @@ module github.com/unistack-org/micro-codec-json/v3
go 1.16 go 1.16
require github.com/unistack-org/micro/v3 v3.7.1 require github.com/unistack-org/micro/v3 v3.7.3

9
go.sum
View File

@ -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/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-jwt/jwt/v4 v4.0.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
@ -6,14 +5,11 @@ 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= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= 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/silas/dag v0.0.0-20210121180416-41cf55125c34/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I= 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 h1:KrWLS4FUX7UAWNAilQf70uad6ZPf/0EudeddCXllRVc=
github.com/unistack-org/micro-proto v0.0.9/go.mod h1:Cckwmzd89gvS7ThxzZp9kQR/EOdksFQcsTAtDDyKwrg= 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.3 h1:7dCv7WuTYp47jZ/doVCXDNMnY75imprbmwSnmchTSJY=
github.com/unistack-org/micro/v3 v3.7.1/go.mod h1:gBoY6gvzeFiJTZ4FgDttGNSs4Y1+1PRg2cV1yTRMSlg= github.com/unistack-org/micro/v3 v3.7.3/go.mod h1:78vy7ggElJD+ayx8ruhJsY+SkGUupfR32LXrg1UObvg=
golang.org/x/net v0.0.0-20210510120150-4163338589ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= 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-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@ -27,4 +23,3 @@ google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+Rur
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= 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/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.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=

82
json.go
View File

@ -2,6 +2,7 @@
package json package json
import ( import (
"bytes"
"encoding/json" "encoding/json"
"io" "io"
@ -9,13 +10,35 @@ import (
rutil "github.com/unistack-org/micro/v3/util/reflect" rutil "github.com/unistack-org/micro/v3/util/reflect"
) )
type jsonCodec struct{} var (
DefaultMarshalOptions = JsonMarshalOptions{
EscapeHTML: true,
}
DefaultUnmarshalOptions = JsonUnmarshalOptions{
DisallowUnknownFields: false,
UseNumber: false,
}
)
type JsonMarshalOptions struct {
EscapeHTML bool
}
type JsonUnmarshalOptions struct {
DisallowUnknownFields bool
UseNumber bool
}
type jsonCodec struct {
opts codec.Options
}
const ( const (
flattenTag = "flatten" flattenTag = "flatten"
) )
func (c *jsonCodec) Marshal(v interface{}) ([]byte, error) { func (c *jsonCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error) {
if v == nil { if v == nil {
return nil, nil return nil, nil
} }
@ -24,14 +47,35 @@ func (c *jsonCodec) Marshal(v interface{}) ([]byte, error) {
return m.Data, nil return m.Data, nil
} }
if nv, err := rutil.StructFieldByTag(v, codec.DefaultTagName, flattenTag); err == nil { options := c.opts
for _, o := range opts {
o(&options)
}
if nv, err := rutil.StructFieldByTag(v, options.TagName, flattenTag); err == nil {
v = nv v = nv
} }
marshalOptions := DefaultMarshalOptions
if c.opts.Context != nil {
if f, ok := c.opts.Context.Value(marshalOptionsKey{}).(JsonMarshalOptions); ok {
marshalOptions = f
}
}
if !marshalOptions.EscapeHTML {
w := bytes.NewBuffer(nil)
enc := json.NewEncoder(w)
enc.SetEscapeHTML(marshalOptions.EscapeHTML)
err := enc.Encode(v)
buf := w.Bytes()
return buf[:len(buf)-1], err
}
return json.Marshal(v) return json.Marshal(v)
} }
func (c *jsonCodec) Unmarshal(b []byte, v interface{}) error { func (c *jsonCodec) Unmarshal(b []byte, v interface{}, opts ...codec.Option) error {
if len(b) == 0 || v == nil { if len(b) == 0 || v == nil {
return nil return nil
} }
@ -41,10 +85,34 @@ func (c *jsonCodec) Unmarshal(b []byte, v interface{}) error {
return nil return nil
} }
if nv, err := rutil.StructFieldByTag(v, codec.DefaultTagName, flattenTag); err == nil { options := c.opts
for _, o := range opts {
o(&options)
}
if nv, err := rutil.StructFieldByTag(v, options.TagName, flattenTag); err == nil {
v = nv v = nv
} }
unmarshalOptions := DefaultUnmarshalOptions
if c.opts.Context != nil {
if f, ok := c.opts.Context.Value(unmarshalOptionsKey{}).(JsonUnmarshalOptions); ok {
unmarshalOptions = f
}
}
if unmarshalOptions.DisallowUnknownFields || unmarshalOptions.UseNumber {
dec := json.NewDecoder(bytes.NewBuffer(b))
if unmarshalOptions.DisallowUnknownFields {
dec.DisallowUnknownFields()
}
if unmarshalOptions.UseNumber {
dec.UseNumber()
}
return dec.Decode(v)
}
return json.Unmarshal(b, v) return json.Unmarshal(b, v)
} }
@ -88,6 +156,6 @@ func (c *jsonCodec) String() string {
return "json" return "json"
} }
func NewCodec() codec.Codec { func NewCodec(opts ...codec.Option) codec.Codec {
return &jsonCodec{} return &jsonCodec{opts: codec.NewOptions(opts...)}
} }

17
options.go Normal file
View 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 JsonUnmarshalOptions) codec.Option {
return codec.SetOption(marshalOptionsKey{}, o)
}