2021-04-16 17:32:06 +03:00
|
|
|
// Package urlencode provides a urlencode codec
|
|
|
|
package urlencode
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
|
2021-10-25 20:27:26 +03:00
|
|
|
"go.unistack.org/micro/v3/codec"
|
|
|
|
rutil "go.unistack.org/micro/v3/util/reflect"
|
2021-04-16 17:32:06 +03:00
|
|
|
)
|
|
|
|
|
2021-09-24 00:20:43 +03:00
|
|
|
type urlencodeCodec struct {
|
|
|
|
opts codec.Options
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ codec.Codec = &urlencodeCodec{}
|
2021-04-16 17:32:06 +03:00
|
|
|
|
2021-05-26 01:11:08 +03:00
|
|
|
const (
|
|
|
|
flattenTag = "flatten"
|
|
|
|
)
|
|
|
|
|
2021-09-24 00:20:43 +03:00
|
|
|
func (c *urlencodeCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error) {
|
2021-05-26 01:11:08 +03:00
|
|
|
if v == nil {
|
2021-04-16 17:32:06 +03:00
|
|
|
return nil, nil
|
2021-05-26 01:11:08 +03:00
|
|
|
}
|
|
|
|
|
2021-09-24 00:20:43 +03:00
|
|
|
options := c.opts
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
2021-04-16 17:32:06 +03:00
|
|
|
}
|
2021-09-24 00:20:43 +03:00
|
|
|
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, flattenTag); nerr == nil {
|
2021-05-26 01:11:08 +03:00
|
|
|
v = nv
|
|
|
|
}
|
|
|
|
|
2021-09-24 00:20:43 +03:00
|
|
|
if m, ok := v.(*codec.Frame); ok {
|
|
|
|
return m.Data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
uv, err := rutil.StructURLValues(v, "", []string{"protobuf", "json", "xml", "yaml"})
|
2021-04-16 17:32:06 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-05-26 01:11:08 +03:00
|
|
|
return []byte(uv.Encode()), nil
|
2021-04-16 17:32:06 +03:00
|
|
|
}
|
|
|
|
|
2021-09-24 00:20:43 +03:00
|
|
|
func (c *urlencodeCodec) Unmarshal(b []byte, v interface{}, opts ...codec.Option) error {
|
2021-05-26 01:11:08 +03:00
|
|
|
if len(b) == 0 || v == nil {
|
2021-04-16 17:32:06 +03:00
|
|
|
return nil
|
|
|
|
}
|
2021-05-26 01:11:08 +03:00
|
|
|
|
2021-09-24 00:20:43 +03:00
|
|
|
options := c.opts
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, flattenTag); nerr == nil {
|
|
|
|
v = nv
|
|
|
|
}
|
|
|
|
|
2021-05-26 01:11:08 +03:00
|
|
|
if m, ok := v.(*codec.Frame); ok {
|
2021-04-16 17:32:06 +03:00
|
|
|
m.Data = b
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
mp, err := rutil.URLMap(string(b))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-24 00:20:43 +03:00
|
|
|
return rutil.Merge(v, rutil.FlattenMap(mp), rutil.Tags([]string{"protobuf", "json", "xml", "yaml"}), rutil.SliceAppend(true))
|
2021-04-16 17:32:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *urlencodeCodec) ReadHeader(conn io.Reader, m *codec.Message, t codec.MessageType) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-26 01:11:08 +03:00
|
|
|
func (c *urlencodeCodec) ReadBody(conn io.Reader, v interface{}) error {
|
|
|
|
if v == nil {
|
2021-04-16 17:32:06 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-26 01:11:08 +03:00
|
|
|
buf, err := io.ReadAll(conn)
|
2021-04-16 17:32:06 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if len(buf) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-26 01:11:08 +03:00
|
|
|
return c.Unmarshal(buf, v)
|
2021-04-16 17:32:06 +03:00
|
|
|
}
|
|
|
|
|
2021-05-26 01:11:08 +03:00
|
|
|
func (c *urlencodeCodec) Write(conn io.Writer, m *codec.Message, v interface{}) error {
|
|
|
|
if v == nil {
|
2021-04-16 17:32:06 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-26 01:11:08 +03:00
|
|
|
buf, err := c.Marshal(v)
|
2021-04-16 17:32:06 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = conn.Write(buf)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *urlencodeCodec) String() string {
|
2021-05-26 01:11:08 +03:00
|
|
|
return "urlencode"
|
2021-04-16 17:32:06 +03:00
|
|
|
}
|
|
|
|
|
2021-09-24 00:20:43 +03:00
|
|
|
func NewCodec(opts ...codec.Option) *urlencodeCodec {
|
|
|
|
return &urlencodeCodec{opts: codec.NewOptions(opts...)}
|
2021-04-16 17:32:06 +03:00
|
|
|
}
|