2021-04-16 17:32:06 +03:00
|
|
|
// Package urlencode provides a urlencode codec
|
|
|
|
package urlencode
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/unistack-org/micro/v3/codec"
|
|
|
|
rutil "github.com/unistack-org/micro/v3/util/reflect"
|
|
|
|
)
|
|
|
|
|
|
|
|
type urlencodeCodec struct{}
|
|
|
|
|
2021-05-26 01:11:47 +03:00
|
|
|
const (
|
|
|
|
flattenTag = "flatten"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (c *urlencodeCodec) Marshal(v interface{}) ([]byte, error) {
|
|
|
|
if v == nil {
|
2021-04-16 17:32:06 +03:00
|
|
|
return nil, nil
|
2021-05-26 01:11:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if m, ok := v.(*codec.Frame); ok {
|
2021-04-16 17:32:06 +03:00
|
|
|
return m.Data, nil
|
|
|
|
}
|
|
|
|
|
2021-05-26 01:11:47 +03:00
|
|
|
if nv, nerr := rutil.StructFieldByTag(v, codec.DefaultTagName, flattenTag); nerr == nil {
|
|
|
|
v = nv
|
|
|
|
}
|
|
|
|
|
|
|
|
uv, err := rutil.StructURLValues(v, "", []string{"protobuf", "json"})
|
2021-04-16 17:32:06 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-05-26 01:11:47 +03:00
|
|
|
return []byte(uv.Encode()), nil
|
2021-04-16 17:32:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *urlencodeCodec) Unmarshal(b []byte, v interface{}) error {
|
2021-05-26 01:11:47 +03:00
|
|
|
if len(b) == 0 || v == nil {
|
2021-04-16 17:32:06 +03:00
|
|
|
return nil
|
|
|
|
}
|
2021-05-26 01:11:47 +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-05-26 01:11:47 +03:00
|
|
|
if nv, nerr := rutil.StructFieldByTag(v, codec.DefaultTagName, flattenTag); nerr == nil {
|
|
|
|
v = nv
|
|
|
|
}
|
|
|
|
|
2021-04-16 17:32:06 +03:00
|
|
|
return rutil.Merge(v, rutil.FlattenMap(mp), rutil.Tags([]string{"protobuf", "json"}), rutil.SliceAppend(true))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *urlencodeCodec) ReadHeader(conn io.Reader, m *codec.Message, t codec.MessageType) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-26 01:11:47 +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:47 +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:47 +03:00
|
|
|
return c.Unmarshal(buf, v)
|
2021-04-16 17:32:06 +03:00
|
|
|
}
|
|
|
|
|
2021-05-26 01:11:47 +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:47 +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:47 +03:00
|
|
|
return "urlencode"
|
2021-04-16 17:32:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewCodec() codec.Codec {
|
|
|
|
return &urlencodeCodec{}
|
|
|
|
}
|