From 7bfb42eb865807eb604e45c4e7c56aff8fa46b83 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Wed, 26 May 2021 01:11:08 +0300 Subject: [PATCH] simplify Signed-off-by: Vasiliy Tolstov --- go.mod | 4 ++-- go.sum | 10 ++++---- urlencode.go | 65 ++++++++++++++++++++++++---------------------------- 3 files changed, 37 insertions(+), 42 deletions(-) diff --git a/go.mod b/go.mod index 9c658c4..8417b11 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,5 @@ 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 diff --git a/go.sum b/go.sum index 3e07cd4..07856fa 100644 --- a/go.sum +++ b/go.sum @@ -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/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/unistack-org/micro/v3 v3.3.13 h1:y4bDDkbwnjgOckrhFkC6D/o42tr75X33UbrB+Ko0M68= -github.com/unistack-org/micro/v3 v3.3.13/go.mod h1:98hNcMXp/WyWJwLwCuwrhN1Jm7aCWaRNsMfRjK8Fq+Y= -golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= +github.com/unistack-org/micro/v3 v3.3.19 h1:x0bl4H+IZvdHHKfA8qMcox9W92+e7rtbqgRfQPoP79w= +github.com/unistack-org/micro/v3 v3.3.19/go.mod h1:LXmPfbJnJNvL0kQs8HfnkV3Wya2Wb+C7keVq++RCZnk= +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-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/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= 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= diff --git a/urlencode.go b/urlencode.go index 7a943a2..0f35247 100644 --- a/urlencode.go +++ b/urlencode.go @@ -3,7 +3,6 @@ package urlencode import ( "io" - "io/ioutil" "github.com/unistack-org/micro/v3/codec" rutil "github.com/unistack-org/micro/v3/util/reflect" @@ -11,30 +10,37 @@ import ( type urlencodeCodec struct{} -func (c *urlencodeCodec) Marshal(b interface{}) ([]byte, error) { - switch m := b.(type) { - case nil: +const ( + flattenTag = "flatten" +) + +func (c *urlencodeCodec) Marshal(v interface{}) ([]byte, error) { + if v == nil { return nil, nil - case *codec.Frame: + } + + if m, ok := v.(*codec.Frame); ok { 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 { return nil, err } - return []byte(v.Encode()), nil + return []byte(uv.Encode()), nil } func (c *urlencodeCodec) Unmarshal(b []byte, v interface{}) error { - if len(b) == 0 { + if len(b) == 0 || v == nil { return nil } - switch m := v.(type) { - case nil: - return nil - case *codec.Frame: + + if m, ok := v.(*codec.Frame); ok { m.Data = b return nil } @@ -44,6 +50,10 @@ func (c *urlencodeCodec) Unmarshal(b []byte, v interface{}) error { 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)) } @@ -51,52 +61,37 @@ func (c *urlencodeCodec) ReadHeader(conn io.Reader, m *codec.Message, t codec.Me return nil } -func (c *urlencodeCodec) ReadBody(conn io.Reader, b interface{}) error { - switch m := b.(type) { - 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 +func (c *urlencodeCodec) ReadBody(conn io.Reader, v interface{}) error { + if v == nil { return nil } - buf, err := ioutil.ReadAll(conn) + buf, err := io.ReadAll(conn) if err != nil { return err } else if len(buf) == 0 { 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 { - switch m := b.(type) { - case nil: +func (c *urlencodeCodec) Write(conn io.Writer, m *codec.Message, v interface{}) error { + if v == 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 { return err } _, err = conn.Write(buf) - return err } func (c *urlencodeCodec) String() string { - return "json" + return "urlencode" } func NewCodec() codec.Codec {