Hotfix: actually update the default xml attribute prefix #1428

This commit is contained in:
Mike Farah 2022-11-13 11:13:05 +11:00
parent 83ef7ee5cf
commit 88ce6ffcbe
7 changed files with 97 additions and 69 deletions

View File

@ -109,7 +109,7 @@ EOL
read -r -d '' expected << EOM read -r -d '' expected << EOM
cat: cat:
+content: BiBi +content: BiBi
+legs: "4" +@legs: "4"
EOM EOM
X=$(./yq e -p=xml test.yml) X=$(./yq e -p=xml test.yml)
@ -129,9 +129,9 @@ EOL
read -r -d '' expected << EOM read -r -d '' expected << EOM
+p_xml: version="1.0" +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
EOM EOM
X=$(./yq e -p=xml test.yml) X=$(./yq e -p=xml test.yml)
@ -190,7 +190,7 @@ EOL
read -r -d '' expected << EOM read -r -d '' expected << EOM
cat: cat:
+content: BiBi +content: BiBi
+legs: "4" +@legs: "4"
EOM EOM
X=$(cat /dev/null | ./yq e -p=xml test.yml) X=$(cat /dev/null | ./yq e -p=xml test.yml)

View File

@ -252,7 +252,7 @@ EOM
testOutputXmComplex() { testOutputXmComplex() {
cat >test.yml <<EOL cat >test.yml <<EOL
a: {b: {c: ["cat", "dog"], +f: meow}} a: {b: {c: ["cat", "dog"], +@f: meow}}
EOL EOL
read -r -d '' expected << EOM read -r -d '' expected << EOM

View File

@ -103,15 +103,15 @@ yq -P sample.json
rootCmd.PersistentFlags().StringVarP(&outputFormat, "output-format", "o", "yaml", "[yaml|y|json|j|props|p|xml|x] output format type.") rootCmd.PersistentFlags().StringVarP(&outputFormat, "output-format", "o", "yaml", "[yaml|y|json|j|props|p|xml|x] output format type.")
rootCmd.PersistentFlags().StringVarP(&inputFormat, "input-format", "p", "yaml", "[yaml|y|props|p|xml|x] parse format for input. Note that json is a subset of yaml.") rootCmd.PersistentFlags().StringVarP(&inputFormat, "input-format", "p", "yaml", "[yaml|y|props|p|xml|x] parse format for input. Note that json is a subset of yaml.")
rootCmd.PersistentFlags().StringVar(&yqlib.ConfiguredXMLPreferences.AttributePrefix, "xml-attribute-prefix", "+", "prefix for xml attributes") rootCmd.PersistentFlags().StringVar(&yqlib.ConfiguredXMLPreferences.AttributePrefix, "xml-attribute-prefix", yqlib.ConfiguredXMLPreferences.AttributePrefix, "prefix for xml attributes")
rootCmd.PersistentFlags().StringVar(&yqlib.ConfiguredXMLPreferences.ContentName, "xml-content-name", "+content", "name for xml content (if no attribute name is present).") rootCmd.PersistentFlags().StringVar(&yqlib.ConfiguredXMLPreferences.ContentName, "xml-content-name", yqlib.ConfiguredXMLPreferences.ContentName, "name for xml content (if no attribute name is present).")
rootCmd.PersistentFlags().BoolVar(&yqlib.ConfiguredXMLPreferences.StrictMode, "xml-strict-mode", false, "enables strict parsing of XML. See https://pkg.go.dev/encoding/xml for more details.") rootCmd.PersistentFlags().BoolVar(&yqlib.ConfiguredXMLPreferences.StrictMode, "xml-strict-mode", yqlib.ConfiguredXMLPreferences.StrictMode, "enables strict parsing of XML. See https://pkg.go.dev/encoding/xml for more details.")
rootCmd.PersistentFlags().BoolVar(&yqlib.ConfiguredXMLPreferences.KeepNamespace, "xml-keep-namespace", true, "enables keeping namespace after parsing attributes") rootCmd.PersistentFlags().BoolVar(&yqlib.ConfiguredXMLPreferences.KeepNamespace, "xml-keep-namespace", yqlib.ConfiguredXMLPreferences.KeepNamespace, "enables keeping namespace after parsing attributes")
rootCmd.PersistentFlags().BoolVar(&yqlib.ConfiguredXMLPreferences.UseRawToken, "xml-raw-token", true, "enables using RawToken method instead Token. Commonly disables namespace translations. See https://pkg.go.dev/encoding/xml#Decoder.RawToken for details.") rootCmd.PersistentFlags().BoolVar(&yqlib.ConfiguredXMLPreferences.UseRawToken, "xml-raw-token", yqlib.ConfiguredXMLPreferences.UseRawToken, "enables using RawToken method instead Token. Commonly disables namespace translations. See https://pkg.go.dev/encoding/xml#Decoder.RawToken for details.")
rootCmd.PersistentFlags().StringVar(&yqlib.ConfiguredXMLPreferences.ProcInstPrefix, "xml-proc-inst-prefix", "+p_", "prefix for xml processing instructions (e.g. <?xml version=\"1\"?>)") rootCmd.PersistentFlags().StringVar(&yqlib.ConfiguredXMLPreferences.ProcInstPrefix, "xml-proc-inst-prefix", yqlib.ConfiguredXMLPreferences.ProcInstPrefix, "prefix for xml processing instructions (e.g. <?xml version=\"1\"?>)")
rootCmd.PersistentFlags().StringVar(&yqlib.ConfiguredXMLPreferences.DirectiveName, "xml-directive-name", "+directive", "name for xml directives (e.g. <!DOCTYPE thing cat>)") rootCmd.PersistentFlags().StringVar(&yqlib.ConfiguredXMLPreferences.DirectiveName, "xml-directive-name", yqlib.ConfiguredXMLPreferences.DirectiveName, "name for xml directives (e.g. <!DOCTYPE thing cat>)")
rootCmd.PersistentFlags().BoolVar(&yqlib.ConfiguredXMLPreferences.SkipProcInst, "xml-skip-proc-inst", false, "skip over process instructions (e.g. <?xml version=\"1\"?>)") rootCmd.PersistentFlags().BoolVar(&yqlib.ConfiguredXMLPreferences.SkipProcInst, "xml-skip-proc-inst", yqlib.ConfiguredXMLPreferences.SkipProcInst, "skip over process instructions (e.g. <?xml version=\"1\"?>)")
rootCmd.PersistentFlags().BoolVar(&yqlib.ConfiguredXMLPreferences.SkipDirectives, "xml-skip-directives", false, "skip over directives (e.g. <!DOCTYPE thing cat>)") rootCmd.PersistentFlags().BoolVar(&yqlib.ConfiguredXMLPreferences.SkipDirectives, "xml-skip-directives", yqlib.ConfiguredXMLPreferences.SkipDirectives, "skip over directives (e.g. <!DOCTYPE thing cat>)")
rootCmd.PersistentFlags().BoolVarP(&nullInput, "null-input", "n", false, "Don't read input, simply evaluate the expression given. Useful for creating docs from scratch.") rootCmd.PersistentFlags().BoolVarP(&nullInput, "null-input", "n", false, "Don't read input, simply evaluate the expression given. Useful for creating docs from scratch.")
rootCmd.PersistentFlags().BoolVarP(&noDocSeparators, "no-doc", "N", false, "Don't print document separators (---)") rootCmd.PersistentFlags().BoolVarP(&noDocSeparators, "no-doc", "N", false, "Don't print document separators (---)")

View File

@ -258,55 +258,55 @@ cat:
``` ```
## Parse xml: keep attribute namespace ## Parse xml: keep attribute namespace
Defaults to true
Given a sample.xml file of: Given a sample.xml file of:
```xml ```xml
<?xml version="1.0"?> <?xml version="1.0"?>
<map xmlns="some-namespace" xmlns:xsi="some-instance" xsi:schemaLocation="some-url"> <map xmlns="some-namespace" xmlns:xsi="some-instance" xsi:schemaLocation="some-url"></map>
</map>
``` ```
then then
```bash ```bash
yq -p=xml -o=xml --xml-keep-namespace '.' sample.xml yq -p=xml -o=xml --xml-keep-namespace=false '.' sample.xml
``` ```
will output will output
```xml ```xml
<?xml version="1.0"?> <?xml version="1.0"?>
<map xmlns="some-namespace" xmlns:xsi="some-instance" some-instance:schemaLocation="some-url"></map>
```
instead of
```xml
<?xml version="1.0"?>
<map xmlns="some-namespace" xmlns:xsi="some-instance" some-instance:schemaLocation="some-url"></map>
```
## Parse xml: keep raw attribute namespace
Given a sample.xml file of:
```xml
<?xml version="1.0"?>
<map xmlns="some-namespace" xmlns:xsi="some-instance" xsi:schemaLocation="some-url">
</map>
```
then
```bash
yq -p=xml -o=xml --xml-keep-namespace --xml-raw-token '.' sample.xml
```
will output
```xml
<?xml version="1.0"?>
<map xmlns="some-namespace" xmlns:xsi="some-instance" some-instance:schemaLocation="some-url"></map>
```
instead of
```xml
<?xml version="1.0"?>
<map xmlns="some-namespace" xsi="some-instance" schemaLocation="some-url"></map> <map xmlns="some-namespace" xsi="some-instance" schemaLocation="some-url"></map>
``` ```
instead of
```xml
<?xml version="1.0"?>
<map xmlns="some-namespace" xmlns:xsi="some-instance" xsi:schemaLocation="some-url"></map>
```
## Parse xml: keep raw attribute namespace
Defaults to true
Given a sample.xml file of:
```xml
<?xml version="1.0"?>
<map xmlns="some-namespace" xmlns:xsi="some-instance" xsi:schemaLocation="some-url"></map>
```
then
```bash
yq -p=xml -o=xml --xml-raw-token=false '.' sample.xml
```
will output
```xml
<?xml version="1.0"?>
<map xmlns="some-namespace" xmlns:xsi="some-instance" some-instance:schemaLocation="some-url"></map>
```
instead of
```xml
<?xml version="1.0"?>
<map xmlns="some-namespace" xmlns:xsi="some-instance" xsi:schemaLocation="some-url"></map>
```
## Encode xml: simple ## Encode xml: simple
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml

View File

@ -18,7 +18,7 @@ func NewDefaultXmlPreferences() XmlPreferences {
ContentName: "+content", ContentName: "+content",
StrictMode: false, StrictMode: false,
KeepNamespace: true, KeepNamespace: true,
UseRawToken: false, UseRawToken: true,
ProcInstPrefix: "+p_", ProcInstPrefix: "+p_",
DirectiveName: "+directive", DirectiveName: "+directive",
SkipProcInst: false, SkipProcInst: false,

View File

@ -157,17 +157,15 @@ const expectedXMLWithComments = `<!--
</cat><!-- below_cat --> </cat><!-- below_cat -->
` `
const inputXMLWithNamespacedAttr = ` const inputXMLWithNamespacedAttr = `<?xml version="1.0"?>
<?xml version="1.0"?> <map xmlns="some-namespace" xmlns:xsi="some-instance" xsi:schemaLocation="some-url"></map>
<map xmlns="some-namespace" xmlns:xsi="some-instance" xsi:schemaLocation="some-url">
</map>
` `
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 +@xsi:schemaLocation: some-url
` `
const expectedYAMLWithRawNamespacedAttr = `+p_xml: version="1.0" const expectedYAMLWithRawNamespacedAttr = `+p_xml: version="1.0"
@ -177,6 +175,13 @@ map:
+@xsi:schemaLocation: some-url +@xsi:schemaLocation: some-url
` `
const expectedYAMLWithoutRawNamespacedAttr = `+p_xml: version="1.0"
map:
+@xmlns: some-namespace
+@xmlns:xsi: some-instance
+@some-instance:schemaLocation: some-url
`
const xmlWithCustomDtd = ` const xmlWithCustomDtd = `
<?xml version="1.0"?> <?xml version="1.0"?>
<!DOCTYPE root [ <!DOCTYPE root [
@ -266,6 +271,13 @@ var xmlScenarios = []formatScenario{
expected: expectedDtd, expected: expectedDtd,
scenarioType: "roundtrip", scenarioType: "roundtrip",
}, },
{
description: "Roundtrip with name spaced attributes",
skipDoc: true,
input: inputXMLWithNamespacedAttr,
expected: inputXMLWithNamespacedAttr,
scenarioType: "roundtrip",
},
{ {
description: "Parse xml: skip custom dtd", description: "Parse xml: skip custom dtd",
subdescription: "DTDs are directives, skip over directives to skip DTDs.", subdescription: "DTDs are directives, skip over directives to skip DTDs.",
@ -323,19 +335,28 @@ var xmlScenarios = []formatScenario{
scenarioType: "decode", scenarioType: "decode",
}, },
{ {
description: "Parse xml: keep attribute namespace", description: "Parse xml: keep attribute namespace",
skipDoc: false, subdescription: fmt.Sprintf(`Defaults to %v`, ConfiguredXMLPreferences.KeepNamespace),
input: inputXMLWithNamespacedAttr, skipDoc: false,
expected: expectedYAMLWithNamespacedAttr, input: inputXMLWithNamespacedAttr,
scenarioType: "decode-keep-ns", expected: expectedYAMLWithNamespacedAttr,
scenarioType: "decode-keep-ns",
}, },
{ {
description: "Parse xml: keep raw attribute namespace", description: "Parse xml: keep raw attribute namespace",
skipDoc: false, skipDoc: true,
input: inputXMLWithNamespacedAttr, input: inputXMLWithNamespacedAttr,
expected: expectedYAMLWithRawNamespacedAttr, expected: expectedYAMLWithRawNamespacedAttr,
scenarioType: "decode-raw-token", scenarioType: "decode-raw-token",
}, },
{
description: "Parse xml: keep raw attribute namespace",
subdescription: fmt.Sprintf(`Defaults to %v`, ConfiguredXMLPreferences.UseRawToken),
skipDoc: false,
input: inputXMLWithNamespacedAttr,
expected: expectedYAMLWithoutRawNamespacedAttr,
scenarioType: "decode-raw-token-off",
},
{ {
description: "Encode xml: simple", description: "Encode xml: simple",
input: "cat: purrs", input: "cat: purrs",
@ -452,6 +473,10 @@ func testXMLScenario(t *testing.T, s formatScenario) {
prefs := NewDefaultXmlPreferences() prefs := NewDefaultXmlPreferences()
prefs.UseRawToken = true prefs.UseRawToken = true
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewXMLDecoder(prefs), NewYamlEncoder(2, false, ConfiguredYamlPreferences)), s.description) test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewXMLDecoder(prefs), NewYamlEncoder(2, false, ConfiguredYamlPreferences)), s.description)
case "decode-raw-token-off":
prefs := NewDefaultXmlPreferences()
prefs.UseRawToken = false
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewXMLDecoder(prefs), NewYamlEncoder(2, false, ConfiguredYamlPreferences)), s.description)
case "roundtrip-skip-directives": case "roundtrip-skip-directives":
prefs := NewDefaultXmlPreferences() prefs := NewDefaultXmlPreferences()
prefs.SkipDirectives = true prefs.SkipDirectives = true
@ -484,7 +509,7 @@ func documentXMLScenario(t *testing.T, w *bufio.Writer, i interface{}) {
documentXMLRoundTripScenario(w, s) documentXMLRoundTripScenario(w, s)
case "decode-keep-ns": case "decode-keep-ns":
documentXMLDecodeKeepNsScenario(w, s) documentXMLDecodeKeepNsScenario(w, s)
case "decode-raw-token": case "decode-raw-token-off":
documentXMLDecodeKeepNsRawTokenScenario(w, s) documentXMLDecodeKeepNsRawTokenScenario(w, s)
case "roundtrip-skip-directives": case "roundtrip-skip-directives":
documentXMLSkipDirectrivesScenario(w, s) documentXMLSkipDirectrivesScenario(w, s)
@ -528,14 +553,14 @@ func documentXMLDecodeKeepNsScenario(w *bufio.Writer, s formatScenario) {
writeOrPanic(w, fmt.Sprintf("```xml\n%v\n```\n", s.input)) writeOrPanic(w, fmt.Sprintf("```xml\n%v\n```\n", s.input))
writeOrPanic(w, "then\n") writeOrPanic(w, "then\n")
writeOrPanic(w, "```bash\nyq -p=xml -o=xml --xml-keep-namespace '.' sample.xml\n```\n") writeOrPanic(w, "```bash\nyq -p=xml -o=xml --xml-keep-namespace=false '.' sample.xml\n```\n")
writeOrPanic(w, "will output\n") writeOrPanic(w, "will output\n")
prefs := NewDefaultXmlPreferences() prefs := NewDefaultXmlPreferences()
prefs.KeepNamespace = true prefs.KeepNamespace = false
writeOrPanic(w, fmt.Sprintf("```xml\n%v```\n\n", mustProcessFormatScenario(s, NewXMLDecoder(prefs), NewXMLEncoder(2, prefs)))) writeOrPanic(w, fmt.Sprintf("```xml\n%v```\n\n", mustProcessFormatScenario(s, NewXMLDecoder(prefs), NewXMLEncoder(2, prefs))))
prefsWithout := NewDefaultXmlPreferences() prefsWithout := NewDefaultXmlPreferences()
prefs.KeepNamespace = false prefs.KeepNamespace = true
writeOrPanic(w, "instead of\n") writeOrPanic(w, "instead of\n")
writeOrPanic(w, fmt.Sprintf("```xml\n%v```\n\n", mustProcessFormatScenario(s, NewXMLDecoder(prefsWithout), NewXMLEncoder(2, prefsWithout)))) writeOrPanic(w, fmt.Sprintf("```xml\n%v```\n\n", mustProcessFormatScenario(s, NewXMLDecoder(prefsWithout), NewXMLEncoder(2, prefsWithout))))
} }
@ -552,16 +577,16 @@ func documentXMLDecodeKeepNsRawTokenScenario(w *bufio.Writer, s formatScenario)
writeOrPanic(w, fmt.Sprintf("```xml\n%v\n```\n", s.input)) writeOrPanic(w, fmt.Sprintf("```xml\n%v\n```\n", s.input))
writeOrPanic(w, "then\n") writeOrPanic(w, "then\n")
writeOrPanic(w, "```bash\nyq -p=xml -o=xml --xml-keep-namespace --xml-raw-token '.' sample.xml\n```\n") writeOrPanic(w, "```bash\nyq -p=xml -o=xml --xml-raw-token=false '.' sample.xml\n```\n")
writeOrPanic(w, "will output\n") writeOrPanic(w, "will output\n")
prefs := NewDefaultXmlPreferences() prefs := NewDefaultXmlPreferences()
prefs.KeepNamespace = true prefs.UseRawToken = false
writeOrPanic(w, fmt.Sprintf("```xml\n%v```\n\n", mustProcessFormatScenario(s, NewXMLDecoder(prefs), NewXMLEncoder(2, prefs)))) writeOrPanic(w, fmt.Sprintf("```xml\n%v```\n\n", mustProcessFormatScenario(s, NewXMLDecoder(prefs), NewXMLEncoder(2, prefs))))
prefsWithout := NewDefaultXmlPreferences() prefsWithout := NewDefaultXmlPreferences()
prefsWithout.KeepNamespace = false prefsWithout.UseRawToken = true
writeOrPanic(w, "instead of\n") writeOrPanic(w, "instead of\n")
writeOrPanic(w, fmt.Sprintf("```xml\n%v```\n\n", mustProcessFormatScenario(s, NewXMLDecoder(prefsWithout), NewXMLEncoder(2, prefsWithout)))) writeOrPanic(w, fmt.Sprintf("```xml\n%v```\n\n", mustProcessFormatScenario(s, NewXMLDecoder(prefsWithout), NewXMLEncoder(2, prefsWithout))))

View File

@ -1,3 +1,6 @@
4.30.2:
- Actually updated the default xml prefix :facepalm:
4.30.1: 4.30.1:
- XML users note: the default attribute prefix has change to `+@` to avoid naming conflicts! - 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