2 Commits

Author SHA1 Message Date
e37ddb00df add options
Some checks failed
coverage / build (push) Successful in 1m18s
test / test (push) Failing after 15m38s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2025-05-13 19:53:58 +03:00
vtolstov
c3e541a87e Apply Code Coverage Badge 2025-05-13 15:24:08 +00:00
2 changed files with 39 additions and 2 deletions

View File

@@ -1,2 +1,2 @@
# micro-codec-xml
![Coverage](https://img.shields.io/badge/Coverage-26.5%25-red)
![Coverage](https://img.shields.io/badge/Coverage-20.0%25-red)

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 {