Skip to content

Commit

Permalink
Fix an issue with XML encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
TomWright committed Aug 30, 2023
1 parent 31b6a92 commit 501ce1a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 21 deletions.
11 changes: 9 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Nothing yet

## [v2.3.5] - 20230-08-29
## [v2.3.6] - 2023-08-30

### Fixed

- XML is now formatted correctly. (https://github.com/TomWright/dasel/issues/354)

## [v2.3.5] - 2023-08-29

### Changed

Expand Down Expand Up @@ -607,7 +613,8 @@ See [documentation](https://daseldocs.tomwright.me) for all changes.

- Everything!

[unreleased]: https://github.com/TomWright/dasel/compare/v2.3.5...HEAD
[unreleased]: https://github.com/TomWright/dasel/compare/v2.3.6...HEAD
[v2.3.6]: https://github.com/TomWright/dasel/compare/v2.3.5...v2.3.6
[v2.3.5]: https://github.com/TomWright/dasel/compare/v2.3.4...v2.3.5
[v2.3.4]: https://github.com/TomWright/dasel/compare/v2.3.3...v2.3.4
[v2.3.3]: https://github.com/TomWright/dasel/compare/v2.3.2...v2.3.3
Expand Down
52 changes: 33 additions & 19 deletions storage/xml.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"github.com/tomwright/dasel/v2"
"github.com/tomwright/dasel/v2/dencoding"
"strings"

"github.com/clbanning/mxj/v2"
Expand Down Expand Up @@ -66,28 +67,36 @@ func (p *XMLParser) ToBytes(value dasel.Value, options ...ReadWriteOption) ([]by
}

writeMap := func(val interface{}) error {
if m, ok := val.(map[string]interface{}); ok {
mv := mxj.New()
for k, v := range m {
mv[k] = v
}
var m map[string]interface{}

switch v := val.(type) {
case *dencoding.Map:
m = v.UnorderedData()
case map[string]any:
m = v
default:
_, err := buf.Write([]byte(fmt.Sprintf("%v\n", val)))
return err
}

var byteData []byte
var err error
if prettyPrint {
byteData, err = mv.XmlIndent("", indent)
} else {
byteData, err = mv.Xml()
}
mv := mxj.New()
for k, v := range m {
mv[k] = v
}

if err != nil {
return err
}
buf.Write(byteData)
buf.Write([]byte("\n"))
return nil
var byteData []byte
var err error
if prettyPrint {
byteData, err = mv.XmlIndent("", indent)
} else {
byteData, err = mv.Xml()
}
buf.Write([]byte(fmt.Sprintf("%v\n", val)))

if err != nil {
return err
}
buf.Write(byteData)
buf.Write([]byte("\n"))
return nil
}

Expand All @@ -102,6 +111,11 @@ func (p *XMLParser) ToBytes(value dasel.Value, options ...ReadWriteOption) ([]by
return nil, err
}
}
case value.IsDencodingMap():
dm := value.Interface().(*dencoding.Map)
if err := writeMap(dm.UnorderedData()); err != nil {
return nil, err
}
default:
if err := writeMap(value.Interface()); err != nil {
return nil, err
Expand Down

0 comments on commit 501ce1a

Please sign in to comment.