Compare commits

..

6 Commits
master ... v3

Author SHA1 Message Date
3863e4d8d3 add codec.RawMessage support
All checks were successful
test / test (push) Successful in 1m46s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-12-23 20:58:57 +03:00
3df8637fd4 add codec.RawMessage support
All checks were successful
test / test (push) Successful in 2m57s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-12-23 20:55:04 +03:00
cbf0a52cb9 Merge pull request 'Update workflows' (#71) from atolstikhin/micro-codec-urlencode:v3 into v3
All checks were successful
test / test (push) Successful in 13m28s
Reviewed-on: #71
2024-12-14 16:43:03 +03:00
Aleksandr Tolstikhin
f9a7145177 Update workflows 2024-12-14 01:02:06 +07:00
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 41 additions and 72 deletions

View File

@ -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
View File

@ -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
View File

@ -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=

View File

@ -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"
} }