This commit is contained in:
Mike Farah 2021-12-21 15:56:08 +11:00
parent df32baedf1
commit 851a43b9b6
3 changed files with 204 additions and 14 deletions

View File

@ -19,7 +19,7 @@ Given a sample.xml file of:
```
then
```bash
yq e sample.xml
yq e -p=xml '.' sample.xml
```
will output
```yaml
@ -37,7 +37,7 @@ Given a sample.xml file of:
```
then
```bash
yq e sample.xml
yq e -p=xml '.' sample.xml
```
will output
```yaml
@ -58,7 +58,7 @@ Given a sample.xml file of:
```
then
```bash
yq e sample.xml
yq e -p=xml '.' sample.xml
```
will output
```yaml
@ -77,7 +77,7 @@ Given a sample.xml file of:
```
then
```bash
yq e sample.xml
yq e -p=xml '.' sample.xml
```
will output
```yaml
@ -86,3 +86,37 @@ cat:
+legs: "4"
```
## Encode xml: simple
Given a sample.yml file of:
```yaml
cat: purrs
```
then
```bash
yq e -o=xml '.' sample.yml
```
will output
```xml
<cat>purrs</cat>
```
## Encode xml: array
Given a sample.yml file of:
```yaml
pets:
cat:
- purrs
- meows
```
then
```bash
yq e -o=xml '.' sample.yml
```
will output
```xml
<pets>
<cat>purrs</cat>
<cat>meows</cat>
</pets>
```

92
pkg/yqlib/encoder_xml.go Normal file
View File

@ -0,0 +1,92 @@
package yqlib
import (
"encoding/xml"
"fmt"
"io"
yaml "gopkg.in/yaml.v3"
)
type xmlEncoder struct {
xmlEncoder *xml.Encoder
}
func NewXmlEncoder(writer io.Writer, indent int) Encoder {
encoder := xml.NewEncoder(writer)
var indentString = ""
for index := 0; index < indent; index++ {
indentString = indentString + " "
}
encoder.Indent("", indentString)
return &xmlEncoder{encoder}
}
func (e *xmlEncoder) Encode(node *yaml.Node) error {
switch node.Kind {
case yaml.MappingNode:
return e.encodeMap(node)
case yaml.DocumentNode:
return e.Encode(unwrapDoc(node))
case yaml.ScalarNode:
var charData xml.CharData = []byte(node.Value)
return e.xmlEncoder.EncodeToken(charData)
}
return fmt.Errorf("unsupported type %v", node.Tag)
}
func (e *xmlEncoder) doEncode(node *yaml.Node, start xml.StartElement) error {
switch node.Kind {
case yaml.MappingNode:
err := e.xmlEncoder.EncodeToken(start)
if err != nil {
return err
}
err = e.encodeMap(node)
if err != nil {
return err
}
return e.xmlEncoder.EncodeToken(start.End())
case yaml.SequenceNode:
return e.encodeArray(node, start)
case yaml.ScalarNode:
err := e.xmlEncoder.EncodeToken(start)
if err != nil {
return err
}
var charData xml.CharData = []byte(node.Value)
err = e.xmlEncoder.EncodeToken(charData)
if err != nil {
return err
}
return e.xmlEncoder.EncodeToken(start.End())
}
return fmt.Errorf("unsupported type %v", node.Tag)
}
func (e *xmlEncoder) encodeArray(node *yaml.Node, start xml.StartElement) error {
for i := 0; i < len(node.Content); i++ {
value := node.Content[i]
err := e.doEncode(value, start.Copy())
if err != nil {
return err
}
}
return nil
}
func (e *xmlEncoder) encodeMap(node *yaml.Node) error {
for i := 0; i < len(node.Content); i += 2 {
key := node.Content[i]
value := node.Content[i+1]
start := xml.StartElement{Name: xml.Name{Local: key.Value}}
err := e.doEncode(value, start)
if err != nil {
return err
}
}
return nil
}

View File

@ -24,43 +24,81 @@ func decodeXml(t *testing.T, xml string) *CandidateNode {
return &CandidateNode{Node: node}
}
func yamlToXml(sampleYaml string, indent int) string {
var output bytes.Buffer
writer := bufio.NewWriter(&output)
var encoder = NewXmlEncoder(writer, indent)
inputs, err := readDocuments(strings.NewReader(sampleYaml), "sample.yml", 0, NewYamlDecoder())
if err != nil {
panic(err)
}
node := inputs.Front().Value.(*CandidateNode).Node
err = encoder.Encode(node)
if err != nil {
panic(err)
}
writer.Flush()
return strings.TrimSuffix(output.String(), "\n")
}
type xmlScenario struct {
inputXml string
input string
expected string
description string
subdescription string
skipDoc bool
encodeScenario bool
}
var xmlScenarios = []xmlScenario{
{
description: "Parse xml: simple",
inputXml: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<cat>meow</cat>",
input: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<cat>meow</cat>",
expected: "D0, P[], (doc)::cat: meow\n",
},
{
description: "Parse xml: array",
subdescription: "Consecutive nodes with identical xml names are assumed to be arrays.",
inputXml: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<animal>1</animal>\n<animal>2</animal>",
input: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<animal>1</animal>\n<animal>2</animal>",
expected: "D0, P[], (doc)::animal:\n - \"1\"\n - \"2\"\n",
},
{
description: "Parse xml: attributes",
subdescription: "Attributes are converted to fields, with the attribute prefix.",
inputXml: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<cat legs=\"4\">\n <legs>7</legs>\n</cat>",
input: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<cat legs=\"4\">\n <legs>7</legs>\n</cat>",
expected: "D0, P[], (doc)::cat:\n +legs: \"4\"\n legs: \"7\"\n",
},
{
description: "Parse xml: attributes with content",
subdescription: "Content is added as a field, using the content name",
inputXml: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<cat legs=\"4\">meow</cat>",
input: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<cat legs=\"4\">meow</cat>",
expected: "D0, P[], (doc)::cat:\n +content: meow\n +legs: \"4\"\n",
},
{
description: "Encode xml: simple",
input: "cat: purrs",
expected: "<cat>purrs</cat>",
encodeScenario: true,
},
{
description: "Encode xml: array",
input: "pets:\n cat:\n - purrs\n - meows",
expected: "<pets>\n <cat>purrs</cat>\n <cat>meows</cat>\n</pets>",
encodeScenario: true,
},
}
//encode
func testXmlScenario(t *testing.T, s *xmlScenario) {
var actual = resultToString(t, decodeXml(t, s.inputXml))
test.AssertResult(t, s.expected, actual)
if s.encodeScenario {
test.AssertResultWithContext(t, s.expected, yamlToXml(s.input, 2), s.description)
} else {
var actual = resultToString(t, decodeXml(t, s.input))
test.AssertResultWithContext(t, s.expected, actual, s.description)
}
}
func documentXmlScenario(t *testing.T, w *bufio.Writer, i interface{}) {
@ -69,6 +107,15 @@ func documentXmlScenario(t *testing.T, w *bufio.Writer, i interface{}) {
if s.skipDoc {
return
}
if s.encodeScenario {
documentXmlEncodeScenario(t, w, s)
} else {
documentXmlDecodeScenario(t, w, s)
}
}
func documentXmlDecodeScenario(t *testing.T, w *bufio.Writer, s xmlScenario) {
writeOrPanic(w, fmt.Sprintf("## %v\n", s.description))
if s.subdescription != "" {
@ -77,16 +124,16 @@ func documentXmlScenario(t *testing.T, w *bufio.Writer, i interface{}) {
}
writeOrPanic(w, "Given a sample.xml file of:\n")
writeOrPanic(w, fmt.Sprintf("```xml\n%v\n```\n", s.inputXml))
writeOrPanic(w, fmt.Sprintf("```xml\n%v\n```\n", s.input))
writeOrPanic(w, "then\n")
writeOrPanic(w, "```bash\nyq e sample.xml\n```\n")
writeOrPanic(w, "```bash\nyq e -p=xml '.' sample.xml\n```\n")
writeOrPanic(w, "will output\n")
var output bytes.Buffer
printer := NewPrinterWithSingleWriter(bufio.NewWriter(&output), YamlOutputFormat, true, false, 2, true)
node := decodeXml(t, s.inputXml)
node := decodeXml(t, s.input)
err := printer.PrintResults(node.AsList())
if err != nil {
@ -95,7 +142,24 @@ func documentXmlScenario(t *testing.T, w *bufio.Writer, i interface{}) {
}
writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", output.String()))
}
func documentXmlEncodeScenario(t *testing.T, w *bufio.Writer, s xmlScenario) {
writeOrPanic(w, fmt.Sprintf("## %v\n", s.description))
if s.subdescription != "" {
writeOrPanic(w, s.subdescription)
writeOrPanic(w, "\n\n")
}
writeOrPanic(w, "Given a sample.yml file of:\n")
writeOrPanic(w, fmt.Sprintf("```yaml\n%v\n```\n", s.input))
writeOrPanic(w, "then\n")
writeOrPanic(w, "```bash\nyq e -o=xml '.' sample.yml\n```\n")
writeOrPanic(w, "will output\n")
writeOrPanic(w, fmt.Sprintf("```xml\n%v\n```\n\n", yamlToXml(s.input, 2)))
}
func TestXmlScenarios(t *testing.T) {