This commit is contained in:
Mike Farah 2021-12-31 12:50:16 +11:00
parent 2529a4708a
commit cdd2a90a98
5 changed files with 31 additions and 11 deletions

View File

@ -101,7 +101,8 @@ func (dec *xmlDecoder) convertToYamlNode(n *xmlNode) (*yaml.Node, error) {
}
scalar := createScalarNode(n.Data, n.Data)
log.Debug("scalar headC: %v, footC: %v", n.HeadComment, n.FootComment)
scalar.LineComment = n.HeadComment
scalar.HeadComment = n.HeadComment
scalar.LineComment = n.LineComment
scalar.FootComment = n.FootComment
return scalar, nil
@ -133,6 +134,7 @@ type xmlNode struct {
Children []*xmlChildrenKv
HeadComment string
FootComment string
LineComment string
Data string
}
@ -207,6 +209,9 @@ func (dec *xmlDecoder) decodeXml(root *xmlNode) error {
case xml.CharData:
// Extract XML data (if any)
elem.n.Data = trimNonGraphic(string(se))
if elem.n.Data != "" {
elem.state = "chardata"
}
case xml.EndElement:
log.Debug("end element %v", elem.label)
elem.state = "finished"
@ -228,14 +233,16 @@ func (dec *xmlDecoder) decodeXml(root *xmlNode) error {
child := elem.n.Children[len(elem.n.Children)-1]
log.Debug("putting it here: %v", child.K)
child.V[0].FootComment = child.V[0].FootComment + commentStr
child.V[0].FootComment = joinFilter([]string{child.V[0].FootComment, commentStr})
} else {
log.Debug("putting it on the element")
elem.n.FootComment = elem.n.FootComment + commentStr
elem.n.FootComment = joinFilter([]string{elem.n.FootComment, commentStr})
}
} else if elem.state == "chardata" {
elem.n.LineComment = joinFilter([]string{elem.n.LineComment, commentStr})
} else {
log.Debug("got a head comment for %v: %v", elem.label, commentStr)
log.Debug("got a head comment for (%v) %v: %v", elem.state, elem.label, commentStr)
elem.n.HeadComment = joinFilter([]string{elem.n.HeadComment, commentStr})
}

View File

@ -61,9 +61,11 @@ cat:
y:
# in y before
d: "4" # in d before in d after
d: "4" # in d after
# in y after
# in d before
# after cat
```

View File

@ -113,7 +113,7 @@ func (e *xmlEncoder) encodeStart(encoder *xml.Encoder, node *yaml.Node, start xm
if err != nil {
return err
}
return e.encodeComment(encoder, headAndLineComment(node))
return e.encodeComment(encoder, headComment(node))
}
func (e *xmlEncoder) encodeEnd(encoder *xml.Encoder, node *yaml.Node, start xml.StartElement) error {
@ -142,6 +142,10 @@ func (e *xmlEncoder) doEncode(encoder *xml.Encoder, node *yaml.Node, start xml.S
return err
}
if err = e.encodeComment(encoder, lineComment(node)); err != nil {
return err
}
return e.encodeEnd(encoder, node, start)
}
return fmt.Errorf("unsupported type %v", node.Tag)

View File

@ -235,8 +235,15 @@ func createScalarNode(value interface{}, stringValue string) *yaml.Node {
}
func headAndLineComment(node *yaml.Node) string {
return strings.Replace(node.HeadComment, "#", "", 1) +
strings.Replace(node.LineComment, "#", "", 1)
return headComment(node) + lineComment(node)
}
func headComment(node *yaml.Node) string {
return strings.Replace(node.HeadComment, "#", "", 1)
}
func lineComment(node *yaml.Node) string {
return strings.Replace(node.LineComment, "#", "", 1)
}
func footComment(node *yaml.Node) string {

View File

@ -91,11 +91,11 @@ cat:
`
var expectedRoundtripXmlWithComments = `<!-- before cat --><cat><!-- in cat before -->
<x><!-- multi
<x>3<!-- multi
line comment
for x -->3</x><!-- before y -->
for x --></x><!-- before y -->
<y><!-- in y before -->
<d><!-- in d before in d after -->4</d><!-- in y after -->
<d><!-- in d before -->4<!-- in d after --></d><!-- in y after -->
</y><!-- in_cat_after -->
</cat><!-- after cat -->
`