Skip to content

Commit 1d371b7

Browse files
committed
Refactoring encoding configuration
1 parent 55f6a3a commit 1d371b7

File tree

9 files changed

+35
-44
lines changed

9 files changed

+35
-44
lines changed

cmd/utils.go

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -180,38 +180,22 @@ func configureEncoder() (yqlib.Encoder, error) {
180180
func createEncoder(format *yqlib.PrinterOutputFormat) (yqlib.Encoder, error) {
181181
yqlib.ConfiguredXMLPreferences.Indent = indent
182182
yqlib.ConfiguredYamlPreferences.Indent = indent
183-
yqlib.ConfiguredJsonPreferences.Indent = indent
183+
yqlib.ConfiguredJSONPreferences.Indent = indent
184184

185185
yqlib.ConfiguredYamlPreferences.UnwrapScalar = unwrapScalar
186186
yqlib.ConfiguredPropertiesPreferences.UnwrapScalar = unwrapScalar
187-
yqlib.ConfiguredJsonPreferences.UnwrapScalar = unwrapScalar
187+
yqlib.ConfiguredJSONPreferences.UnwrapScalar = unwrapScalar
188188

189189
yqlib.ConfiguredYamlPreferences.ColorsEnabled = colorsEnabled
190-
yqlib.ConfiguredJsonPreferences.ColorsEnabled = colorsEnabled
190+
yqlib.ConfiguredJSONPreferences.ColorsEnabled = colorsEnabled
191191

192192
yqlib.ConfiguredYamlPreferences.PrintDocSeparators = !noDocSeparators
193193

194-
switch format {
195-
case yqlib.JSONOutputFormat:
196-
return yqlib.NewJSONEncoder(yqlib.ConfiguredJsonPreferences), nil
197-
case yqlib.PropsOutputFormat:
198-
return yqlib.NewPropertiesEncoder(yqlib.ConfiguredPropertiesPreferences), nil
199-
case yqlib.CSVOutputFormat:
200-
return yqlib.NewCsvEncoder(yqlib.ConfiguredCsvPreferences), nil
201-
case yqlib.TSVOutputFormat:
202-
return yqlib.NewCsvEncoder(yqlib.ConfiguredTsvPreferences), nil
203-
case yqlib.YamlOutputFormat:
204-
return yqlib.NewYamlEncoder(yqlib.ConfiguredYamlPreferences), nil
205-
case yqlib.XMLOutputFormat:
206-
return yqlib.NewXMLEncoder(yqlib.ConfiguredXMLPreferences), nil
207-
case yqlib.TomlOutputFormat:
208-
return yqlib.NewTomlEncoder(), nil
209-
case yqlib.ShellVariablesOutputFormat:
210-
return yqlib.NewShellVariablesEncoder(), nil
211-
case yqlib.LuaOutputFormat:
212-
return yqlib.NewLuaEncoder(yqlib.ConfiguredLuaPreferences), nil
194+
encoder := format.EncoderFactory()
195+
if encoder == nil {
196+
return nil, fmt.Errorf("invalid encoder: %v", format)
213197
}
214-
return nil, fmt.Errorf("invalid encoder: %v", format)
198+
return encoder, nil
215199
}
216200

217201
// this is a hack to enable backwards compatibility with githubactions (which pipe /dev/null into everything)

pkg/yqlib/encoder_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func yamlToJSON(t *testing.T, sampleYaml string, indent int) string {
1616
var output bytes.Buffer
1717
writer := bufio.NewWriter(&output)
1818

19-
prefs := ConfiguredJsonPreferences.Copy()
19+
prefs := ConfiguredJSONPreferences.Copy()
2020
prefs.Indent = indent
2121
prefs.UnwrapScalar = false
2222
var jsonEncoder = NewJSONEncoder(prefs)

pkg/yqlib/formatting_expressions_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func documentExpressionScenario(_ *testing.T, w *bufio.Writer, i interface{}) {
7171
encoder := NewYamlEncoder(ConfiguredYamlPreferences)
7272

7373
if s.scenarioType == "shebang-json" {
74-
encoder = NewJSONEncoder(ConfiguredJsonPreferences)
74+
encoder = NewJSONEncoder(ConfiguredJSONPreferences)
7575
}
7676

7777
writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), encoder)))

