2020-12-01 00:07:38 +03:00
|
|
|
// Package xml provides a xml codec
|
2021-10-25 20:41:38 +03:00
|
|
|
package xml // import "go.unistack.org/micro-codec-xml/v3"
|
2020-12-01 00:07:38 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
|
|
|
"io"
|
|
|
|
|
2021-10-25 20:28:35 +03:00
|
|
|
"go.unistack.org/micro/v3/codec"
|
|
|
|
rutil "go.unistack.org/micro/v3/util/reflect"
|
2020-12-01 00:07:38 +03:00
|
|
|
)
|
|
|
|
|
2021-09-23 06:39:12 +03:00
|
|
|
var _ codec.Codec = &xmlCodec{}
|
|
|
|
|
|
|
|
type xmlCodec struct {
|
|
|
|
opts codec.Options
|
|
|
|
}
|
2020-12-01 00:07:38 +03:00
|
|
|
|
2021-05-26 01:16:52 +03:00
|
|
|
const (
|
|
|
|
flattenTag = "flatten"
|
|
|
|
)
|
|
|
|
|
2021-09-23 06:39:12 +03:00
|
|
|
func (c *xmlCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error) {
|
2021-09-23 17:39:34 +03:00
|
|
|
if v == nil {
|
2020-12-01 00:07:38 +03:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2021-09-23 06:39:12 +03:00
|
|
|
options := c.opts
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, flattenTag); nerr == nil {
|
2021-05-26 01:16:52 +03:00
|
|
|
v = nv
|
|
|
|
}
|
|
|
|
|
2021-09-23 17:39:34 +03:00
|
|
|
if m, ok := v.(*codec.Frame); ok {
|
|
|
|
return m.Data, nil
|
|
|
|
}
|
|
|
|
|
2021-05-26 01:16:52 +03:00
|
|
|
return xml.Marshal(v)
|
2020-12-01 00:07:38 +03:00
|
|
|
}
|
|
|
|
|
2021-09-23 06:39:12 +03:00
|
|
|
func (c *xmlCodec) Unmarshal(b []byte, v interface{}, opts ...codec.Option) error {
|
2021-05-26 01:16:52 +03:00
|
|
|
if len(b) == 0 || v == nil {
|
2020-12-01 00:07:38 +03:00
|
|
|
return nil
|
|
|
|
}
|
2021-05-26 01:16:52 +03:00
|
|
|
|
2021-09-23 06:39:12 +03:00
|
|
|
options := c.opts
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, flattenTag); nerr == nil {
|
2021-05-26 01:16:52 +03:00
|
|
|
v = nv
|
|
|
|
}
|
|
|
|
|
2021-09-23 17:39:34 +03:00
|
|
|
if m, ok := v.(*codec.Frame); ok {
|
|
|
|
m.Data = b
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-01 00:07:38 +03:00
|
|
|
return xml.Unmarshal(b, v)
|
|
|
|
}
|
|
|
|
|
2020-12-21 00:01:47 +03:00
|
|
|
func (c *xmlCodec) ReadHeader(conn io.Reader, m *codec.Message, t codec.MessageType) error {
|
2020-12-01 00:07:38 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-26 01:16:52 +03:00
|
|
|
func (c *xmlCodec) ReadBody(conn io.Reader, v interface{}) error {
|
|
|
|
if v == nil {
|
2020-12-01 00:07:38 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-26 01:16:52 +03:00
|
|
|
buf, err := io.ReadAll(conn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if len(buf) == 0 {
|
2021-04-12 16:45:56 +03:00
|
|
|
return nil
|
|
|
|
}
|
2021-05-26 01:16:52 +03:00
|
|
|
|
|
|
|
return c.Unmarshal(buf, v)
|
2020-12-01 00:07:38 +03:00
|
|
|
}
|
|
|
|
|
2021-05-26 01:16:52 +03:00
|
|
|
func (c *xmlCodec) Write(conn io.Writer, m *codec.Message, v interface{}) error {
|
|
|
|
if v == nil {
|
2020-12-01 00:07:38 +03:00
|
|
|
return nil
|
2021-05-26 01:16:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
buf, err := c.Marshal(v)
|
|
|
|
if err != nil {
|
2020-12-01 00:07:38 +03:00
|
|
|
return err
|
2021-05-26 01:16:52 +03:00
|
|
|
} else if len(buf) == 0 {
|
|
|
|
return codec.ErrInvalidMessage
|
2020-12-01 00:07:38 +03:00
|
|
|
}
|
|
|
|
|
2021-05-26 01:16:52 +03:00
|
|
|
_, err = conn.Write(buf)
|
|
|
|
return err
|
2020-12-01 00:07:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *xmlCodec) String() string {
|
|
|
|
return "xml"
|
|
|
|
}
|
|
|
|
|
2021-09-23 06:39:12 +03:00
|
|
|
func NewCodec(opts ...codec.Option) *xmlCodec {
|
|
|
|
return &xmlCodec{opts: codec.NewOptions(opts...)}
|
2020-12-01 00:07:38 +03:00
|
|
|
}
|