Refactoring encoding configuration

This commit is contained in:
Mike Farah 2024-02-24 15:59:12 +11:00
parent 55f6a3a49d
commit 1d371b712f
9 changed files with 35 additions and 44 deletions

View File

@ -180,38 +180,22 @@ 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 yqlib.ConfiguredXMLPreferences.Indent = indent
yqlib.ConfiguredYamlPreferences.Indent = indent yqlib.ConfiguredYamlPreferences.Indent = indent
yqlib.ConfiguredJsonPreferences.Indent = indent yqlib.ConfiguredJSONPreferences.Indent = indent
yqlib.ConfiguredYamlPreferences.UnwrapScalar = unwrapScalar yqlib.ConfiguredYamlPreferences.UnwrapScalar = unwrapScalar
yqlib.ConfiguredPropertiesPreferences.UnwrapScalar = unwrapScalar yqlib.ConfiguredPropertiesPreferences.UnwrapScalar = unwrapScalar
yqlib.ConfiguredJsonPreferences.UnwrapScalar = unwrapScalar yqlib.ConfiguredJSONPreferences.UnwrapScalar = unwrapScalar
yqlib.ConfiguredYamlPreferences.ColorsEnabled = colorsEnabled yqlib.ConfiguredYamlPreferences.ColorsEnabled = colorsEnabled
yqlib.ConfiguredJsonPreferences.ColorsEnabled = colorsEnabled yqlib.ConfiguredJSONPreferences.ColorsEnabled = colorsEnabled
yqlib.ConfiguredYamlPreferences.PrintDocSeparators = !noDocSeparators yqlib.ConfiguredYamlPreferences.PrintDocSeparators = !noDocSeparators
switch format { encoder := format.EncoderFactory()
case yqlib.JSONOutputFormat: if encoder == nil {
return yqlib.NewJSONEncoder(yqlib.ConfiguredJsonPreferences), nil
case yqlib.PropsOutputFormat:
return yqlib.NewPropertiesEncoder(yqlib.ConfiguredPropertiesPreferences), nil
case yqlib.CSVOutputFormat:
return yqlib.NewCsvEncoder(yqlib.ConfiguredCsvPreferences), nil
case yqlib.TSVOutputFormat:
return yqlib.NewCsvEncoder(yqlib.ConfiguredTsvPreferences), nil
case yqlib.YamlOutputFormat:
return yqlib.NewYamlEncoder(yqlib.ConfiguredYamlPreferences), nil
case yqlib.XMLOutputFormat:
return yqlib.NewXMLEncoder(yqlib.ConfiguredXMLPreferences), nil
case yqlib.TomlOutputFormat:
return yqlib.NewTomlEncoder(), nil
case yqlib.ShellVariablesOutputFormat:
return yqlib.NewShellVariablesEncoder(), nil
case yqlib.LuaOutputFormat:
return yqlib.NewLuaEncoder(yqlib.ConfiguredLuaPreferences), nil
}
return nil, fmt.Errorf("invalid encoder: %v", format) return nil, fmt.Errorf("invalid encoder: %v", format)
}
return encoder, nil
} }
// this is a hack to enable backwards compatibility with githubactions (which pipe /dev/null into everything) // this is a hack to enable backwards compatibility with githubactions (which pipe /dev/null into everything)

View File

@ -16,7 +16,7 @@ func yamlToJSON(t *testing.T, sampleYaml string, indent int) string {
var output bytes.Buffer var output bytes.Buffer
writer := bufio.NewWriter(&output) writer := bufio.NewWriter(&output)
prefs := ConfiguredJsonPreferences.Copy() prefs := ConfiguredJSONPreferences.Copy()
prefs.Indent = indent prefs.Indent = indent
prefs.UnwrapScalar = false prefs.UnwrapScalar = false
var jsonEncoder = NewJSONEncoder(prefs) var jsonEncoder = NewJSONEncoder(prefs)

View File

@ -71,7 +71,7 @@ func documentExpressionScenario(_ *testing.T, w *bufio.Writer, i interface{}) {
encoder := NewYamlEncoder(ConfiguredYamlPreferences) encoder := NewYamlEncoder(ConfiguredYamlPreferences)
if s.scenarioType == "shebang-json" { if s.scenarioType == "shebang-json" {
encoder = NewJSONEncoder(ConfiguredJsonPreferences) encoder = NewJSONEncoder(ConfiguredJSONPreferences)
} }
writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), encoder))) writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), encoder)))