pkg/yqlib/json.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ func (p *JsonPreferences) Copy() JsonPreferences {
2222
}
2323
}
2424

25-
var ConfiguredJsonPreferences = NewDefaultJsonPreferences()
25+
var ConfiguredJSONPreferences = NewDefaultJsonPreferences()

pkg/yqlib/json_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ func documentRoundtripNdJsonScenario(w *bufio.Writer, s formatScenario, indent i
328328
}
329329

330330
writeOrPanic(w, "will output\n")
331-
prefs := ConfiguredJsonPreferences.Copy()
331+
prefs := ConfiguredJSONPreferences.Copy()
332332
prefs.Indent = indent
333333
prefs.UnwrapScalar = false
334334

@@ -386,7 +386,7 @@ func decodeJSON(t *testing.T, jsonString string) *CandidateNode {
386386
}
387387

388388
func testJSONScenario(t *testing.T, s formatScenario) {
389-
prefs := ConfiguredJsonPreferences.Copy()
389+
prefs := ConfiguredJSONPreferences.Copy()
390390
prefs.Indent = s.indent
391391
prefs.UnwrapScalar = false
392392
switch s.scenarioType {
@@ -488,7 +488,7 @@ func documentJSONEncodeScenario(w *bufio.Writer, s formatScenario) {
488488
writeOrPanic(w, fmt.Sprintf("```bash\nyq -o=json -I=%v '%v' sample.yml\n```\n", s.indent, expression))
489489
}
490490
writeOrPanic(w, "will output\n")
491-
prefs := ConfiguredJsonPreferences.Copy()
491+
prefs := ConfiguredJSONPreferences.Copy()
492492
prefs.Indent = s.indent
493493
prefs.UnwrapScalar = false
494494

pkg/yqlib/operator_encoder_decoder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func configureEncoder(format *PrinterOutputFormat, indent int) Encoder {
1313

1414
switch format {
1515
case JSONOutputFormat:
16-
prefs := ConfiguredJsonPreferences.Copy()
16+
prefs := ConfiguredJSONPreferences.Copy()
1717
prefs.Indent = indent
1818
prefs.ColorsEnabled = false
1919
prefs.UnwrapScalar = false

pkg/yqlib/operators_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type expressionScenario struct {
3434
func TestMain(m *testing.M) {
3535
logging.SetLevel(logging.ERROR, "")
3636
ConfiguredYamlPreferences.ColorsEnabled = false
37-
ConfiguredJsonPreferences.ColorsEnabled = false
37+
ConfiguredJSONPreferences.ColorsEnabled = false
3838
Now = func() time.Time {
3939
return time.Date(2021, time.May, 19, 1, 2, 3, 4, time.UTC)
4040
}

pkg/yqlib/printer.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,29 @@ type Printer interface {
1818
SetNulSepOutput(nulSepOutput bool)
1919
}
2020

21+
type EncoderFactoryFunction func() Encoder
22+
2123
type PrinterOutputFormat struct {
22-
FormalName string
23-
Names []string
24+
FormalName string
25+
Names []string
26+
EncoderFactory EncoderFactoryFunction
2427
}
2528

26-
var YamlOutputFormat = &PrinterOutputFormat{"yaml", []string{"y", "yml"}}
27-
var JSONOutputFormat = &PrinterOutputFormat{"json", []string{"j"}}
28-
var PropsOutputFormat = &PrinterOutputFormat{"props", []string{"p", "properties"}}
29-
var CSVOutputFormat = &PrinterOutputFormat{"csv", []string{"c"}}
30-
var TSVOutputFormat = &PrinterOutputFormat{"tsv", []string{"t"}}
31-
var XMLOutputFormat = &PrinterOutputFormat{"xml", []string{"x"}}
29+
var YamlOutputFormat = &PrinterOutputFormat{"yaml", []string{"y", "yml"}, func() Encoder { return NewYamlEncoder(ConfiguredYamlPreferences) }}
30+
var JSONOutputFormat = &PrinterOutputFormat{"json", []string{"j"}, func() Encoder { return NewJSONEncoder(ConfiguredJSONPreferences) }}
31+
var PropsOutputFormat = &PrinterOutputFormat{"props", []string{"p", "properties"}, func() Encoder { return NewPropertiesEncoder(ConfiguredPropertiesPreferences) }}
32+
var CSVOutputFormat = &PrinterOutputFormat{"csv", []string{"c"}, func() Encoder { return NewCsvEncoder(ConfiguredCsvPreferences) }}
33+
var TSVOutputFormat = &PrinterOutputFormat{"tsv", []string{"t"}, func() Encoder { return NewCsvEncoder(ConfiguredTsvPreferences) }}
34+
var XMLOutputFormat = &PrinterOutputFormat{"xml", []string{"x"}, func() Encoder { return NewXMLEncoder(ConfiguredXMLPreferences) }}
3235

3336
var Base64OutputFormat = &PrinterOutputFormat{}
3437
var UriOutputFormat = &PrinterOutputFormat{}
3538
var ShOutputFormat = &PrinterOutputFormat{}
3639

37-
var TomlOutputFormat = &PrinterOutputFormat{"toml", []string{}}
38-
var ShellVariablesOutputFormat = &PrinterOutputFormat{"shell", []string{"s", "sh"}}
40+
var TomlOutputFormat = &PrinterOutputFormat{"toml", []string{}, func() Encoder { return NewTomlEncoder() }}
41+
var ShellVariablesOutputFormat = &PrinterOutputFormat{"shell", []string{"s", "sh"}, func() Encoder { return NewShellVariablesEncoder() }}
3942

40-
var LuaOutputFormat = &PrinterOutputFormat{"lua", []string{"l"}}
43+
var LuaOutputFormat = &PrinterOutputFormat{"lua", []string{"l"}, func() Encoder { return NewLuaEncoder(ConfiguredLuaPreferences) }}
4144

4245
var Formats = []*PrinterOutputFormat{
4346
YamlOutputFormat,
@@ -66,6 +69,10 @@ func (f *PrinterOutputFormat) MatchesName(name string) bool {
6669
return false
6770
}
6871

72+
func (f *PrinterOutputFormat) GetConfiguredEncoder() Encoder {
73+
return f.EncoderFactory()
74+
}
75+
6976
func OutputFormatFromString(format string) (*PrinterOutputFormat, error) {
7077
for _, printerFormat := range Formats {
7178
if printerFormat.MatchesName(format) {

pkg/yqlib/printer_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ func TestPrinterMultipleDocsJson(t *testing.T) {
314314
var writer = bufio.NewWriter(&output)
315315
// note printDocSeparators is true, it should still not print document separators
316316
// when outputting JSON.
317-
prefs := ConfiguredJsonPreferences.Copy()
317+
prefs := ConfiguredJSONPreferences.Copy()
318318
prefs.Indent = 0
319319
encoder := NewJSONEncoder(prefs)
320320
if encoder == nil {
@@ -368,7 +368,7 @@ func TestPrinterNulSeparatorWithJson(t *testing.T) {
368368
var writer = bufio.NewWriter(&output)
369369
// note printDocSeparators is true, it should still not print document separators
370370
// when outputting JSON.
371-
prefs := ConfiguredJsonPreferences.Copy()
371+
prefs := ConfiguredJSONPreferences.Copy()
372372
prefs.Indent = 0
373373
encoder := NewJSONEncoder(prefs)
374374
if encoder == nil {

0 commit comments

Comments
 (0)