Compare commits

...

2 Commits
master ... v3

Author SHA1 Message Date
0b6698d75f update to latest micro, optimize flatten
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-09-17 12:21:13 +03:00
100a44103e add ability to unmarshal to structpb.Value
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-02-05 13:39:32 +03:00
4 changed files with 35 additions and 62 deletions

View File

@ -28,7 +28,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)
}
@ -36,15 +36,3 @@ func TestFrameFlatten(t *testing.T) {
t.Fatalf("bytes not equal %s != %s", buf, `test`)
}
}
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)
}
}

9
go.mod
View File

@ -1,10 +1,9 @@
module go.unistack.org/micro-codec-urlencode/v3
go 1.19
go 1.20
require (
go.unistack.org/micro-proto/v3 v3.3.1
go.unistack.org/micro/v3 v3.10.14
go.unistack.org/micro-proto/v3 v3.4.1
go.unistack.org/micro/v3 v3.10.88
google.golang.org/protobuf v1.34.2
)
require google.golang.org/protobuf v1.28.1 // indirect

9
go.sum
View File

@ -66,8 +66,12 @@ github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQ
go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI=
go.unistack.org/micro-proto/v3 v3.3.1 h1:nQ0MtWvP2G3QrpOgawVOPhpZZYkq6umTGDqs8FxJYIo=
go.unistack.org/micro-proto/v3 v3.3.1/go.mod h1:cwRyv8uInM2I7EbU7O8Fx2Ls3N90Uw9UCCcq4olOdfE=
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.14 h1:7fgLpwGlCN67twhwtngJDEQvrMkUBDSA5vzZqxIDqNE=
go.unistack.org/micro/v3 v3.10.14/go.mod h1:uMAc0U/x7dmtICCrblGf0ZLgYegu3VwQAquu+OFCw1Q=
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=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
@ -136,8 +140,11 @@ google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlba
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=
google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

View File

@ -1,12 +1,13 @@
// Package urlencode provides a urlencode codec
package urlencode // import "go.unistack.org/micro-codec-urlencode/v3"
package urlencode
import (
"io"
"encoding/json"
pb "go.unistack.org/micro-proto/v3/codec"
"go.unistack.org/micro/v3/codec"
rutil "go.unistack.org/micro/v3/util/reflect"
"google.golang.org/protobuf/types/known/structpb"
)
type urlencodeCodec struct {
@ -15,10 +16,6 @@ type urlencodeCodec struct {
var _ codec.Codec = &urlencodeCodec{}
const (
flattenTag = "flatten"
)
func (c *urlencodeCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error) {
if v == nil {
return nil, nil
@ -28,8 +25,11 @@ func (c *urlencodeCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, e
for _, o := range opts {
o(&options)
}
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, flattenTag); nerr == nil {
v = nv
if options.Flatten {
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, "flatten"); nerr == nil {
v = nv
}
}
switch m := v.(type) {
@ -57,8 +57,10 @@ func (c *urlencodeCodec) Unmarshal(b []byte, v interface{}, opts ...codec.Option
o(&options)
}
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, flattenTag); nerr == nil {
v = nv
if options.Flatten {
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, "flatten"); nerr == nil {
v = nv
}
}
switch m := v.(type) {
@ -75,42 +77,19 @@ func (c *urlencodeCodec) Unmarshal(b []byte, v interface{}, opts ...codec.Option
return err
}
switch t := v.(type) {
case *structpb.Value:
buf, err := json.Marshal(mp)
if err == nil {
err = t.UnmarshalJSON(buf)
}
return err
}
return rutil.Merge(v, rutil.FlattenMap(mp), rutil.Tags([]string{"protobuf", "json", "xml", "yaml"}), rutil.SliceAppend(true))
}
func (c *urlencodeCodec) ReadHeader(conn io.Reader, m *codec.Message, t codec.MessageType) error {
return nil
}
func (c *urlencodeCodec) 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
}
return c.Unmarshal(buf, v)
}
func (c *urlencodeCodec) 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 *urlencodeCodec) String() string {
return "urlencode"
}