View File

@ -22,4 +22,4 @@ func (p *JsonPreferences) Copy() JsonPreferences {
} }
} }
var ConfiguredJsonPreferences = NewDefaultJsonPreferences() var ConfiguredJSONPreferences = NewDefaultJsonPreferences()

View File

@ -328,7 +328,7 @@ func documentRoundtripNdJsonScenario(w *bufio.Writer, s formatScenario, indent i
} }
writeOrPanic(w, "will output\n") writeOrPanic(w, "will output\n")
prefs := ConfiguredJsonPreferences.Copy() prefs := ConfiguredJSONPreferences.Copy()
prefs.Indent = indent prefs.Indent = indent
prefs.UnwrapScalar = false prefs.UnwrapScalar = false
@ -386,7 +386,7 @@ func decodeJSON(t *testing.T, jsonString string) *CandidateNode {
} }
func testJSONScenario(t *testing.T, s formatScenario) { func testJSONScenario(t *testing.T, s formatScenario) {
prefs := ConfiguredJsonPreferences.Copy() prefs := ConfiguredJSONPreferences.Copy()
prefs.Indent = s.indent prefs.Indent = s.indent
prefs.UnwrapScalar = false prefs.UnwrapScalar = false
switch s.scenarioType { switch s.scenarioType {
@ -488,7 +488,7 @@ func documentJSONEncodeScenario(w *bufio.Writer, s formatScenario) {
writeOrPanic(w, fmt.Sprintf("```bash\nyq -o=json -I=%v '%v' sample.yml\n```\n", s.indent, expression)) writeOrPanic(w, fmt.Sprintf("```bash\nyq -o=json -I=%v '%v' sample.yml\n```\n", s.indent, expression))
} }
writeOrPanic(w, "will output\n") writeOrPanic(w, "will output\n")
prefs := ConfiguredJsonPreferences.Copy() prefs := ConfiguredJSONPreferences.Copy()
prefs.Indent = s.indent prefs.Indent = s.indent
prefs.UnwrapScalar = false prefs.UnwrapScalar = false

View File

@ -13,7 +13,7 @@ func configureEncoder(format *PrinterOutputFormat, indent int) Encoder {
switch format { switch format {
case JSONOutputFormat: case JSONOutputFormat:
prefs := ConfiguredJsonPreferences.Copy() prefs := ConfiguredJSONPreferences.Copy()
prefs.Indent = indent prefs.Indent = indent
prefs.ColorsEnabled = false prefs.ColorsEnabled = false
prefs.UnwrapScalar = false prefs.UnwrapScalar = false

View File

@ -34,7 +34,7 @@ type expressionScenario struct {
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
logging.SetLevel(logging.ERROR, "") logging.SetLevel(logging.ERROR, "")
ConfiguredYamlPreferences.ColorsEnabled = false ConfiguredYamlPreferences.ColorsEnabled = false
ConfiguredJsonPreferences.ColorsEnabled = false ConfiguredJSONPreferences.ColorsEnabled = false
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

@ -18,26 +18,29 @@ type Printer interface {
SetNulSepOutput(nulSepOutput bool) SetNulSepOutput(nulSepOutput bool)
} }
type EncoderFactoryFunction func() Encoder
type PrinterOutputFormat struct { type PrinterOutputFormat struct {
FormalName string FormalName string
Names []string Names []string
EncoderFactory EncoderFactoryFunction
} }
var YamlOutputFormat = &PrinterOutputFormat{"yaml", []string{"y", "yml"}} var YamlOutputFormat = &PrinterOutputFormat{"yaml", []string{"y", "yml"}, func() Encoder { return NewYamlEncoder(ConfiguredYamlPreferences) }}
var JSONOutputFormat = &PrinterOutputFormat{"json", []string{"j"}} var JSONOutputFormat = &PrinterOutputFormat{"json", []string{"j"}, func() Encoder { return NewJSONEncoder(ConfiguredJSONPreferences) }}
var PropsOutputFormat = &PrinterOutputFormat{"props", []string{"p", "properties"}} var PropsOutputFormat = &PrinterOutputFormat{"props", []string{"p", "properties"}, func() Encoder { return NewPropertiesEncoder(ConfiguredPropertiesPreferences) }}
var CSVOutputFormat = &PrinterOutputFormat{"csv", []string{"c"}} var CSVOutputFormat = &PrinterOutputFormat{"csv", []string{"c"}, func() Encoder { return NewCsvEncoder(ConfiguredCsvPreferences) }}
var TSVOutputFormat = &PrinterOutputFormat{"tsv", []string{"t"}} var TSVOutputFormat = &PrinterOutputFormat{"tsv", []string{"t"}, func() Encoder { return NewCsvEncoder(ConfiguredTsvPreferences) }}
var XMLOutputFormat = &PrinterOutputFormat{"xml", []string{"x"}} var XMLOutputFormat = &PrinterOutputFormat{"xml", []string{"x"}, func() Encoder { return NewXMLEncoder(ConfiguredXMLPreferences) }}
var Base64OutputFormat = &PrinterOutputFormat{} var Base64OutputFormat = &PrinterOutputFormat{}
var UriOutputFormat = &PrinterOutputFormat{} var UriOutputFormat = &PrinterOutputFormat{}
var ShOutputFormat = &PrinterOutputFormat{} var ShOutputFormat = &PrinterOutputFormat{}
var TomlOutputFormat = &PrinterOutputFormat{"toml", []string{}} var TomlOutputFormat = &PrinterOutputFormat{"toml", []string{}, func() Encoder { return NewTomlEncoder() }}
var ShellVariablesOutputFormat = &PrinterOutputFormat{"shell", []string{"s", "sh"}} var ShellVariablesOutputFormat = &PrinterOutputFormat{"shell", []string{"s", "sh"}, func() Encoder { return NewShellVariablesEncoder() }}
var LuaOutputFormat = &PrinterOutputFormat{"lua", []string{"l"}} var LuaOutputFormat = &PrinterOutputFormat{"lua", []string{"l"}, func() Encoder { return NewLuaEncoder(ConfiguredLuaPreferences) }}
var Formats = []*PrinterOutputFormat{ var Formats = []*PrinterOutputFormat{
YamlOutputFormat, YamlOutputFormat,
@ -66,6 +69,10 @@ func (f *PrinterOutputFormat) MatchesName(name string) bool {
return false return false
} }
func (f *PrinterOutputFormat) GetConfiguredEncoder() Encoder {
return f.EncoderFactory()
}
func OutputFormatFromString(format string) (*PrinterOutputFormat, error) { func OutputFormatFromString(format string) (*PrinterOutputFormat, error) {
for _, printerFormat := range Formats { for _, printerFormat := range Formats {
if printerFormat.MatchesName(format) { if printerFormat.MatchesName(format) {

View File

@ -314,7 +314,7 @@ func TestPrinterMultipleDocsJson(t *testing.T) {
var writer = bufio.NewWriter(&output) var writer = bufio.NewWriter(&output)
// note printDocSeparators is true, it should still not print document separators // note printDocSeparators is true, it should still not print document separators
// when outputting JSON. // when outputting JSON.
prefs := ConfiguredJsonPreferences.Copy() prefs := ConfiguredJSONPreferences.Copy()
prefs.Indent = 0 prefs.Indent = 0
encoder := NewJSONEncoder(prefs) encoder := NewJSONEncoder(prefs)
if encoder == nil { if encoder == nil {
@ -368,7 +368,7 @@ func TestPrinterNulSeparatorWithJson(t *testing.T) {
var writer = bufio.NewWriter(&output) var writer = bufio.NewWriter(&output)
// note printDocSeparators is true, it should still not print document separators // note printDocSeparators is true, it should still not print document separators
// when outputting JSON. // when outputting JSON.
prefs := ConfiguredJsonPreferences.Copy() prefs := ConfiguredJSONPreferences.Copy()
prefs.Indent = 0 prefs.Indent = 0
encoder := NewJSONEncoder(prefs) encoder := NewJSONEncoder(prefs)
if encoder == nil { if encoder == nil {