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"
|
|
|
|
|
2023-03-05 23:46:31 +03:00
|
|
|
pb "go.unistack.org/micro-proto/v3/codec"
|
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)
|
|
|
|
}
|
2024-09-17 12:18:41 +03:00
|
|
|
|
|
|
|
if options.Flatten {
|
|
|
|
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, flattenTag); nerr == nil {
|
|
|
|
v = nv
|
|
|
|
}
|
2021-05-26 01:16:52 +03:00
|
|
|
}
|
|
|
|
|
2023-03-05 23:46:31 +03:00
|
|
|
switch m := v.(type) {
|
|
|
|
case *codec.Frame:
|
|
|
|
return m.Data, nil
|
|
|
|
case *pb.Frame:
|
2021-09-23 17:39:34 +03:00
|
|
|
return m.Data, nil
|
2024-12-23 20:57:22 +03:00
|
|
|
case codec.RawMessage:
|
|
|
|
return []byte(m), nil
|
|
|
|
case *codec.RawMessage:
|
|
|
|
return []byte(*m), nil
|
2021-09-23 17:39:34 +03:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-09-17 12:18:41 +03:00
|
|
|
if options.Flatten {
|
|
|
|
if nv, nerr := rutil.StructFieldByTag(v, options.TagName, flattenTag); nerr == nil {
|
|
|
|
v = nv
|
|
|
|
}
|
2021-05-26 01:16:52 +03:00
|
|
|
}
|
|
|
|
|
2023-03-05 23:46:31 +03:00
|
|
|
switch m := v.(type) {
|
|
|
|
case *codec.Frame:
|
|
|
|
m.Data = b
|
|
|
|
return nil
|
|
|
|
case *pb.Frame:
|
2021-09-23 17:39:34 +03:00
|
|
|
m.Data = b
|
|
|
|
return nil
|
2024-12-23 20:57:22 +03:00
|
|
|
case *codec.RawMessage:
|
|
|
|
*m = append((*m)[0:0], b...)
|
|
|
|
return nil
|
|
|
|
case codec.RawMessage:
|
|
|
|
copy(m, b)
|
|
|
|
return nil
|
2021-09-23 17:39:34 +03:00
|
|
|
}
|
|
|
|
|
2020-12-01 00:07:38 +03:00
|
|
|
return xml.Unmarshal(b, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|