This commit is contained in:
Mike Farah 2022-01-01 12:24:44 +11:00
parent 8c512dcc40
commit 60b4840a20
3 changed files with 53 additions and 7 deletions

View File

@ -72,19 +72,26 @@ func (dec *xmlDecoder) processComment(c string) string {
func (dec *xmlDecoder) createMap(n *xmlNode) (*yaml.Node, error) { func (dec *xmlDecoder) createMap(n *xmlNode) (*yaml.Node, error) {
log.Debug("createMap: headC: %v, footC: %v", n.HeadComment, n.FootComment) 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 { if len(n.Data) > 0 {
label := dec.contentPrefix 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 label := keyValuePair.K
children := keyValuePair.V children := keyValuePair.V
labelNode := createScalarNode(label, label) labelNode := createScalarNode(label, label)
var valueNode *yaml.Node var valueNode *yaml.Node
var err error var err error
if i == 0 {
labelNode.HeadComment = dec.processComment(n.HeadComment)
}
log.Debug("len of children in %v is %v", label, len(children)) log.Debug("len of children in %v is %v", label, len(children))
if len(children) > 1 { if len(children) > 1 {
valueNode, err = dec.createSequence(children) valueNode, err = dec.createSequence(children)
@ -95,8 +102,8 @@ func (dec *xmlDecoder) createMap(n *xmlNode) (*yaml.Node, error) {
// 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 { if len(children[0].Children) == 0 && children[0].HeadComment != "" {
labelNode.HeadComment = joinFilter([]string{labelNode.HeadComment, children[0].HeadComment}) labelNode.HeadComment = labelNode.HeadComment + "\n" + strings.TrimSpace(children[0].HeadComment)
children[0].HeadComment = "" children[0].HeadComment = ""
} }
valueNode, err = dec.convertToYamlNode(children[0]) valueNode, err = dec.convertToYamlNode(children[0])

View File

@ -61,7 +61,8 @@ cat:
# before y # before y
y: y:
# in d before # in y before
# in d before
d: z # in d after d: z # in d after
# in y after # in y after

View File

@ -76,6 +76,43 @@ for x --></x>
</cat> </cat>
<!-- after cat --> <!-- after cat -->
` `
var inputXmlWithCommentsWithSubChild = `
<!-- before cat -->
<cat>
<!-- in cat before -->
<x>3<!-- multi
line comment
for x --></x>
<!-- before y -->
<y>
<!-- in y before -->
<d><!-- in d before --><z/><!-- in d after --></d>
<!-- in y after -->
</y>
<!-- in_cat_after -->
</cat>
<!-- after cat -->
`
var inputXmlWithCommentsWithArray = `
<!-- before cat -->
<cat>
<!-- in cat before -->
<x>3<!-- multi
line comment
for x --></x>
<!-- before y -->
<y>
<!-- in y before -->
<d><!-- in d before --><z/><!-- in d after --></d>
<!-- in y after -->
</y>
<!-- in_cat_after -->
</cat>
<!-- after cat -->
`
var expectedDecodeYamlWithComments = `D0, P[], (doc)::# before cat var expectedDecodeYamlWithComments = `D0, P[], (doc)::# before cat
cat: cat:
@ -87,7 +124,8 @@ cat:
y: y:
# in y before # in y before
d: "4" # in d before in d after # in d before
d: z # in d after
# in y after # in y after
# after cat # after cat