Updated default xml naming prefix

This commit is contained in:
Mike Farah 2022-11-10 22:22:55 +11:00
parent 46dbd0c859
commit ececd00fbd
7 changed files with 38 additions and 29 deletions

View File

@ -59,10 +59,18 @@ yq -P sample.json
return err return err
} }
if (inputFormat == "x" || inputFormat == "xml") && inputFormatType, err := yqlib.InputFormatFromString(inputFormat)
outputFormatType != yqlib.XMLOutputFormat &&
yqlib.ConfiguredXMLPreferences.AttributePrefix == "+" { if err != nil {
yqlib.GetLogger().Warning("The default xml-attribute-prefix will change in the v4.30 to `+@` to avoid " + return err
}
if (inputFormatType == yqlib.XMLInputFormat &&
outputFormatType != yqlib.XMLOutputFormat ||
inputFormatType != yqlib.XMLInputFormat &&
outputFormatType == yqlib.XMLOutputFormat) &&
yqlib.ConfiguredXMLPreferences.AttributePrefix == "+@" {
yqlib.GetLogger().Warning("The default xml-attribute-prefix has changed in the v4.30 to `+@` to avoid " +
"naming conflicts with the default content name, directive name and proc inst prefix. If you need to keep " + "naming conflicts with the default content name, directive name and proc inst prefix. If you need to keep " +
"`+` please set that value explicityly with --xml-attribute-prefix.") "`+` please set that value explicityly with --xml-attribute-prefix.")
} }

View File

@ -337,7 +337,7 @@ Given a sample.yml file of:
a: a:
cool: cool:
foo: bar foo: bar
+id: hi +@id: hi
``` ```
then then
```bash ```bash
@ -357,7 +357,7 @@ Given a sample.yml file of:
a: a:
cool: cool:
foo: bar foo: bar
+id: hi +@id: hi
``` ```
then then
```bash ```bash
@ -375,7 +375,7 @@ Given a sample.yml file of:
a: a:
cool: cool:
foo: bar foo: bar
+id: hi +@id: hi
``` ```
then then
```bash ```bash

View File

@ -128,7 +128,7 @@ will output
```yaml ```yaml
+p_xml: version="1.0" encoding="UTF-8" +p_xml: version="1.0" encoding="UTF-8"
cat: cat:
+legs: "4" +@legs: "4"
legs: "7" legs: "7"
``` ```
@ -149,7 +149,7 @@ will output
+p_xml: version="1.0" encoding="UTF-8" +p_xml: version="1.0" encoding="UTF-8"
cat: cat:
+content: meow +content: meow
+legs: "4" +@legs: "4"
``` ```
## Parse xml: custom dtd ## Parse xml: custom dtd
@ -347,7 +347,7 @@ Fields with the matching xml-attribute-prefix are assumed to be attributes.
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
cat: cat:
+name: tiger +@name: tiger
meows: true meows: true
``` ```
@ -368,7 +368,7 @@ Fields with the matching xml-content-name is assumed to be content.
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
cat: cat:
+name: tiger +@name: tiger
+content: cool +content: cool
``` ```

View File

