Fixed xml comment in array of scalars #1465

This commit is contained in:
Mike Farah 2022-12-17 10:27:07 +11:00
parent 481ea64ccc
commit 48d00f807e
2 changed files with 15 additions and 4 deletions

View File

@ -58,7 +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") log.Debugf("creating content node for map: %v", dec.prefs.ContentName)
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)
@ -78,7 +78,7 @@ func (dec *xmlDecoder) createMap(n *xmlNode) (*yaml.Node, error) {
labelNode.HeadComment = dec.processComment(n.HeadComment) labelNode.HeadComment = dec.processComment(n.HeadComment)
} }
log.Debugf("label=%v, i=%v, keyValuePair.FootComment: %v", label, i, keyValuePair.FootComment)
labelNode.FootComment = dec.processComment(keyValuePair.FootComment) labelNode.FootComment = dec.processComment(keyValuePair.FootComment)
log.Debug("len of children in %v is %v", label, len(children)) log.Debug("len of children in %v is %v", label, len(children))
@ -337,8 +337,14 @@ func applyFootComment(elem *element, commentStr string) {
if len(elem.n.Children) > 0 { if len(elem.n.Children) > 0 {
lastChildIndex := len(elem.n.Children) - 1 lastChildIndex := len(elem.n.Children) - 1
childKv := elem.n.Children[lastChildIndex] childKv := elem.n.Children[lastChildIndex]
log.Debug("got a foot comment for %v: [%v]", childKv.K, commentStr) log.Debug("got a foot comment, putting on last child for %v: [%v]", childKv.K, commentStr)
// if it's an array of scalars, put the foot comment on the scalar itself
if len(childKv.V) > 0 && len(childKv.V[0].Children) == 0 {
nodeToUpdate := childKv.V[len(childKv.V)-1]
nodeToUpdate.FootComment = joinComments([]string{nodeToUpdate.FootComment, commentStr}, " ")
} else {
childKv.FootComment = joinComments([]string{elem.n.FootComment, commentStr}, " ") childKv.FootComment = joinComments([]string{elem.n.FootComment, commentStr}, " ")
}
} else { } else {
log.Debug("got a foot comment for %v: [%v]", elem.label, commentStr) log.Debug("got a foot comment for %v: [%v]", elem.label, commentStr)
elem.n.FootComment = joinComments([]string{elem.n.FootComment, commentStr}, " ") elem.n.FootComment = joinComments([]string{elem.n.FootComment, commentStr}, " ")

View File

@ -259,6 +259,11 @@ var xmlScenarios = []formatScenario{
input: "<root>value<!-- comment-->anotherValue </root>", input: "<root>value<!-- comment-->anotherValue </root>",
expected: "root:\n # comment\n - value\n - anotherValue\n", expected: "root:\n # comment\n - value\n - anotherValue\n",
}, },
{
skipDoc: true,
input: "<root><cats><cat>quick</cat><cat>soft</cat><!-- kitty_comment--><cat>squishy</cat></cats></root>",
expected: "root:\n cats:\n cat:\n - quick\n - soft\n # kitty_comment\n\n - squishy\n",
},
{ {
description: "Parse xml: simple", description: "Parse xml: simple",
subdescription: "Notice how all the values are strings, see the next example on how you can fix that.", subdescription: "Notice how all the values are strings, see the next example on how you can fix that.",