mirror of
https://github.com/mikefarah/yq.git
synced 2025-01-27 08:55:37 +00:00
XML Decoder: Fixed processing comments in empty XML #1446
This commit is contained in:
parent
68f47c02c8
commit
77998d1bb3
@ -58,6 +58,7 @@ func (dec *xmlDecoder) createMap(n *xmlNode) (*yaml.Node, error) {
|
|||||||
yamlNode := &yaml.Node{Kind: yaml.MappingNode, Tag: "!!map"}
|
yamlNode := &yaml.Node{Kind: yaml.MappingNode, Tag: "!!map"}
|
||||||
|
|
||||||
if len(n.Data) > 0 {
|
if len(n.Data) > 0 {
|
||||||
|
log.Debug("creating content node for map")
|
||||||
label := dec.prefs.ContentName
|
label := dec.prefs.ContentName
|
||||||
labelNode := createScalarNode(label, label)
|
labelNode := createScalarNode(label, label)
|
||||||
labelNode.HeadComment = dec.processComment(n.HeadComment)
|
labelNode.HeadComment = dec.processComment(n.HeadComment)
|
||||||
@ -87,13 +88,21 @@ func (dec *xmlDecoder) createMap(n *xmlNode) (*yaml.Node, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
log.Debug("before hack, this is the data len: %", len(children[0].Data))
|
||||||
// comment hack for maps of scalars
|
// comment hack for maps of scalars
|
||||||
// if the value is a scalar, the head comment of the scalar needs to go on the key?
|
// if the value is a scalar, the head comment of the scalar needs to go on the key?
|
||||||
// add tests for <z/> as well as multiple <ds> of inputXmlWithComments > yaml
|
// add tests for <z/> as well as multiple <ds> of inputXmlWithComments > yaml
|
||||||
if len(children[0].Children) == 0 && children[0].HeadComment != "" {
|
if len(children[0].Children) == 0 && children[0].HeadComment != "" {
|
||||||
log.Debug("scalar comment hack")
|
if len(children[0].Data) > 0 {
|
||||||
labelNode.HeadComment = labelNode.HeadComment + "\n" + strings.TrimSpace(children[0].HeadComment)
|
|
||||||
children[0].HeadComment = ""
|
log.Debug("scalar comment hack")
|
||||||
|
labelNode.HeadComment = labelNode.HeadComment + "\n" + strings.TrimSpace(children[0].HeadComment)
|
||||||
|
children[0].HeadComment = ""
|
||||||
|
} else {
|
||||||
|
// child is null, put the headComment as a linecomment for reasons
|
||||||
|
children[0].LineComment = children[0].HeadComment
|
||||||
|
children[0].HeadComment = ""
|
||||||
|
}
|
||||||
}
|
}
|
||||||
valueNode, err = dec.convertToYamlNode(children[0])
|
valueNode, err = dec.convertToYamlNode(children[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -249,6 +249,11 @@ var xmlScenarios = []formatScenario{
|
|||||||
input: "<root> <!-- comment-->value</root>",
|
input: "<root> <!-- comment-->value</root>",
|
||||||
expected: "\n# comment\nroot: value\n", //needs fix
|
expected: "\n# comment\nroot: value\n", //needs fix
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
skipDoc: true,
|
||||||
|
input: "<root> <!-- comment--></root>",
|
||||||
|
expected: "root: # comment\n",
|
||||||
|
},
|
||||||
{
|
{
|
||||||
skipDoc: true,
|
skipDoc: true,
|
||||||
input: "<root>value<!-- comment-->anotherValue </root>",
|
input: "<root>value<!-- comment-->anotherValue </root>",
|
||||||
|
Loading…
Reference in New Issue
Block a user