From ececd00fbd9ed93439f45f49e15856d0f2957019 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Thu, 10 Nov 2022 22:22:55 +1100 Subject: [PATCH] Updated default xml naming prefix --- cmd/root.go | 16 +++++++++---- pkg/yqlib/doc/operators/encode-decode.md | 6 ++--- pkg/yqlib/doc/usage/xml.md | 8 +++---- pkg/yqlib/operator_encoder_decoder_test.go | 6 ++--- pkg/yqlib/xml.go | 2 +- pkg/yqlib/xml_test.go | 28 +++++++++++----------- release_notes.txt | 1 + 7 files changed, 38 insertions(+), 29 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index dbde993e..8b8b4d3f 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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.") } diff --git a/pkg/yqlib/doc/operators/encode-decode.md b/pkg/yqlib/doc/operators/encode-decode.md index 71a1863c..f9e5097d 100644 --- a/pkg/yqlib/doc/operators/encode-decode.md +++ b/pkg/yqlib/doc/operators/encode-decode.md @@ -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 diff --git a/pkg/yqlib/doc/usage/xml.md b/pkg/yqlib/doc/usage/xml.md index a1d535b1..11c4193e 100644 --- a/pkg/yqlib/doc/usage/xml.md +++ b/pkg/yqlib/doc/usage/xml.md @@ -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 ``` diff --git a/pkg/yqlib/operator_encoder_decoder_test.go b/pkg/yqlib/operator_encoder_decoder_test.go index 91057b5e..e3961fbe 100644 --- a/pkg/yqlib/operator_encoder_decoder_test.go +++ b/pkg/yqlib/operator_encoder_decoder_test.go @@ -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)::\n bar\n\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)::bar\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 \n bar\n \n", diff --git a/pkg/yqlib/xml.go b/pkg/yqlib/xml.go index 7903c207..55e384ac 100644 --- a/pkg/yqlib/xml.go +++ b/pkg/yqlib/xml.go @@ -14,7 +14,7 @@ type XmlPreferences struct { func NewDefaultXmlPreferences() XmlPreferences { return XmlPreferences{ - AttributePrefix: "+", + AttributePrefix: "+@", ContentName: "+content", StrictMode: false, KeepNamespace: true, diff --git a/pkg/yqlib/xml_test.go b/pkg/yqlib/xml_test.go index 715c9458..57ba56c1 100644 --- a/pkg/yqlib/xml_test.go +++ b/pkg/yqlib/xml_test.go @@ -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: "\n\n 7\n", - 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: "\nmeow", - 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: "\n true\n\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: "\n true\n\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: "cool\n", scenarioType: "encode", }, diff --git a/release_notes.txt b/release_notes.txt index dfeb0d5b..577748a0 100644 --- a/release_notes.txt +++ b/release_notes.txt @@ -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