@ -194,7 +194,7 @@ var encoderDecoderOperatorScenarios = []expressionScenario{
}, },
{ {
description: "Encode value as xml string", description: "Encode value as xml string",
document: `{a: {cool: {foo: "bar", +id: hi}}}`, document: `{a: {cool: {foo: "bar", +@id: hi}}}`,
expression: `.a | to_xml`, expression: `.a | to_xml`,
expected: []string{ expected: []string{
"D0, P[a], (!!str)::<cool id=\"hi\">\n <foo>bar</foo>\n</cool>\n\n", "D0, P[a], (!!str)::<cool id=\"hi\">\n <foo>bar</foo>\n</cool>\n\n",
@ -202,7 +202,7 @@ var encoderDecoderOperatorScenarios = []expressionScenario{
}, },
{ {
description: "Encode value as xml string on a single line", description: "Encode value as xml string on a single line",
document: `{a: {cool: {foo: "bar", +id: hi}}}`, document: `{a: {cool: {foo: "bar", +@id: hi}}}`,
expression: `.a | @xml`, expression: `.a | @xml`,
expected: []string{ expected: []string{
"D0, P[a], (!!str)::<cool id=\"hi\"><foo>bar</foo></cool>\n\n", "D0, P[a], (!!str)::<cool id=\"hi\"><foo>bar</foo></cool>\n\n",
@ -210,7 +210,7 @@ var encoderDecoderOperatorScenarios = []expressionScenario{
}, },
{ {
description: "Encode value as xml string with custom indentation", description: "Encode value as xml string with custom indentation",
document: `{a: {cool: {foo: "bar", +id: hi}}}`, document: `{a: {cool: {foo: "bar", +@id: hi}}}`,
expression: `{"cat": .a | to_xml(1)}`, expression: `{"cat": .a | to_xml(1)}`,
expected: []string{ expected: []string{
"D0, P[], (!!map)::cat: |\n <cool id=\"hi\">\n <foo>bar</foo>\n </cool>\n", "D0, P[], (!!map)::cat: |\n <cool id=\"hi\">\n <foo>bar</foo>\n </cool>\n",

View File

@ -14,7 +14,7 @@ type XmlPreferences struct {
func NewDefaultXmlPreferences() XmlPreferences { func NewDefaultXmlPreferences() XmlPreferences {
return XmlPreferences{ return XmlPreferences{
AttributePrefix: "+", AttributePrefix: "+@",
ContentName: "+content", ContentName: "+content",
StrictMode: false, StrictMode: false,
KeepNamespace: true, KeepNamespace: true,

View File

@ -58,7 +58,7 @@ cat:
d: d:
# in d before # in d before
z: z:
+sweet: cool +@sweet: cool
# in d after # in d after
# in y after # in y after
# in_cat_after # in_cat_after
@ -98,11 +98,11 @@ cat:
d: d:
- # in d before - # in d before
z: z:
+sweet: cool +@sweet: cool
# in d after # in d after
- # in d2 before - # in d2 before
z: z:
+sweet: cool2 +@sweet: cool2
# in d2 after # in d2 after
# in y after # in y after
# in_cat_after # in_cat_after
@ -165,16 +165,16 @@ const inputXMLWithNamespacedAttr = `
const expectedYAMLWithNamespacedAttr = `+p_xml: version="1.0" const expectedYAMLWithNamespacedAttr = `+p_xml: version="1.0"
map: map:
+xmlns: some-namespace +@xmlns: some-namespace
+xmlns:xsi: some-instance +@xmlns:xsi: some-instance
+some-instance:schemaLocation: some-url +@some-instance:schemaLocation: some-url
` `
const expectedYAMLWithRawNamespacedAttr = `+p_xml: version="1.0" const expectedYAMLWithRawNamespacedAttr = `+p_xml: version="1.0"
map: map:
+xmlns: some-namespace +@xmlns: some-namespace
+xmlns:xsi: some-instance +@xmlns:xsi: some-instance
+xsi:schemaLocation: some-url +@xsi:schemaLocation: some-url
` `
const xmlWithCustomDtd = ` const xmlWithCustomDtd = `
@ -251,13 +251,13 @@ var xmlScenarios = []formatScenario{
description: "Parse xml: attributes", description: "Parse xml: attributes",
subdescription: "Attributes are converted to fields, with the default attribute prefix '+'. Use '--xml-attribute-prefix` to set your own.", subdescription: "Attributes are converted to fields, with the default attribute prefix '+'. Use '--xml-attribute-prefix` to set your own.",
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: "+p_xml: version=\"1.0\" encoding=\"UTF-8\"\ncat:\n +legs: \"4\"\n legs: \"7\"\n", expected: "+p_xml: version=\"1.0\" encoding=\"UTF-8\"\ncat:\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 default content name of `+content`. Use `--xml-content-name` to set your own.", subdescription: "Content is added as a field, using the default content name of `+content`. Use `--xml-content-name` to set your own.",
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: "+p_xml: version=\"1.0\" encoding=\"UTF-8\"\ncat:\n +content: meow\n +legs: \"4\"\n", expected: "+p_xml: version=\"1.0\" encoding=\"UTF-8\"\ncat:\n +content: meow\n +@legs: \"4\"\n",
}, },
{ {
description: "Parse xml: custom dtd", description: "Parse xml: custom dtd",
@ -367,14 +367,14 @@ var xmlScenarios = []formatScenario{
{ {
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",
}, },
{ {
description: "double prefix", description: "double prefix",
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",
}, },
@ -402,7 +402,7 @@ var xmlScenarios = []formatScenario{
{ {
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",
}, },

View File

@ -1,4 +1,5 @@
4.30.1: 4.30.1:
- XML users note: the default attribute prefix has change to `+@` to avoid naming conflicts!
- Can use expressions in slice #1419 - Can use expressions in slice #1419
- Fixed unhandled exception when decoding CSV thanks @washanhanzi - Fixed unhandled exception when decoding CSV thanks @washanhanzi
- Added array_to_map operator for #1415 - Added array_to_map operator for #1415