2021-04-16 17:32:06 +03:00
|
|
|
// Package urlencode provides a urlencode codec
|
2024-02-05 13:39:32 +03:00
|
|
|
package urlencode
|
2021-04-16 17:32:06 +03:00
|
|
|
|
|
|
|
import (
|
2024-02-05 13:39:32 +03:00
|
|
|
"encoding/json"
|
2021-04-16 17:32:06 +03:00
|
|
|
"io"
|
|
|
|
|
2023-03-05 23:44:32 +03:00
|
|
|
pb "go.unistack.org/micro-proto/v3/codec"
|
2021-10-25 20:27:26 +03:00
|
|
|
"go.unistack.org/micro/v3/codec"
|
|
|
|
rutil "go.unistack.org/micro/v3/util/reflect"
|
2024-02-05 13:39:32 +03:00
|
|
|
"google.golang.org/protobuf/types/known/structpb"
|
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
|
|
|
|
}
|
|
|
|
|
2023-03-05 23:44:32 +03:00
|
|
|
switch m := v.(type) {
|
|
|
|
case *codec.Frame:
|
|
|
|
return m.Data, nil
|
|
|
|
case *pb.Frame:
|
2021-09-24 00:20:43 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-03-05 23:44:32 +03:00
|
|
|
switch m := v.(type) {
|
|
|
|
case *codec.Frame:
|
|
|
|
m.Data = b
|
|
|
|
return nil
|
|
|
|
case *pb.Frame:
|
2021-04-16 17:32:06 +03:00
|
|
|
m.Data = b
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
mp, err := rutil.URLMap(string(b))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-05 13:39:32 +03:00
|
|
|
switch t := v.(type) {
|
|
|
|
case *structpb.Value:
|
|
|
|
|
|
|
|
buf, err := json.Marshal(mp)
|
|
|
|
if err == nil {
|
|
|
|
err = t.UnmarshalJSON(buf)
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|