Skip to content

Commit 24bbffd

Browse files
committed
xml prefs should be part of API
1 parent c62e18f commit 24bbffd

File tree

9 files changed

+54
-53
lines changed

9 files changed

+54
-53
lines changed

cmd/root.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ yq -P sample.json
5454
yqlib.InitExpressionParser()
5555
if (inputFormat == "x" || inputFormat == "xml") &&
5656
outputFormat != "x" && outputFormat != "xml" &&
57-
yqlib.XMLPreferences.AttributePrefix == "+" {
57+
yqlib.ConfiguredXMLPreferences.AttributePrefix == "+" {
5858
yqlib.GetLogger().Warning("The default xml-attribute-prefix will change in the v4.30 to `+@` to avoid " +
5959
"naming conflicts with the default content name, directive name and proc inst prefix. If you need to keep " +
6060
"`+` please set that value explicityly with --xml-attribute-prefix.")
@@ -73,15 +73,15 @@ yq -P sample.json
7373
rootCmd.PersistentFlags().StringVarP(&outputFormat, "output-format", "o", "yaml", "[yaml|y|json|j|props|p|xml|x] output format type.")
7474
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.")
7575

76-
rootCmd.PersistentFlags().StringVar(&yqlib.XMLPreferences.AttributePrefix, "xml-attribute-prefix", "+", "prefix for xml attributes")
77-
rootCmd.PersistentFlags().StringVar(&yqlib.XMLPreferences.ContentName, "xml-content-name", "+content", "name for xml content (if no attribute name is present).")
78-
rootCmd.PersistentFlags().BoolVar(&yqlib.XMLPreferences.StrictMode, "xml-strict-mode", false, "enables strict parsing of XML. See https://pkg.go.dev/encoding/xml for more details.")
79-
rootCmd.PersistentFlags().BoolVar(&yqlib.XMLPreferences.KeepNamespace, "xml-keep-namespace", true, "enables keeping namespace after parsing attributes")
80-
rootCmd.PersistentFlags().BoolVar(&yqlib.XMLPreferences.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.")
81-
rootCmd.PersistentFlags().StringVar(&yqlib.XMLPreferences.ProcInstPrefix, "xml-proc-inst-prefix", "+p_", "prefix for xml processing instructions (e.g. <?xml version=\"1\"?>)")
82-
rootCmd.PersistentFlags().StringVar(&yqlib.XMLPreferences.DirectiveName, "xml-directive-name", "+directive", "name for xml directives (e.g. <!DOCTYPE thing cat>)")
83-
rootCmd.PersistentFlags().BoolVar(&yqlib.XMLPreferences.SkipProcInst, "xml-skip-proc-inst", false, "skip over process instructions (e.g. <?xml version=\"1\"?>)")
84-
rootCmd.PersistentFlags().BoolVar(&yqlib.XMLPreferences.SkipDirectives, "xml-skip-directives", false, "skip over directives (e.g. <!DOCTYPE thing cat>)")
76+
rootCmd.PersistentFlags().StringVar(&yqlib.ConfiguredXMLPreferences.AttributePrefix, "xml-attribute-prefix", "+", "prefix for xml attributes")
77+
rootCmd.PersistentFlags().StringVar(&yqlib.ConfiguredXMLPreferences.ContentName, "xml-content-name", "+content", "name for xml content (if no attribute name is present).")
78+
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.")
79+
rootCmd.PersistentFlags().BoolVar(&yqlib.ConfiguredXMLPreferences.KeepNamespace, "xml-keep-namespace", true, "enables keeping namespace after parsing attributes")
80+
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.")
81+
rootCmd.PersistentFlags().StringVar(&yqlib.ConfiguredXMLPreferences.ProcInstPrefix, "xml-proc-inst-prefix", "+p_", "prefix for xml processing instructions (e.g. <?xml version=\"1\"?>)")
82+
rootCmd.PersistentFlags().StringVar(&yqlib.ConfiguredXMLPreferences.DirectiveName, "xml-directive-name", "+directive", "name for xml directives (e.g. <!DOCTYPE thing cat>)")
83+
rootCmd.PersistentFlags().BoolVar(&yqlib.ConfiguredXMLPreferences.SkipProcInst, "xml-skip-proc-inst", false, "skip over process instructions (e.g. <?xml version=\"1\"?>)")
84+
rootCmd.PersistentFlags().BoolVar(&yqlib.ConfiguredXMLPreferences.SkipDirectives, "xml-skip-directives", false, "skip over directives (e.g. <!DOCTYPE thing cat>)")
8585

8686
rootCmd.PersistentFlags().BoolVarP(&nullInput, "null-input", "n", false, "Don't read input, simply evaluate the expression given. Useful for creating docs from scratch.")
8787
rootCmd.PersistentFlags().BoolVarP(&noDocSeparators, "no-doc", "N", false, "Don't print document separators (---)")

cmd/utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func configureDecoder() (yqlib.Decoder, error) {
6363
}
6464
switch yqlibInputFormat {
6565
case yqlib.XMLInputFormat:
66-
return yqlib.NewXMLDecoder(yqlib.XMLPreferences), nil
66+
return yqlib.NewXMLDecoder(yqlib.ConfiguredXMLPreferences), nil
6767
case yqlib.PropertiesInputFormat:
6868
return yqlib.NewPropertiesDecoder(), nil
6969
case yqlib.JsonInputFormat:
@@ -107,7 +107,7 @@ func configureEncoder(format yqlib.PrinterOutputFormat) yqlib.Encoder {
107107
case yqlib.YamlOutputFormat:
108108
return yqlib.NewYamlEncoder(indent, colorsEnabled, !noDocSeparators, unwrapScalar)
109109
case yqlib.XMLOutputFormat:
110-
return yqlib.NewXMLEncoder(indent, yqlib.XMLPreferences)
110+
return yqlib.NewXMLEncoder(indent, yqlib.ConfiguredXMLPreferences)
111111
}
112112
panic("invalid encoder")
113113
}

pkg/yqlib/decoder_xml.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ type xmlDecoder struct {
1515
reader io.Reader
1616
readAnything bool
1717
finished bool
18-
prefs xmlPreferences
18+
prefs XmlPreferences
1919
}
2020

21-
func NewXMLDecoder(prefs xmlPreferences) Decoder {
21+
func NewXMLDecoder(prefs XmlPreferences) Decoder {
2222
return &xmlDecoder{
2323
finished: false,
2424
prefs: prefs,

pkg/yqlib/encoder_xml.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import (
1212
type xmlEncoder struct {
1313
indentString string
1414
writer io.Writer
15-
prefs xmlPreferences
15+
prefs XmlPreferences
1616
}
1717

18-
func NewXMLEncoder(indent int, prefs xmlPreferences) Encoder {
18+
func NewXMLEncoder(indent int, prefs XmlPreferences) Encoder {
1919
var indentString = ""
2020

2121
for index := 0; index < indent; index++ {

pkg/yqlib/lexer_participle.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ var participleYqRules = []*participleYqRule{
7676
{"Base64d", `@base64d`, decodeOp(Base64InputFormat), 0},
7777
{"Base64", `@base64`, encodeWithIndent(Base64OutputFormat, 0), 0},
7878

79-
{"LoadXML", `load_?xml|xml_?load`, loadOp(NewXMLDecoder(XMLPreferences), false), 0},
79+
{"LoadXML", `load_?xml|xml_?load`, loadOp(NewXMLDecoder(ConfiguredXMLPreferences), false), 0},
8080

8181
{"LoadBase64", `load_?base64`, loadOp(NewBase64Decoder(), false), 0},
8282

pkg/yqlib/lib.go

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,6 @@ func InitExpressionParser() {
2121
}
2222
}
2323

24-
type xmlPreferences struct {
25-
AttributePrefix string
26-
ContentName string
27-
StrictMode bool
28-
KeepNamespace bool
29-
UseRawToken bool
30-
ProcInstPrefix string
31-
DirectiveName string
32-
SkipProcInst bool
33-
SkipDirectives bool
34-
}
35-
36-
func NewDefaultXmlPreferences() xmlPreferences {
37-
return xmlPreferences{
38-
AttributePrefix: "+",
39-
ContentName: "+content",
40-
StrictMode: false,
41-
KeepNamespace: true,
42-
UseRawToken: false,
43-
ProcInstPrefix: "+p_",
44-
DirectiveName: "+directive",
45-
SkipProcInst: false,
46-
SkipDirectives: false,
47-
}
48-
}
49-
50-
var XMLPreferences = NewDefaultXmlPreferences()
51-
5224
var log = logging.MustGetLogger("yq-lib")
5325

5426
var PrettyPrintExp = `(... | (select(tag != "!!str"), select(tag == "!!str") | select(test("(?i)^(y|yes|n|no|on|off)$") | not)) ) style=""`

pkg/yqlib/operator_encoder_decoder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func configureEncoder(format PrinterOutputFormat, indent int) Encoder {
2323
case YamlOutputFormat:
2424
return NewYamlEncoder(indent, false, true, true)
2525
case XMLOutputFormat:
26-
return NewXMLEncoder(indent, XMLPreferences)
26+
return NewXMLEncoder(indent, ConfiguredXMLPreferences)
2727
case Base64OutputFormat:
2828
return NewBase64Encoder()
2929
}
@@ -104,7 +104,7 @@ func decodeOperator(d *dataTreeNavigator, context Context, expressionNode *Expre
104104
case YamlInputFormat:
105105
decoder = NewYamlDecoder()
106106
case XMLInputFormat:
107-
decoder = NewXMLDecoder(XMLPreferences)
107+
decoder = NewXMLDecoder(ConfiguredXMLPreferences)
108108
case Base64InputFormat:
109109
decoder = NewBase64Decoder()
110110
case PropertiesInputFormat:

pkg/yqlib/xml.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package yqlib
2+
3+
type XmlPreferences struct {
4+
AttributePrefix string
5+
ContentName string
6+
StrictMode bool
7+
KeepNamespace bool
8+
UseRawToken bool
9+
ProcInstPrefix string
10+
DirectiveName string
11+
SkipProcInst bool
12+
SkipDirectives bool
13+
}
14+
15+
func NewDefaultXmlPreferences() XmlPreferences {
16+
return XmlPreferences{
17+
AttributePrefix: "+",
18+
ContentName: "+content",
19+
StrictMode: false,
20+
KeepNamespace: true,
21+
UseRawToken: false,
22+
ProcInstPrefix: "+p_",
23+
DirectiveName: "+directive",
24+
SkipProcInst: false,
25+
SkipDirectives: false,
26+
}
27+
}
28+
29+
var ConfiguredXMLPreferences = NewDefaultXmlPreferences()

pkg/yqlib/xml_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -414,11 +414,11 @@ var xmlScenarios = []formatScenario{
414414
func testXMLScenario(t *testing.T, s formatScenario) {
415415
switch s.scenarioType {
416416
case "", "decode":
417-
test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewXMLDecoder(XMLPreferences), NewYamlEncoder(4, false, true, true)), s.description)
417+
test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewXMLDecoder(ConfiguredXMLPreferences), NewYamlEncoder(4, false, true, true)), s.description)
418418
case "encode":
419-
test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewYamlDecoder(), NewXMLEncoder(2, XMLPreferences)), s.description)
419+
test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewYamlDecoder(), NewXMLEncoder(2, ConfiguredXMLPreferences)), s.description)
420420
case "roundtrip":
421-
test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewXMLDecoder(XMLPreferences), NewXMLEncoder(2, XMLPreferences)), s.description)
421+
test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewXMLDecoder(ConfiguredXMLPreferences), NewXMLEncoder(2, ConfiguredXMLPreferences)), s.description)
422422
case "decode-keep-ns":
423423
prefs := NewDefaultXmlPreferences()
424424
prefs.KeepNamespace = true
@@ -480,7 +480,7 @@ func documentXMLDecodeScenario(w *bufio.Writer, s formatScenario) {
480480
writeOrPanic(w, fmt.Sprintf("```bash\nyq -p=xml '%v' sample.xml\n```\n", expression))
481481
writeOrPanic(w, "will output\n")
482482

483-
writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", processFormatScenario(s, NewXMLDecoder(XMLPreferences), NewYamlEncoder(2, false, true, true))))
483+
writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", processFormatScenario(s, NewXMLDecoder(ConfiguredXMLPreferences), NewYamlEncoder(2, false, true, true))))
484484
}
485485

486486
func documentXMLDecodeKeepNsScenario(w *bufio.Writer, s formatScenario) {
@@ -549,7 +549,7 @@ func documentXMLEncodeScenario(w *bufio.Writer, s formatScenario) {
549549
writeOrPanic(w, "```bash\nyq -o=xml '.' sample.yml\n```\n")
550550
writeOrPanic(w, "will output\n")
551551

552-
writeOrPanic(w, fmt.Sprintf("```xml\n%v```\n\n", processFormatScenario(s, NewYamlDecoder(), NewXMLEncoder(2, XMLPreferences))))
552+
writeOrPanic(w, fmt.Sprintf("```xml\n%v```\n\n", processFormatScenario(s, NewYamlDecoder(), NewXMLEncoder(2, ConfiguredXMLPreferences))))
553553
}
554554

555555
func documentXMLRoundTripScenario(w *bufio.Writer, s formatScenario) {
@@ -567,7 +567,7 @@ func documentXMLRoundTripScenario(w *bufio.Writer, s formatScenario) {
567567
writeOrPanic(w, "```bash\nyq -p=xml -o=xml '.' sample.xml\n```\n")
568568
writeOrPanic(w, "will output\n")
569569

570-
writeOrPanic(w, fmt.Sprintf("```xml\n%v```\n\n", processFormatScenario(s, NewXMLDecoder(XMLPreferences), NewXMLEncoder(2, XMLPreferences))))
570+
writeOrPanic(w, fmt.Sprintf("```xml\n%v```\n\n", processFormatScenario(s, NewXMLDecoder(ConfiguredXMLPreferences), NewXMLEncoder(2, ConfiguredXMLPreferences))))
571571
}
572572

573573
func documentXMLSkipDirectrivesScenario(w *bufio.Writer, s formatScenario) {

0 commit comments

Comments
 (0)