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

View File

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

View File

@ -113,7 +113,7 @@ func (e *xmlEncoder) encodeStart(encoder *xml.Encoder, node *yaml.Node, start xm
if err != nil { if err != nil {
return err 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 { 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 return err
} }
if err = e.encodeComment(encoder, lineComment(node)); err != nil {
return err
}
return e.encodeEnd(encoder, node, start) return e.encodeEnd(encoder, node, start)
} }
return fmt.Errorf("unsupported type %v", node.Tag) 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 { func headAndLineComment(node *yaml.Node) string {
return strings.Replace(node.HeadComment, "#", "", 1) + return headComment(node) + lineComment(node)
strings.Replace(node.LineComment, "#", "", 1) }
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 { func footComment(node *yaml.Node) string {

View File

@ -91,11 +91,11 @@ cat:
` `
var expectedRoundtripXmlWithComments = `<!-- before cat --><cat><!-- in cat before --> var expectedRoundtripXmlWithComments = `<!-- before cat --><cat><!-- in cat before -->
<x><!-- multi <x>3<!-- multi
line comment line comment
for x -->3</x><!-- before y --> for x --></x><!-- before y -->
<y><!-- in y before --> <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 --> </y><!-- in_cat_after -->
</cat><!-- after cat --> </cat><!-- after cat -->
` `