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
}
if (inputFormat == "x" || inputFormat == "xml") &&
outputFormatType != yqlib.XMLOutputFormat &&
yqlib.ConfiguredXMLPreferences.AttributePrefix == "+" {
yqlib.GetLogger().Warning("The default xml-attribute-prefix will change in the v4.30 to `+@` to avoid " +
inputFormatType, err := yqlib.InputFormatFromString(inputFormat)
if err != nil {
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 " +
"`+` please set that value explicityly with --xml-attribute-prefix.")
}

View File

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

View File

@ -128,7 +128,7 @@ will output
```yaml
+p_xml: version="1.0" encoding="UTF-8"
cat:
+legs: "4"
+@legs: "4"
legs: "7"
```
@ -149,7 +149,7 @@ will output
+p_xml: version="1.0" encoding="UTF-8"
cat:
+content: meow
+legs: "4"
+@legs: "4"
```
## 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:
```yaml
cat:
+name: tiger
+@name: tiger
meows: true
```
@ -368,7 +368,7 @@ Fields with the matching xml-content-name is assumed to be content.
Given a sample.yml file of:
```yaml
cat:
+name: tiger
+@name: tiger
+content: cool
```

View File

@ -194,7 +194,7 @@ var encoderDecoderOperatorScenarios = []expressionScenario{
},
{
description: "Encode value as xml string",
document: `{a: {cool: {foo: "bar", +id: hi}}}`,
document: `{a: {cool: {foo: "bar", +@id: hi}}}`,
expression: `.a | to_xml`,
expected: []string{
"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",
document: `{a: {cool: {foo: "bar", +id: hi}}}`,
document: `{a: {cool: {foo: "bar", +@id: hi}}}`,
expression: `.a | @xml`,
expected: []string{
"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",
document: `{a: {cool: {foo: "bar", +id: hi}}}`,
document: `{a: {cool: {foo: "bar", +@id: hi}}}`,
expression: `{"cat": .a | to_xml(1)}`,
expected: []string{
"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 {
return XmlPreferences{
AttributePrefix: "+",
AttributePrefix: "+@",
ContentName: "+content",
StrictMode: false,
KeepNamespace: true,

View File

@ -58,7 +58,7 @@ cat:
d:
# in d before
z:
+sweet: cool
+@sweet: cool
# in d after
# in y after
# in_cat_after
@ -98,11 +98,11 @@ cat:
d:
- # in d before
z:
+sweet: cool
+@sweet: cool
# in d after
- # in d2 before
z:
+sweet: cool2
+@sweet: cool2
# in d2 after
# in y after
# in_cat_after
@ -165,16 +165,16 @@ const inputXMLWithNamespacedAttr = `
const expectedYAMLWithNamespacedAttr = `+p_xml: version="1.0"
map:
+xmlns: some-namespace
+xmlns:xsi: some-instance
+some-instance:schemaLocation: some-url
+@xmlns: some-namespace
+@xmlns:xsi: some-instance
+@some-instance:schemaLocation: some-url
`
const expectedYAMLWithRawNamespacedAttr = `+p_xml: version="1.0"
map:
+xmlns: some-namespace
+xmlns:xsi: some-instance
+xsi:schemaLocation: some-url
+@xmlns: some-namespace
+@xmlns:xsi: some-instance
+@xsi:schemaLocation: some-url
`
const xmlWithCustomDtd = `
@ -251,13 +251,13 @@ var xmlScenarios = []formatScenario{
description: "Parse xml: attributes",
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>",
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",
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>",
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",
@ -367,14 +367,14 @@ var xmlScenarios = []formatScenario{
{
description: "Encode xml: 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",
scenarioType: "encode",
},
{
description: "double prefix",
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",
scenarioType: "encode",
},
@ -402,7 +402,7 @@ var xmlScenarios = []formatScenario{
{
description: "Encode xml: attributes with 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",
scenarioType: "encode",
},

View File

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