add options

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
2025-05-13 19:51:26 +03:00
parent c3e541a87e
commit 09db4e10b0

39
xml.go
View File

@@ -21,6 +21,18 @@ const (
flattenTag = "flatten"
)
type unmarshalStrictKey struct{}
func UnmarshalStrict(b bool) codec.Option {
return codec.SetOption(unmarshalStrictKey{}, b)
}
type unmarshalXML11Key struct{}
func UnmarshalXML11(b bool) codec.Option {
return codec.SetOption(unmarshalXML11Key{}, b)
}
func (c *xmlCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error) {
if v == nil {
return nil, nil
@@ -82,7 +94,32 @@ func (c *xmlCodec) Unmarshal(b []byte, v interface{}, opts ...codec.Option) erro
return nil
}
return xml.NewDecoder(newReader(b)).Decode(v)
var unmarshalStrict bool
var unmarshalXML11 bool
if options.Context != nil {
if v, ok := options.Context.Value(unmarshalStrictKey{}).(bool); ok {
unmarshalStrict = v
}
if v, ok := options.Context.Value(unmarshalXML11Key{}).(bool); ok {
unmarshalXML11 = v
}
}
var r io.Reader
if unmarshalXML11 {
r = newReader(b)
} else {
r = bytes.NewReader(b)
}
d := xml.NewDecoder(r)
if unmarshalStrict {
d.Strict = true
}
return d.Decode(v)
}
type reader struct {