Skip to content

Commit 1617e5d

Browse files
committed
XML Decoder: Comment parsing tweak
1 parent 77998d1 commit 1617e5d

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

pkg/yqlib/decoder_xml.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,14 @@ func (dec *xmlDecoder) createMap(n *xmlNode) (*yaml.Node, error) {
8888
return nil, err
8989
}
9090
} else {
91-
log.Debug("before hack, this is the data len: %", len(children[0].Data))
9291
// comment hack for maps of scalars
9392
// if the value is a scalar, the head comment of the scalar needs to go on the key?
9493
// add tests for <z/> as well as multiple <ds> of inputXmlWithComments > yaml
9594
if len(children[0].Children) == 0 && children[0].HeadComment != "" {
9695
if len(children[0].Data) > 0 {
9796

98-
log.Debug("scalar comment hack")
99-
labelNode.HeadComment = labelNode.HeadComment + "\n" + strings.TrimSpace(children[0].HeadComment)
97+
log.Debug("scalar comment hack, currentlabel [%v]", labelNode.HeadComment)
98+
labelNode.HeadComment = joinComments([]string{labelNode.HeadComment, strings.TrimSpace(children[0].HeadComment)}, "\n")
10099
children[0].HeadComment = ""
101100
} else {
102101
// child is null, put the headComment as a linecomment for reasons
@@ -311,10 +310,10 @@ func (dec *xmlDecoder) decodeXML(root *xmlNode) error {
311310

312311
} else if elem.state == "chardata" {
313312
log.Debug("got a line comment for (%v) %v: [%v]", elem.state, elem.label, commentStr)
314-
elem.n.LineComment = joinFilter([]string{elem.n.LineComment, commentStr})
313+
elem.n.LineComment = joinComments([]string{elem.n.LineComment, commentStr}, " ")
315314
} else {
316315
log.Debug("got a head comment for (%v) %v: [%v]", elem.state, elem.label, commentStr)
317-
elem.n.HeadComment = joinFilter([]string{elem.n.HeadComment, commentStr})
316+
elem.n.HeadComment = joinComments([]string{elem.n.HeadComment, commentStr}, " ")
318317
}
319318

320319
case xml.ProcInst:
@@ -339,21 +338,21 @@ func applyFootComment(elem *element, commentStr string) {
339338
lastChildIndex := len(elem.n.Children) - 1
340339
childKv := elem.n.Children[lastChildIndex]
341340
log.Debug("got a foot comment for %v: [%v]", childKv.K, commentStr)
342-
childKv.FootComment = joinFilter([]string{elem.n.FootComment, commentStr})
341+
childKv.FootComment = joinComments([]string{elem.n.FootComment, commentStr}, " ")
343342
} else {
344343
log.Debug("got a foot comment for %v: [%v]", elem.label, commentStr)
345-
elem.n.FootComment = joinFilter([]string{elem.n.FootComment, commentStr})
344+
elem.n.FootComment = joinComments([]string{elem.n.FootComment, commentStr}, " ")
346345
}
347346
}
348347

349-
func joinFilter(rawStrings []string) string {
348+
func joinComments(rawStrings []string, joinStr string) string {
350349
stringsToJoin := make([]string, 0)
351350
for _, str := range rawStrings {
352351
if str != "" {
353352
stringsToJoin = append(stringsToJoin, str)
354353
}
355354
}
356-
return strings.Join(stringsToJoin, " ")
355+
return strings.Join(stringsToJoin, joinStr)
357356
}
358357

359358
// trimNonGraphic returns a slice of the string s, with all leading and trailing

pkg/yqlib/xml_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,8 @@ var xmlScenarios = []formatScenario{
246246
},
247247
{
248248
skipDoc: true,
249-
input: "<root> <!-- comment-->value</root>",
250-
expected: "\n# comment\nroot: value\n", //needs fix
249+
input: "<root><!-- comment-->value</root>",
250+
expected: "# comment\nroot: value\n",
251251
},
252252
{
253253
skipDoc: true,

0 commit comments

Comments
 (0)