Skip to content

Commit 77998d1

Browse files
committed
XML Decoder: Fixed processing comments in empty XML #1446
1 parent 68f47c0 commit 77998d1

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

pkg/yqlib/decoder_xml.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ func (dec *xmlDecoder) createMap(n *xmlNode) (*yaml.Node, error) {
5858
yamlNode := &yaml.Node{Kind: yaml.MappingNode, Tag: "!!map"}
5959

6060
if len(n.Data) > 0 {
61+
log.Debug("creating content node for map")
6162
label := dec.prefs.ContentName
6263
labelNode := createScalarNode(label, label)
6364
labelNode.HeadComment = dec.processComment(n.HeadComment)
@@ -87,13 +88,21 @@ func (dec *xmlDecoder) createMap(n *xmlNode) (*yaml.Node, error) {
8788
return nil, err
8889
}
8990
} else {
91+
log.Debug("before hack, this is the data len: %", len(children[0].Data))
9092
// comment hack for maps of scalars
9193
// if the value is a scalar, the head comment of the scalar needs to go on the key?
9294
// add tests for <z/> as well as multiple <ds> of inputXmlWithComments > yaml
9395
if len(children[0].Children) == 0 && children[0].HeadComment != "" {
94-
log.Debug("scalar comment hack")
95-
labelNode.HeadComment = labelNode.HeadComment + "\n" + strings.TrimSpace(children[0].HeadComment)
96-
children[0].HeadComment = ""
96+
if len(children[0].Data) > 0 {
97+
98+
log.Debug("scalar comment hack")
99+
labelNode.HeadComment = labelNode.HeadComment + "\n" + strings.TrimSpace(children[0].HeadComment)
100+
children[0].HeadComment = ""
101+
} else {
102+
// child is null, put the headComment as a linecomment for reasons
103+
children[0].LineComment = children[0].HeadComment
104+
children[0].HeadComment = ""
105+
}
97106
}
98107
valueNode, err = dec.convertToYamlNode(children[0])
99108
if err != nil {

pkg/yqlib/xml_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,11 @@ var xmlScenarios = []formatScenario{
249249
input: "<root> <!-- comment-->value</root>",
250250
expected: "\n# comment\nroot: value\n", //needs fix
251251
},
252+
{
253+
skipDoc: true,
254+
input: "<root> <!-- comment--></root>",
255+
expected: "root: # comment\n",
256+
},
252257
{
253258
skipDoc: true,
254259
input: "<root>value<!-- comment-->anotherValue </root>",

0 commit comments

Comments
 (0)