From 60b4840a20dc9ab5a375b7008c56a912f104dece Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Sat, 1 Jan 2022 12:24:44 +1100 Subject: [PATCH] wip --- pkg/yqlib/decoder_xml.go | 17 +++++++++++----- pkg/yqlib/doc/usage/xml.md | 3 ++- pkg/yqlib/xml_test.go | 40 +++++++++++++++++++++++++++++++++++++- 3 files changed, 53 insertions(+), 7 deletions(-) diff --git a/pkg/yqlib/decoder_xml.go b/pkg/yqlib/decoder_xml.go index e2a29dc0..49dc222c 100644 --- a/pkg/yqlib/decoder_xml.go +++ b/pkg/yqlib/decoder_xml.go @@ -72,19 +72,26 @@ func (dec *xmlDecoder) processComment(c string) string { func (dec *xmlDecoder) createMap(n *xmlNode) (*yaml.Node, error) { log.Debug("createMap: headC: %v, footC: %v", n.HeadComment, n.FootComment) - yamlNode := &yaml.Node{Kind: yaml.MappingNode, HeadComment: dec.processComment(n.HeadComment), FootComment: dec.processComment(n.FootComment)} + yamlNode := &yaml.Node{Kind: yaml.MappingNode, FootComment: dec.processComment(n.FootComment)} if len(n.Data) > 0 { label := dec.contentPrefix - yamlNode.Content = append(yamlNode.Content, createScalarNode(label, label), createScalarNode(n.Data, n.Data)) + labelNode := createScalarNode(label, label) + labelNode.HeadComment = dec.processComment(n.HeadComment) + yamlNode.Content = append(yamlNode.Content, labelNode, createScalarNode(n.Data, n.Data)) } - for _, keyValuePair := range n.Children { + for i, keyValuePair := range n.Children { label := keyValuePair.K children := keyValuePair.V labelNode := createScalarNode(label, label) var valueNode *yaml.Node var err error + + if i == 0 { + labelNode.HeadComment = dec.processComment(n.HeadComment) + } + log.Debug("len of children in %v is %v", label, len(children)) if len(children) > 1 { valueNode, err = dec.createSequence(children) @@ -95,8 +102,8 @@ func (dec *xmlDecoder) createMap(n *xmlNode) (*yaml.Node, error) { // comment hack for maps of scalars // if the value is a scalar, the head comment of the scalar needs to go on the key? // add tests for as well as multiple of inputXmlWithComments > yaml - if len(children[0].Children) == 0 { - labelNode.HeadComment = joinFilter([]string{labelNode.HeadComment, children[0].HeadComment}) + if len(children[0].Children) == 0 && children[0].HeadComment != "" { + labelNode.HeadComment = labelNode.HeadComment + "\n" + strings.TrimSpace(children[0].HeadComment) children[0].HeadComment = "" } valueNode, err = dec.convertToYamlNode(children[0]) diff --git a/pkg/yqlib/doc/usage/xml.md b/pkg/yqlib/doc/usage/xml.md index e374aed0..f3396539 100644 --- a/pkg/yqlib/doc/usage/xml.md +++ b/pkg/yqlib/doc/usage/xml.md @@ -61,7 +61,8 @@ cat: # before y y: - # in d before + # in y before + # in d before d: z # in d after # in y after diff --git a/pkg/yqlib/xml_test.go b/pkg/yqlib/xml_test.go index a8ffdcce..e1ed8de1 100644 --- a/pkg/yqlib/xml_test.go +++ b/pkg/yqlib/xml_test.go @@ -76,6 +76,43 @@ for x --> ` +var inputXmlWithCommentsWithSubChild = ` + + + + 3 + + + + + + + + + + +` + +var inputXmlWithCommentsWithArray = ` + + + + 3 + + + + + + + + + + +` var expectedDecodeYamlWithComments = `D0, P[], (doc)::# before cat cat: @@ -87,7 +124,8 @@ cat: y: # in y before - d: "4" # in d before in d after + # in d before + d: z # in d after # in y after # after cat