From cdd2a90a98c55c3d145af0639b5f51f8c2c4d923 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Fri, 31 Dec 2021 12:50:16 +1100 Subject: [PATCH] wip --- pkg/yqlib/decoder_xml.go | 15 +++++++++++---- pkg/yqlib/doc/usage/xml.md | 4 +++- pkg/yqlib/encoder_xml.go | 6 +++++- pkg/yqlib/lib.go | 11 +++++++++-- pkg/yqlib/xml_test.go | 6 +++--- 5 files changed, 31 insertions(+), 11 deletions(-) diff --git a/pkg/yqlib/decoder_xml.go b/pkg/yqlib/decoder_xml.go index bdb56a49..b08b25fc 100644 --- a/pkg/yqlib/decoder_xml.go +++ b/pkg/yqlib/decoder_xml.go @@ -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}) } diff --git a/pkg/yqlib/doc/usage/xml.md b/pkg/yqlib/doc/usage/xml.md index 2f424cba..21243f34 100644 --- a/pkg/yqlib/doc/usage/xml.md +++ b/pkg/yqlib/doc/usage/xml.md @@ -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 ``` diff --git a/pkg/yqlib/encoder_xml.go b/pkg/yqlib/encoder_xml.go index 12b3e547..254d8f1c 100644 --- a/pkg/yqlib/encoder_xml.go +++ b/pkg/yqlib/encoder_xml.go @@ -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) diff --git a/pkg/yqlib/lib.go b/pkg/yqlib/lib.go index 00c6c190..d34b2886 100644 --- a/pkg/yqlib/lib.go +++ b/pkg/yqlib/lib.go @@ -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 { diff --git a/pkg/yqlib/xml_test.go b/pkg/yqlib/xml_test.go index 6ea23e56..4334392e 100644 --- a/pkg/yqlib/xml_test.go +++ b/pkg/yqlib/xml_test.go @@ -91,11 +91,11 @@ cat: ` var expectedRoundtripXmlWithComments = ` - 3 +for x --> - 4 + 4 `