Skip to content

Commit

Permalink
Fixed xml comment in array of scalars #1465
Browse files Browse the repository at this point in the history
  • Loading branch information
mikefarah committed Dec 16, 2022
1 parent 481ea64 commit 48d00f8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
14 changes: 10 additions & 4 deletions pkg/yqlib/decoder_xml.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (dec *xmlDecoder) createMap(n *xmlNode) (*yaml.Node, error) {
yamlNode := &yaml.Node{Kind: yaml.MappingNode, Tag: "!!map"}

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
labelNode := createScalarNode(label, label)
labelNode.HeadComment = dec.processComment(n.HeadComment)
Expand All @@ -78,7 +78,7 @@ func (dec *xmlDecoder) createMap(n *xmlNode) (*yaml.Node, error) {
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)

log.Debug("len of children in %v is %v", label, len(children))
Expand Down Expand Up @@ -337,8 +337,14 @@ func applyFootComment(elem *element, commentStr string) {
if len(elem.n.Children) > 0 {
lastChildIndex := len(elem.n.Children) - 1
childKv := elem.n.Children[lastChildIndex]
log.Debug("got a foot comment for %v: [%v]", childKv.K, commentStr)
childKv.FootComment = joinComments([]string{elem.n.FootComment, 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}, " ")
}
} else {
log.Debug("got a foot comment for %v: [%v]", elem.label, commentStr)
elem.n.FootComment = joinComments([]string{elem.n.FootComment, commentStr}, " ")
Expand Down
5 changes: 5 additions & 0 deletions pkg/yqlib/xml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,11 @@ var xmlScenarios = []formatScenario{
input: "<root>value<!-- comment-->anotherValue </root>",
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",
subdescription: "Notice how all the values are strings, see the next example on how you can fix that.",
Expand Down

0 comments on commit 48d00f8

Please sign in to comment.