Skip to content

Commit

Permalink
Refactoring encoding configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
mikefarah committed Feb 24, 2024
1 parent 55f6a3a commit 1d371b7
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 44 deletions.
30 changes: 7 additions & 23 deletions cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/yqlib/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/yqlib/formatting_expressions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
2 changes: 1 addition & 1 deletion pkg/yqlib/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ func (p *JsonPreferences) Copy() JsonPreferences {
}
}

var ConfiguredJsonPreferences = NewDefaultJsonPreferences()
var ConfiguredJSONPreferences = NewDefaultJsonPreferences()
6 changes: 3 additions & 3 deletions pkg/yqlib/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion pkg/yqlib/operator_encoder_decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/yqlib/operators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
29 changes: 18 additions & 11 deletions pkg/yqlib/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/yqlib/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 1d371b7

Please sign in to comment.