Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-05-26 01:11:08 +03:00
parent fd2045ff05
commit 7bfb42eb86
3 changed files with 37 additions and 42 deletions

4
go.mod
View File

@ -1,5 +1,5 @@
module github.com/unistack-org/micro-codec-urlencode/v3 module github.com/unistack-org/micro-codec-urlencode/v3
go 1.15 go 1.16
require github.com/unistack-org/micro/v3 v3.3.13 require github.com/unistack-org/micro/v3 v3.3.19

10
go.sum
View File

@ -4,13 +4,13 @@ github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
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/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/unistack-org/micro/v3 v3.3.13 h1:y4bDDkbwnjgOckrhFkC6D/o42tr75X33UbrB+Ko0M68= github.com/unistack-org/micro/v3 v3.3.19 h1:x0bl4H+IZvdHHKfA8qMcox9W92+e7rtbqgRfQPoP79w=
github.com/unistack-org/micro/v3 v3.3.13/go.mod h1:98hNcMXp/WyWJwLwCuwrhN1Jm7aCWaRNsMfRjK8Fq+Y= github.com/unistack-org/micro/v3 v3.3.19/go.mod h1:LXmPfbJnJNvL0kQs8HfnkV3Wya2Wb+C7keVq++RCZnk=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= 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-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
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=

View File

@ -3,7 +3,6 @@ package urlencode
import ( import (
"io" "io"
"io/ioutil"
"github.com/unistack-org/micro/v3/codec" "github.com/unistack-org/micro/v3/codec"
rutil "github.com/unistack-org/micro/v3/util/reflect" rutil "github.com/unistack-org/micro/v3/util/reflect"
@ -11,30 +10,37 @@ import (
type urlencodeCodec struct{} type urlencodeCodec struct{}
func (c *urlencodeCodec) Marshal(b interface{}) ([]byte, error) { const (
switch m := b.(type) { flattenTag = "flatten"
case nil: )
func (c *urlencodeCodec) Marshal(v interface{}) ([]byte, error) {
if v == nil {
return nil, nil return nil, nil
case *codec.Frame: }
if m, ok := v.(*codec.Frame); ok {
return m.Data, nil return m.Data, nil
} }
v, err := rutil.StructURLValues(b, "", []string{"protobuf", "json"}) if nv, nerr := rutil.StructFieldByTag(v, codec.DefaultTagName, flattenTag); nerr == nil {
v = nv
}
uv, err := rutil.StructURLValues(v, "", []string{"protobuf", "json"})
if err != nil { if err != nil {
return nil, err return nil, err
} }
return []byte(v.Encode()), nil return []byte(uv.Encode()), nil
} }
func (c *urlencodeCodec) Unmarshal(b []byte, v interface{}) error { func (c *urlencodeCodec) Unmarshal(b []byte, v interface{}) error {
if len(b) == 0 { if len(b) == 0 || v == nil {
return nil return nil
} }
switch m := v.(type) {
case nil: if m, ok := v.(*codec.Frame); ok {
return nil
case *codec.Frame:
m.Data = b m.Data = b
return nil return nil
} }
@ -44,6 +50,10 @@ func (c *urlencodeCodec) Unmarshal(b []byte, v interface{}) error {
return err return err
} }
if nv, nerr := rutil.StructFieldByTag(v, codec.DefaultTagName, flattenTag); nerr == nil {
v = nv
}
return rutil.Merge(v, rutil.FlattenMap(mp), rutil.Tags([]string{"protobuf", "json"}), rutil.SliceAppend(true)) return rutil.Merge(v, rutil.FlattenMap(mp), rutil.Tags([]string{"protobuf", "json"}), rutil.SliceAppend(true))
} }
@ -51,52 +61,37 @@ func (c *urlencodeCodec) ReadHeader(conn io.Reader, m *codec.Message, t codec.Me
return nil return nil
} }
func (c *urlencodeCodec) ReadBody(conn io.Reader, b interface{}) error { func (c *urlencodeCodec) ReadBody(conn io.Reader, v interface{}) error {
switch m := b.(type) { if v == nil {
case nil:
return nil
case *codec.Frame:
buf, err := ioutil.ReadAll(conn)
if err != nil {
return err
} else if len(buf) == 0 {
return nil
}
m.Data = buf
return nil return nil
} }
buf, err := ioutil.ReadAll(conn) buf, err := io.ReadAll(conn)
if err != nil { if err != nil {
return err return err
} else if len(buf) == 0 { } else if len(buf) == 0 {
return nil return nil
} }
return c.Unmarshal(buf, b) return c.Unmarshal(buf, v)
} }
func (c *urlencodeCodec) Write(conn io.Writer, m *codec.Message, b interface{}) error { func (c *urlencodeCodec) Write(conn io.Writer, m *codec.Message, v interface{}) error {
switch m := b.(type) { if v == nil {
case nil:
return nil return nil
case *codec.Frame:
_, err := conn.Write(m.Data)
return err
} }
buf, err := c.Marshal(b) buf, err := c.Marshal(v)
if err != nil { if err != nil {
return err return err
} }
_, err = conn.Write(buf) _, err = conn.Write(buf)
return err return err
} }
func (c *urlencodeCodec) String() string { func (c *urlencodeCodec) String() string {
return "json" return "urlencode"
} }
func NewCodec() codec.Codec { func NewCodec() codec.Codec {