Refactoring XML encoder prefs

This commit is contained in:
Mike Farah 2024-02-24 15:03:30 +11:00
parent 571caa696a
commit 2866e90c49
6 changed files with 38 additions and 16 deletions

View File

@ -183,6 +183,8 @@ func configureEncoder() (yqlib.Encoder, error) {
} }
func createEncoder(format *yqlib.PrinterOutputFormat) (yqlib.Encoder, error) { func createEncoder(format *yqlib.PrinterOutputFormat) (yqlib.Encoder, error) {
yqlib.ConfiguredXMLPreferences.Indent = indent
switch format { switch format {
case yqlib.JSONOutputFormat: case yqlib.JSONOutputFormat:
return yqlib.NewJSONEncoder(indent, colorsEnabled, unwrapScalar), nil return yqlib.NewJSONEncoder(indent, colorsEnabled, unwrapScalar), nil
@ -195,7 +197,7 @@ func createEncoder(format *yqlib.PrinterOutputFormat) (yqlib.Encoder, error) {
case yqlib.YamlOutputFormat: case yqlib.YamlOutputFormat:
return yqlib.NewYamlEncoder(indent, colorsEnabled, yqlib.ConfiguredYamlPreferences), nil return yqlib.NewYamlEncoder(indent, colorsEnabled, yqlib.ConfiguredYamlPreferences), nil
case yqlib.XMLOutputFormat: case yqlib.XMLOutputFormat:
return yqlib.NewXMLEncoder(indent, yqlib.ConfiguredXMLPreferences), nil return yqlib.NewXMLEncoder(yqlib.ConfiguredXMLPreferences), nil
case yqlib.TomlOutputFormat: case yqlib.TomlOutputFormat:
return yqlib.NewTomlEncoder(), nil return yqlib.NewTomlEncoder(), nil
case yqlib.ShellVariablesOutputFormat: case yqlib.ShellVariablesOutputFormat:

View File

@ -17,10 +17,10 @@ type xmlEncoder struct {
leadingContent string leadingContent string
} }
func NewXMLEncoder(indent int, prefs XmlPreferences) Encoder { func NewXMLEncoder(prefs XmlPreferences) Encoder {
var indentString = "" var indentString = ""
for index := 0; index < indent; index++ { for index := 0; index < prefs.Indent; index++ {
indentString = indentString + " " indentString = indentString + " "
} }
return &xmlEncoder{indentString, nil, prefs, ""} return &xmlEncoder{indentString, nil, prefs, ""}

View File

@ -10,6 +10,7 @@ import (
) )
func configureEncoder(format *PrinterOutputFormat, indent int) Encoder { func configureEncoder(format *PrinterOutputFormat, indent int) Encoder {
switch format { switch format {
case JSONOutputFormat: case JSONOutputFormat:
return NewJSONEncoder(indent, false, false) return NewJSONEncoder(indent, false, false)
@ -22,7 +23,9 @@ func configureEncoder(format *PrinterOutputFormat, indent int) Encoder {
case YamlOutputFormat: case YamlOutputFormat:
return NewYamlEncoder(indent, false, ConfiguredYamlPreferences) return NewYamlEncoder(indent, false, ConfiguredYamlPreferences)
case XMLOutputFormat: case XMLOutputFormat:
return NewXMLEncoder(indent, ConfiguredXMLPreferences) var xmlPrefs = ConfiguredXMLPreferences.Copy()
xmlPrefs.Indent = indent
return NewXMLEncoder(xmlPrefs)
case Base64OutputFormat: case Base64OutputFormat:
return NewBase64Encoder() return NewBase64Encoder()
case UriOutputFormat: case UriOutputFormat:

View File

@ -32,7 +32,7 @@ type expressionScenario struct {
} }
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
logging.SetLevel(logging.DEBUG, "") logging.SetLevel(logging.ERROR, "")
Now = func() time.Time { Now = func() time.Time {
return time.Date(2021, time.May, 19, 1, 2, 3, 4, time.UTC) return time.Date(2021, time.May, 19, 1, 2, 3, 4, time.UTC)
} }

View File

@ -1,6 +1,7 @@
package yqlib package yqlib
type XmlPreferences struct { type XmlPreferences struct {
Indent int
AttributePrefix string AttributePrefix string
ContentName string ContentName string
StrictMode bool StrictMode bool
@ -14,6 +15,7 @@ type XmlPreferences struct {
func NewDefaultXmlPreferences() XmlPreferences { func NewDefaultXmlPreferences() XmlPreferences {
return XmlPreferences{ return XmlPreferences{
Indent: 2,
AttributePrefix: "+@", AttributePrefix: "+@",
ContentName: "+content", ContentName: "+content",
StrictMode: false, StrictMode: false,
@ -26,4 +28,19 @@ func NewDefaultXmlPreferences() XmlPreferences {
} }
} }
func (p *XmlPreferences) Copy() XmlPreferences {
return XmlPreferences{
Indent: p.Indent,
AttributePrefix: p.AttributePrefix,
ContentName: p.ContentName,
StrictMode: p.StrictMode,
KeepNamespace: p.KeepNamespace,
UseRawToken: p.UseRawToken,
ProcInstPrefix: p.ProcInstPrefix,
DirectiveName: p.DirectiveName,
SkipProcInst: p.SkipProcInst,
SkipDirectives: p.SkipDirectives,
}
}
var ConfiguredXMLPreferences = NewDefaultXmlPreferences() var ConfiguredXMLPreferences = NewDefaultXmlPreferences()

View File

@ -619,9 +619,9 @@ func testXMLScenario(t *testing.T, s formatScenario) {
case "", "decode": case "", "decode":
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewXMLDecoder(ConfiguredXMLPreferences), NewYamlEncoder(4, false, ConfiguredYamlPreferences)), s.description) test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewXMLDecoder(ConfiguredXMLPreferences), NewYamlEncoder(4, false, ConfiguredYamlPreferences)), s.description)
case "encode": case "encode":
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewXMLEncoder(2, ConfiguredXMLPreferences)), s.description) test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewXMLEncoder(ConfiguredXMLPreferences)), s.description)
case "roundtrip": case "roundtrip":
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewXMLDecoder(ConfiguredXMLPreferences), NewXMLEncoder(2, ConfiguredXMLPreferences)), s.description) test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewXMLDecoder(ConfiguredXMLPreferences), NewXMLEncoder(ConfiguredXMLPreferences)), s.description)
case "decode-keep-ns": case "decode-keep-ns":
prefs := NewDefaultXmlPreferences() prefs := NewDefaultXmlPreferences()
prefs.KeepNamespace = true prefs.KeepNamespace = true
@ -637,7 +637,7 @@ func testXMLScenario(t *testing.T, s formatScenario) {
case "roundtrip-skip-directives": case "roundtrip-skip-directives":
prefs := NewDefaultXmlPreferences() prefs := NewDefaultXmlPreferences()
prefs.SkipDirectives = true prefs.SkipDirectives = true
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewXMLDecoder(prefs), NewXMLEncoder(2, prefs)), s.description) test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewXMLDecoder(prefs), NewXMLEncoder(prefs)), s.description)
case "decode-error": case "decode-error":
result, err := processFormatScenario(s, NewXMLDecoder(NewDefaultXmlPreferences()), NewYamlEncoder(2, false, ConfiguredYamlPreferences)) result, err := processFormatScenario(s, NewXMLDecoder(NewDefaultXmlPreferences()), NewYamlEncoder(2, false, ConfiguredYamlPreferences))
if err == nil { if err == nil {
@ -646,7 +646,7 @@ func testXMLScenario(t *testing.T, s formatScenario) {
test.AssertResultComplexWithContext(t, s.expectedError, err.Error(), s.description) test.AssertResultComplexWithContext(t, s.expectedError, err.Error(), s.description)
} }
case "encode-error": case "encode-error":
result, err := processFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewXMLEncoder(2, NewDefaultXmlPreferences())) result, err := processFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewXMLEncoder(NewDefaultXmlPreferences()))
if err == nil { if err == nil {
t.Errorf("Expected error '%v' but it worked: %v", s.expectedError, result) t.Errorf("Expected error '%v' but it worked: %v", s.expectedError, result)
} else { } else {
@ -721,12 +721,12 @@ func documentXMLDecodeKeepNsScenario(w *bufio.Writer, s formatScenario) {
writeOrPanic(w, "will output\n") writeOrPanic(w, "will output\n")
prefs := NewDefaultXmlPreferences() prefs := NewDefaultXmlPreferences()
prefs.KeepNamespace = false 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(prefs))))
prefsWithout := NewDefaultXmlPreferences() prefsWithout := NewDefaultXmlPreferences()
prefs.KeepNamespace = true 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(prefsWithout))))
} }
func documentXMLDecodeKeepNsRawTokenScenario(w *bufio.Writer, s formatScenario) { func documentXMLDecodeKeepNsRawTokenScenario(w *bufio.Writer, s formatScenario) {
@ -747,13 +747,13 @@ func documentXMLDecodeKeepNsRawTokenScenario(w *bufio.Writer, s formatScenario)
prefs := NewDefaultXmlPreferences() prefs := NewDefaultXmlPreferences()
prefs.UseRawToken = false 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(prefs))))
prefsWithout := NewDefaultXmlPreferences() prefsWithout := NewDefaultXmlPreferences()
prefsWithout.UseRawToken = true 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(prefsWithout))))
} }
func documentXMLEncodeScenario(w *bufio.Writer, s formatScenario) { func documentXMLEncodeScenario(w *bufio.Writer, s formatScenario) {
@ -771,7 +771,7 @@ func documentXMLEncodeScenario(w *bufio.Writer, s formatScenario) {
writeOrPanic(w, "```bash\nyq -o=xml sample.yml\n```\n") writeOrPanic(w, "```bash\nyq -o=xml sample.yml\n```\n")
writeOrPanic(w, "will output\n") writeOrPanic(w, "will output\n")
writeOrPanic(w, fmt.Sprintf("```xml\n%v```\n\n", mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewXMLEncoder(2, ConfiguredXMLPreferences)))) writeOrPanic(w, fmt.Sprintf("```xml\n%v```\n\n", mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewXMLEncoder(ConfiguredXMLPreferences))))
} }
func documentXMLRoundTripScenario(w *bufio.Writer, s formatScenario) { func documentXMLRoundTripScenario(w *bufio.Writer, s formatScenario) {
@ -789,7 +789,7 @@ func documentXMLRoundTripScenario(w *bufio.Writer, s formatScenario) {
writeOrPanic(w, "```bash\nyq '.' sample.xml\n```\n") writeOrPanic(w, "```bash\nyq '.' sample.xml\n```\n")
writeOrPanic(w, "will output\n") writeOrPanic(w, "will output\n")
writeOrPanic(w, fmt.Sprintf("```xml\n%v```\n\n", mustProcessFormatScenario(s, NewXMLDecoder(ConfiguredXMLPreferences), NewXMLEncoder(2, ConfiguredXMLPreferences)))) writeOrPanic(w, fmt.Sprintf("```xml\n%v```\n\n", mustProcessFormatScenario(s, NewXMLDecoder(ConfiguredXMLPreferences), NewXMLEncoder(ConfiguredXMLPreferences))))
} }
func documentXMLSkipDirectivesScenario(w *bufio.Writer, s formatScenario) { func documentXMLSkipDirectivesScenario(w *bufio.Writer, s formatScenario) {
@ -809,7 +809,7 @@ func documentXMLSkipDirectivesScenario(w *bufio.Writer, s formatScenario) {
prefs := NewDefaultXmlPreferences() prefs := NewDefaultXmlPreferences()
prefs.SkipDirectives = true prefs.SkipDirectives = true
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(prefs))))
} }
func TestXMLScenarios(t *testing.T) { func TestXMLScenarios(t *testing.T) {