Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
3863e4d8d3 | |||
3df8637fd4 | |||
cbf0a52cb9 | |||
|
f9a7145177 | ||
0b6698d75f | |||
100a44103e |
@ -4,7 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"go.unistack.org/micro/v4/codec"
|
"go.unistack.org/micro/v3/codec"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestFrame(t *testing.T) {
|
func TestFrame(t *testing.T) {
|
||||||
@ -28,7 +28,7 @@ func TestFrameFlatten(t *testing.T) {
|
|||||||
Name: &codec.Frame{Data: []byte("test")},
|
Name: &codec.Frame{Data: []byte("test")},
|
||||||
}
|
}
|
||||||
|
|
||||||
buf, err := NewCodec().Marshal(s)
|
buf, err := NewCodec(codec.Flatten(true)).Marshal(s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -36,15 +36,3 @@ func TestFrameFlatten(t *testing.T) {
|
|||||||
t.Fatalf("bytes not equal %s != %s", buf, `test`)
|
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
14
go.mod
14
go.mod
@ -1,9 +1,13 @@
|
|||||||
module go.unistack.org/micro-codec-urlencode/v4
|
module go.unistack.org/micro-codec-urlencode/v3
|
||||||
|
|
||||||
go 1.20
|
go 1.21
|
||||||
|
|
||||||
|
toolchain go1.23.4
|
||||||
|
|
||||||
require (
|
require (
|
||||||
go.unistack.org/micro-proto/v4 v4.0.1
|
go.unistack.org/micro-proto/v3 v3.4.1
|
||||||
go.unistack.org/micro/v4 v4.0.1
|
go.unistack.org/micro/v3 v3.10.88
|
||||||
google.golang.org/protobuf v1.32.0
|
google.golang.org/protobuf v1.35.2
|
||||||
)
|
)
|
||||||
|
|
||||||
|
require github.com/google/go-cmp v0.6.0 // indirect
|
||||||
|
16
go.sum
16
go.sum
@ -1,8 +1,8 @@
|
|||||||
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
|
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||||
go.unistack.org/micro-proto/v4 v4.0.1 h1:2RKHgtCOOcAFgKsnngGK5bqM/6MWXOjVCdw03dbuoF8=
|
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||||
go.unistack.org/micro-proto/v4 v4.0.1/go.mod h1:ArmK7o+uFvxSY3dbJhKBBX4Pm1rhWdLEFf3LxBrMtec=
|
go.unistack.org/micro-proto/v3 v3.4.1 h1:UTjLSRz2YZuaHk9iSlVqqsA50JQNAEK2ZFboGqtEa9Q=
|
||||||
go.unistack.org/micro/v4 v4.0.1 h1:xo1IxbVfgh8i0eY0VeYa3cbb13u5n/Mxnp3FOgWD4Jo=
|
go.unistack.org/micro-proto/v3 v3.4.1/go.mod h1:okx/cnOhzuCX0ggl/vToatbCupi0O44diiiLLsZ93Zo=
|
||||||
go.unistack.org/micro/v4 v4.0.1/go.mod h1:p/J5UcSJjfHsWGT31uKoghQ5rUQZzQJBAFy+Z4+ZVMs=
|
go.unistack.org/micro/v3 v3.10.88 h1:MxlzP+77Y6Kphb3lzHxROL4XfE/WdCQMQpnPv4D9Z8U=
|
||||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
|
go.unistack.org/micro/v3 v3.10.88/go.mod h1:erMgt3Bl7vQQ0e9UpQyR5NlLiZ9pKeEJ9+1tfYFaqUg=
|
||||||
google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=
|
google.golang.org/protobuf v1.35.2 h1:8Ar7bF+apOIoThw1EdZl0p1oWvMqTHmpA2fRTyZO8io=
|
||||||
google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
|
google.golang.org/protobuf v1.35.2/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
|
||||||
|
63
urlencode.go
63
urlencode.go
@ -3,11 +3,10 @@ package urlencode
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io"
|
|
||||||
|
|
||||||
pb "go.unistack.org/micro-proto/v4/codec"
|
pb "go.unistack.org/micro-proto/v3/codec"
|
||||||
"go.unistack.org/micro/v4/codec"
|
"go.unistack.org/micro/v3/codec"
|
||||||
rutil "go.unistack.org/micro/v4/util/reflect"
|
rutil "go.unistack.org/micro/v3/util/reflect"
|
||||||
"google.golang.org/protobuf/types/known/structpb"
|
"google.golang.org/protobuf/types/known/structpb"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -17,10 +16,6 @@ type urlencodeCodec struct {
|
|||||||
|
|
||||||
var _ codec.Codec = &urlencodeCodec{}
|
var _ codec.Codec = &urlencodeCodec{}
|
||||||
|
|
||||||
const (
|
|
||||||
flattenTag = "flatten"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (c *urlencodeCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error) {
|
func (c *urlencodeCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error) {
|
||||||
if v == nil {
|
if v == nil {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
@ -30,15 +25,22 @@ func (c *urlencodeCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, e
|
|||||||
for _, o := range opts {
|
for _, o := range opts {
|
||||||
o(&options)
|
o(&options)
|
||||||
}
|
}
|
||||||
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, flattenTag); nerr == nil {
|
|
||||||
|
if options.Flatten {
|
||||||
|
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, "flatten"); nerr == nil {
|
||||||
v = nv
|
v = nv
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
switch m := v.(type) {
|
switch m := v.(type) {
|
||||||
case *codec.Frame:
|
case *codec.Frame:
|
||||||
return m.Data, nil
|
return m.Data, nil
|
||||||
case *pb.Frame:
|
case *pb.Frame:
|
||||||
return m.Data, nil
|
return m.Data, nil
|
||||||
|
case codec.RawMessage:
|
||||||
|
return []byte(m), nil
|
||||||
|
case *codec.RawMessage:
|
||||||
|
return []byte(*m), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
uv, err := rutil.StructURLValues(v, "", []string{"protobuf", "json", "xml", "yaml"})
|
uv, err := rutil.StructURLValues(v, "", []string{"protobuf", "json", "xml", "yaml"})
|
||||||
@ -59,9 +61,11 @@ func (c *urlencodeCodec) Unmarshal(b []byte, v interface{}, opts ...codec.Option
|
|||||||
o(&options)
|
o(&options)
|
||||||
}
|
}
|
||||||
|
|
||||||
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, flattenTag); nerr == nil {
|
if options.Flatten {
|
||||||
|
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, "flatten"); nerr == nil {
|
||||||
v = nv
|
v = nv
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
switch m := v.(type) {
|
switch m := v.(type) {
|
||||||
case *codec.Frame:
|
case *codec.Frame:
|
||||||
@ -70,6 +74,12 @@ func (c *urlencodeCodec) Unmarshal(b []byte, v interface{}, opts ...codec.Option
|
|||||||
case *pb.Frame:
|
case *pb.Frame:
|
||||||
m.Data = b
|
m.Data = b
|
||||||
return nil
|
return nil
|
||||||
|
case *codec.RawMessage:
|
||||||
|
*m = append((*m)[0:0], b...)
|
||||||
|
return nil
|
||||||
|
case codec.RawMessage:
|
||||||
|
copy(m, b)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
mp, err := rutil.URLMap(string(b))
|
mp, err := rutil.URLMap(string(b))
|
||||||
@ -90,39 +100,6 @@ func (c *urlencodeCodec) Unmarshal(b []byte, v interface{}, opts ...codec.Option
|
|||||||
return rutil.Merge(v, rutil.FlattenMap(mp), rutil.Tags([]string{"protobuf", "json", "xml", "yaml"}), rutil.SliceAppend(true))
|
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 {
|
func (c *urlencodeCodec) String() string {
|
||||||
return "urlencode"
|
return "urlencode"
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user