diff --git a/cmd/utils.go b/cmd/utils.go index c02ba3ca..16eb9e97 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -180,38 +180,22 @@ func configureEncoder() (yqlib.Encoder, error) { func createEncoder(format *yqlib.PrinterOutputFormat) (yqlib.Encoder, error) { yqlib.ConfiguredXMLPreferences.Indent = indent yqlib.ConfiguredYamlPreferences.Indent = indent - yqlib.ConfiguredJsonPreferences.Indent = indent + yqlib.ConfiguredJSONPreferences.Indent = indent yqlib.ConfiguredYamlPreferences.UnwrapScalar = unwrapScalar yqlib.ConfiguredPropertiesPreferences.UnwrapScalar = unwrapScalar - yqlib.ConfiguredJsonPreferences.UnwrapScalar = unwrapScalar + yqlib.ConfiguredJSONPreferences.UnwrapScalar = unwrapScalar yqlib.ConfiguredYamlPreferences.ColorsEnabled = colorsEnabled - yqlib.ConfiguredJsonPreferences.ColorsEnabled = colorsEnabled + yqlib.ConfiguredJSONPreferences.ColorsEnabled = colorsEnabled yqlib.ConfiguredYamlPreferences.PrintDocSeparators = !noDocSeparators - switch format { - case yqlib.JSONOutputFormat: - 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 + encoder := format.EncoderFactory() + if encoder == 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) diff --git a/pkg/yqlib/encoder_test.go b/pkg/yqlib/encoder_test.go index 018fd7fe..10db8c43 100644 --- a/pkg/yqlib/encoder_test.go +++ b/pkg/yqlib/encoder_test.go @@ -16,7 +16,7 @@ func yamlToJSON(t *testing.T, sampleYaml string, indent int) string { var output bytes.Buffer writer := bufio.NewWriter(&output) - prefs := ConfiguredJsonPreferences.Copy() + prefs := ConfiguredJSONPreferences.Copy() prefs.Indent = indent prefs.UnwrapScalar = false var jsonEncoder = NewJSONEncoder(prefs) diff --git a/pkg/yqlib/formatting_expressions_test.go b/pkg/yqlib/formatting_expressions_test.go index 3fa3927d..414e061b 100644 --- a/pkg/yqlib/formatting_expressions_test.go +++ b/pkg/yqlib/formatting_expressions_test.go @@ -71,7 +71,7 @@ func documentExpressionScenario(_ *testing.T, w *bufio.Writer, i interface{}) { encoder := NewYamlEncoder(ConfiguredYamlPreferences) 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))) diff --git a/pkg/yqlib/json.go b/pkg/yqlib/json.go index b108fd6d..fe176f45 100644 --- a/pkg/yqlib/json.go +++ b/pkg/yqlib/json.go @@ -22,4 +22,4 @@ func (p *JsonPreferences) Copy() JsonPreferences { } } -var ConfiguredJsonPreferences = NewDefaultJsonPreferences() +var ConfiguredJSONPreferences = NewDefaultJsonPreferences() diff --git a/pkg/yqlib/json_test.go b/pkg/yqlib/json_test.go index 9579be3e..b8777c97 100644 --- a/pkg/yqlib/json_test.go +++ b/pkg/yqlib/json_test.go @@ -328,7 +328,7 @@ func documentRoundtripNdJsonScenario(w *bufio.Writer, s formatScenario, indent i } writeOrPanic(w, "will output\n") - prefs := ConfiguredJsonPreferences.Copy() + prefs := ConfiguredJSONPreferences.Copy() prefs.Indent = indent prefs.UnwrapScalar = false @@ -386,7 +386,7 @@ func decodeJSON(t *testing.T, jsonString string) *CandidateNode { } func testJSONScenario(t *testing.T, s formatScenario) { - prefs := ConfiguredJsonPreferences.Copy() + prefs := ConfiguredJSONPreferences.Copy() prefs.Indent = s.indent prefs.UnwrapScalar = false 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, "will output\n") - prefs := ConfiguredJsonPreferences.Copy() + prefs := ConfiguredJSONPreferences.Copy() prefs.Indent = s.indent prefs.UnwrapScalar = false diff --git a/pkg/yqlib/operator_encoder_decoder.go b/pkg/yqlib/operator_encoder_decoder.go index 7e048d10..a5e4b4eb 100644 --- a/pkg/yqlib/operator_encoder_decoder.go +++ b/pkg/yqlib/operator_encoder_decoder.go @@ -13,7 +13,7 @@ func configureEncoder(format *PrinterOutputFormat, indent int) Encoder { switch format { case JSONOutputFormat: - prefs := ConfiguredJsonPreferences.Copy() + prefs := ConfiguredJSONPreferences.Copy() prefs.Indent = indent prefs.ColorsEnabled = false prefs.UnwrapScalar = false diff --git a/pkg/yqlib/operators_test.go b/pkg/yqlib/operators_test.go index 17131bde..dd8e3e73 100644 --- a/pkg/yqlib/operators_test.go +++ b/pkg/yqlib/operators_test.go @@ -34,7 +34,7 @@ type expressionScenario struct { func TestMain(m *testing.M) { logging.SetLevel(logging.ERROR, "") ConfiguredYamlPreferences.ColorsEnabled = false - ConfiguredJsonPreferences.ColorsEnabled = false + ConfiguredJSONPreferences.ColorsEnabled = false Now = func() time.Time { return time.Date(2021, time.May, 19, 1, 2, 3, 4, time.UTC) } diff --git a/pkg/yqlib/printer.go b/pkg/yqlib/printer.go index 86b68b11..afa0ef58 100644 --- a/pkg/yqlib/printer.go +++ b/pkg/yqlib/printer.go @@ -18,26 +18,29 @@ type Printer interface { SetNulSepOutput(nulSepOutput bool) } +type EncoderFactoryFunction func() Encoder + type PrinterOutputFormat struct { - FormalName string - Names []string + FormalName string + Names []string + EncoderFactory EncoderFactoryFunction } -var YamlOutputFormat = &PrinterOutputFormat{"yaml", []string{"y", "yml"}} -var JSONOutputFormat = &PrinterOutputFormat{"json", []string{"j"}} -var PropsOutputFormat = &PrinterOutputFormat{"props", []string{"p", "properties"}} -var CSVOutputFormat = &PrinterOutputFormat{"csv", []string{"c"}} -var TSVOutputFormat = &PrinterOutputFormat{"tsv", []string{"t"}} -var XMLOutputFormat = &PrinterOutputFormat{"xml", []string{"x"}} +var YamlOutputFormat = &PrinterOutputFormat{"yaml", []string{"y", "yml"}, func() Encoder { return NewYamlEncoder(ConfiguredYamlPreferences) }} +var JSONOutputFormat = &PrinterOutputFormat{"json", []string{"j"}, func() Encoder { return NewJSONEncoder(ConfiguredJSONPreferences) }} +var PropsOutputFormat = &PrinterOutputFormat{"props", []string{"p", "properties"}, func() Encoder { return NewPropertiesEncoder(ConfiguredPropertiesPreferences) }} +var CSVOutputFormat = &PrinterOutputFormat{"csv", []string{"c"}, func() Encoder { return NewCsvEncoder(ConfiguredCsvPreferences) }} +var TSVOutputFormat = &PrinterOutputFormat{"tsv", []string{"t"}, func() Encoder { return NewCsvEncoder(ConfiguredTsvPreferences) }} +var XMLOutputFormat = &PrinterOutputFormat{"xml", []string{"x"}, func() Encoder { return NewXMLEncoder(ConfiguredXMLPreferences) }} var Base64OutputFormat = &PrinterOutputFormat{} var UriOutputFormat = &PrinterOutputFormat{} var ShOutputFormat = &PrinterOutputFormat{} -var TomlOutputFormat = &PrinterOutputFormat{"toml", []string{}} -var ShellVariablesOutputFormat = &PrinterOutputFormat{"shell", []string{"s", "sh"}} +var TomlOutputFormat = &PrinterOutputFormat{"toml", []string{}, func() Encoder { return NewTomlEncoder() }} +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{ YamlOutputFormat, @@ -66,6 +69,10 @@ func (f *PrinterOutputFormat) MatchesName(name string) bool { return false } +func (f *PrinterOutputFormat) GetConfiguredEncoder() Encoder { + return f.EncoderFactory() +} + func OutputFormatFromString(format string) (*PrinterOutputFormat, error) { for _, printerFormat := range Formats { if printerFormat.MatchesName(format) { diff --git a/pkg/yqlib/printer_test.go b/pkg/yqlib/printer_test.go index 899e1627..19ddd5df 100644 --- a/pkg/yqlib/printer_test.go +++ b/pkg/yqlib/printer_test.go @@ -314,7 +314,7 @@ func TestPrinterMultipleDocsJson(t *testing.T) { var writer = bufio.NewWriter(&output) // note printDocSeparators is true, it should still not print document separators // when outputting JSON. - prefs := ConfiguredJsonPreferences.Copy() + prefs := ConfiguredJSONPreferences.Copy() prefs.Indent = 0 encoder := NewJSONEncoder(prefs) if encoder == nil { @@ -368,7 +368,7 @@ func TestPrinterNulSeparatorWithJson(t *testing.T) { var writer = bufio.NewWriter(&output) // note printDocSeparators is true, it should still not print document separators // when outputting JSON. - prefs := ConfiguredJsonPreferences.Copy() + prefs := ConfiguredJSONPreferences.Copy() prefs.Indent = 0 encoder := NewJSONEncoder(prefs) if encoder == nil {