tests passing!

This commit is contained in:
Mike Farah 2022-01-01 12:34:15 +11:00
parent 60b4840a20
commit 4b0cd5335b
3 changed files with 304 additions and 76 deletions

View File

@ -22,6 +22,81 @@ XML nodes that have attributes then plain content, e.g:
The content of the node will be set as a field in the map with the key "+content". Use the `--xml-content-name` flag to change this. The content of the node will be set as a field in the map with the key "+content". Use the `--xml-content-name` flag to change this.
## Parse xml: simple
Given a sample.xml file of:
```xml
<?xml version="1.0" encoding="UTF-8"?>
<cat>meow</cat>
```
then
```bash
yq e -p=xml '.' sample.xml
```
will output
```yaml
cat: meow
```
## Parse xml: array
Consecutive nodes with identical xml names are assumed to be arrays.
Given a sample.xml file of:
```xml
<?xml version="1.0" encoding="UTF-8"?>
<animal>1</animal>
<animal>2</animal>
```
then
```bash
yq e -p=xml '.' sample.xml
```
will output
```yaml
animal:
- "1"
- "2"
```
## Parse xml: attributes
Attributes are converted to fields, with the attribute prefix.
Given a sample.xml file of:
```xml
<?xml version="1.0" encoding="UTF-8"?>
<cat legs="4">
<legs>7</legs>
</cat>
```
then
```bash
yq e -p=xml '.' sample.xml
```
will output
```yaml
cat:
+legs: "4"
legs: "7"
```
## Parse xml: attributes with content
Content is added as a field, using the content name
Given a sample.xml file of:
```xml
<?xml version="1.0" encoding="UTF-8"?>
<cat legs="4">meow</cat>
```
then
```bash
yq e -p=xml '.' sample.xml
```
will output
```yaml
cat:
+content: meow
+legs: "4"
```
## Parse xml: with comments ## Parse xml: with comments
A best attempt is made to preserve comments. A best attempt is made to preserve comments.
@ -69,3 +144,151 @@ cat:
# after cat # after cat
``` ```
## 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>
```
## Encode xml: attributes
Fields with the matching xml-attribute-prefix are assumed to be attributes.
Given a sample.yml file of:
```yaml
cat:
+name: tiger
meows: true
```
then
```bash
yq e -o=xml '.' sample.yml
```
will output
```xml
<cat name="tiger">
<meows>true</meows>
</cat>
```
## Encode xml: attributes with content
Fields with the matching xml-content-name is assumed to be content.
Given a sample.yml file of:
```yaml
cat:
+name: tiger
+content: cool
```
then
```bash
yq e -o=xml '.' sample.yml
```
will output
```xml
<cat name="tiger">cool</cat>
```
## Encode xml: comments
A best attempt is made to copy comments to xml.
Given a sample.yml file of:
```yaml
# above_cat
cat: # inline_cat
# above_array
array: # inline_array
- val1 # inline_val1
# above_val2
- val2 # inline_val2
# below_cat
```
then
```bash
yq e -o=xml '.' sample.yml
```
will output
```xml
<!-- above_cat inline_cat --><cat><!-- above_array inline_array -->
<array>val1<!-- inline_val1 --></array>
<array><!-- above_val2 -->val2<!-- inline_val2 --></array>
</cat><!-- below_cat -->
```
## Round trip: with comments
A best effort is made, but comment positions and white space are not preserved perfectly.
Given a sample.xml file of:
```xml
<!-- 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 -->
```
then
```bash
yq e -p=xml '.' sample.xml
```
will output
```yaml
# before cat
cat:
# in cat before
x: "3" # multi
# line comment
# for x
# before y
y:
# in y before
# in d before
d: z # in d after
# in y after
# after cat
```

View File

@ -154,6 +154,10 @@ func (e *xmlEncoder) doEncode(encoder *xml.Encoder, node *yaml.Node, start xml.S
func (e *xmlEncoder) encodeComment(encoder *xml.Encoder, commentStr string) error { func (e *xmlEncoder) encodeComment(encoder *xml.Encoder, commentStr string) error {
if commentStr != "" { if commentStr != "" {
log.Debugf("encoding comment %v", commentStr) log.Debugf("encoding comment %v", commentStr)
if !strings.HasSuffix(commentStr, " ") {
commentStr = commentStr + " "
}
var comment xml.Comment = []byte(commentStr) var comment xml.Comment = []byte(commentStr)
err := encoder.EncodeToken(comment) err := encoder.EncodeToken(comment)
if err != nil { if err != nil {

View File

@ -133,10 +133,11 @@ cat:
var expectedRoundtripXmlWithComments = `<!-- before cat --><cat><!-- in cat before --> var expectedRoundtripXmlWithComments = `<!-- before cat --><cat><!-- in cat before -->
<x>3<!-- multi <x>3<!-- multi
line comment line comment
for x --></x><!-- before y --> for x --></x><!-- before y -->
<y><!-- in y before --> <y><!-- in y before
<d><!-- in d before -->4<!-- in d after --></d><!-- in y after --> in d before -->
<d>z<!-- in d after --></d><!-- in y after -->
</y><!-- in_cat_after --> </y><!-- in_cat_after -->
</cat><!-- after cat --> </cat><!-- after cat -->
` `
@ -151,36 +152,36 @@ cat: # inline_cat
# below_cat # below_cat
` `
var expectedXmlWithComments = `<!-- above_cat inline_cat--><cat><!-- above_array inline_array--> var expectedXmlWithComments = `<!-- above_cat inline_cat --><cat><!-- above_array inline_array -->
<array>val1<!-- inline_val1--></array> <array>val1<!-- inline_val1 --></array>
<array><!-- above_val2-->val2<!-- inline_val2--></array> <array><!-- above_val2 -->val2<!-- inline_val2 --></array>
</cat><!-- below_cat--> </cat><!-- below_cat -->
` `
var xmlScenarios = []xmlScenario{ var xmlScenarios = []xmlScenario{
// { {
// description: "Parse xml: simple", description: "Parse xml: simple",
// input: "<?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", expected: "D0, P[], (doc)::cat: meow\n",
// }, },
// { {
// description: "Parse xml: array", description: "Parse xml: array",
// subdescription: "Consecutive nodes with identical xml names are assumed to be arrays.", subdescription: "Consecutive nodes with identical xml names are assumed to be arrays.",
// input: "<?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", expected: "D0, P[], (doc)::animal:\n - \"1\"\n - \"2\"\n",
// }, },
// { {
// description: "Parse xml: attributes", description: "Parse xml: attributes",
// subdescription: "Attributes are converted to fields, with the attribute prefix.", subdescription: "Attributes are converted to fields, with the attribute prefix.",
// input: "<?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", expected: "D0, P[], (doc)::cat:\n +legs: \"4\"\n legs: \"7\"\n",
// }, },
// { {
// description: "Parse xml: attributes with content", description: "Parse xml: attributes with content",
// subdescription: "Content is added as a field, using the content name", subdescription: "Content is added as a field, using the content name",
// input: "<?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", expected: "D0, P[], (doc)::cat:\n +content: meow\n +legs: \"4\"\n",
// }, },
{ {
description: "Parse xml: with comments", description: "Parse xml: with comments",
subdescription: "A best attempt is made to preserve comments.", subdescription: "A best attempt is made to preserve comments.",
@ -188,52 +189,52 @@ var xmlScenarios = []xmlScenario{
expected: expectedDecodeYamlWithComments, expected: expectedDecodeYamlWithComments,
scenarioType: "decode", scenarioType: "decode",
}, },
// { {
// description: "Encode xml: simple", description: "Encode xml: simple",
// input: "cat: purrs", input: "cat: purrs",
// expected: "<cat>purrs</cat>\n", expected: "<cat>purrs</cat>\n",
// scenarioType: "encode", scenarioType: "encode",
// }, },
// { {
// description: "Encode xml: array", description: "Encode xml: array",
// input: "pets:\n cat:\n - purrs\n - meows", input: "pets:\n cat:\n - purrs\n - meows",
// expected: "<pets>\n <cat>purrs</cat>\n <cat>meows</cat>\n</pets>\n", expected: "<pets>\n <cat>purrs</cat>\n <cat>meows</cat>\n</pets>\n",
// scenarioType: "encode", scenarioType: "encode",
// }, },
// { {
// description: "Encode xml: attributes", description: "Encode xml: attributes",
// subdescription: "Fields with the matching xml-attribute-prefix are assumed to be attributes.", subdescription: "Fields with the matching xml-attribute-prefix are assumed to be attributes.",
// input: "cat:\n +name: tiger\n meows: true\n", input: "cat:\n +name: tiger\n meows: true\n",
// expected: "<cat name=\"tiger\">\n <meows>true</meows>\n</cat>\n", expected: "<cat name=\"tiger\">\n <meows>true</meows>\n</cat>\n",
// scenarioType: "encode", scenarioType: "encode",
// }, },
// { {
// skipDoc: true, skipDoc: true,
// input: "cat:\n ++name: tiger\n meows: true\n", input: "cat:\n ++name: tiger\n meows: true\n",
// expected: "<cat +name=\"tiger\">\n <meows>true</meows>\n</cat>\n", expected: "<cat +name=\"tiger\">\n <meows>true</meows>\n</cat>\n",
// scenarioType: "encode", scenarioType: "encode",
// }, },
// { {
// description: "Encode xml: attributes with content", description: "Encode xml: attributes with content",
// subdescription: "Fields with the matching xml-content-name is assumed to be content.", subdescription: "Fields with the matching xml-content-name is assumed to be content.",
// input: "cat:\n +name: tiger\n +content: cool\n", input: "cat:\n +name: tiger\n +content: cool\n",
// expected: "<cat name=\"tiger\">cool</cat>\n", expected: "<cat name=\"tiger\">cool</cat>\n",
// scenarioType: "encode", scenarioType: "encode",
// }, },
// { {
// description: "Encode xml: comments", description: "Encode xml: comments",
// subdescription: "A best attempt is made to copy comments to xml.", subdescription: "A best attempt is made to copy comments to xml.",
// input: yamlWithComments, input: yamlWithComments,
// expected: expectedXmlWithComments, expected: expectedXmlWithComments,
// scenarioType: "encode", scenarioType: "encode",
// }, },
// { {
// description: "Round trip: with comments", description: "Round trip: with comments",
// subdescription: "A best effort is made, but comment positions and white space are not preserved perfectly.", subdescription: "A best effort is made, but comment positions and white space are not preserved perfectly.",
// input: inputXmlWithComments, input: inputXmlWithComments,
// expected: expectedRoundtripXmlWithComments, expected: expectedRoundtripXmlWithComments,
// scenarioType: "roundtrip", scenarioType: "roundtrip",
// }, },
} }
func testXmlScenario(t *testing.T, s xmlScenario) { func testXmlScenario(t *testing.T, s xmlScenario) {