Skip to content

Commit 55f6a3a

Browse files
committed
Refactoring JSON encoder prefs
1 parent 7a01e21 commit 55f6a3a

File tree

9 files changed

+76
-21
lines changed

9 files changed

+76
-21
lines changed

cmd/utils.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,17 +180,20 @@ 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
183184

184185
yqlib.ConfiguredYamlPreferences.UnwrapScalar = unwrapScalar
185186
yqlib.ConfiguredPropertiesPreferences.UnwrapScalar = unwrapScalar
187+
yqlib.ConfiguredJsonPreferences.UnwrapScalar = unwrapScalar
186188

187189
yqlib.ConfiguredYamlPreferences.ColorsEnabled = colorsEnabled
190+
yqlib.ConfiguredJsonPreferences.ColorsEnabled = colorsEnabled
188191

189192
yqlib.ConfiguredYamlPreferences.PrintDocSeparators = !noDocSeparators
190193

191194
switch format {
192195
case yqlib.JSONOutputFormat:
193-
return yqlib.NewJSONEncoder(indent, colorsEnabled, unwrapScalar), nil
196+
return yqlib.NewJSONEncoder(yqlib.ConfiguredJsonPreferences), nil
194197
case yqlib.PropsOutputFormat:
195198
return yqlib.NewPropertiesEncoder(yqlib.ConfiguredPropertiesPreferences), nil
196199
case yqlib.CSVOutputFormat:

pkg/yqlib/encoder_json.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,18 @@ import (
1010
)
1111

1212
type jsonEncoder struct {
13+
prefs JsonPreferences
1314
indentString string
14-
colorise bool
15-
UnwrapScalar bool
1615
}
1716

18-
func NewJSONEncoder(indent int, colorise bool, unwrapScalar bool) Encoder {
17+
func NewJSONEncoder(prefs JsonPreferences) Encoder {
1918
var indentString = ""
2019

21-
for index := 0; index < indent; index++ {
20+
for index := 0; index < prefs.Indent; index++ {
2221
indentString = indentString + " "
2322
}
2423

25-
return &jsonEncoder{indentString, colorise, unwrapScalar}
24+
return &jsonEncoder{prefs, indentString}
2625
}
2726

2827
func (je *jsonEncoder) CanHandleAliases() bool {
@@ -41,13 +40,13 @@ func (je *jsonEncoder) Encode(writer io.Writer, node *CandidateNode) error {
4140
log.Debugf("I need to encode %v", NodeToString(node))
4241
log.Debugf("kids %v", len(node.Content))
4342

44-
if node.Kind == ScalarNode && je.UnwrapScalar {
43+
if node.Kind == ScalarNode && je.prefs.UnwrapScalar {
4544
return writeString(writer, node.Value+"\n")
4645
}
4746

4847
destination := writer
4948
tempBuffer := bytes.NewBuffer(nil)
50-
if je.colorise {
49+
if je.prefs.ColorsEnabled {
5150
destination = tempBuffer
5251
}
5352

@@ -59,7 +58,7 @@ func (je *jsonEncoder) Encode(writer io.Writer, node *CandidateNode) error {
5958
if err != nil {
6059
return err
6160
}
62-
if je.colorise {
61+
if je.prefs.ColorsEnabled {
6362
return colorizeAndPrint(tempBuffer.Bytes(), writer)
6463
}
6564
return nil

pkg/yqlib/encoder_test.go

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

19-
var jsonEncoder = NewJSONEncoder(indent, false, false)
19+
prefs := ConfiguredJsonPreferences.Copy()
20+
prefs.Indent = indent
21+
prefs.UnwrapScalar = false
22+
var jsonEncoder = NewJSONEncoder(prefs)
2023
inputs, err := readDocuments(strings.NewReader(sampleYaml), "sample.yml", 0, NewYamlDecoder(ConfiguredYamlPreferences))
2124
if err != nil {
2225
panic(err)

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(2, false, false)
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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package yqlib
2+
3+
type JsonPreferences struct {
4+
Indent int
5+
ColorsEnabled bool
6+
UnwrapScalar bool
7+
}
8+
9+
func NewDefaultJsonPreferences() JsonPreferences {
10+
return JsonPreferences{
11+
Indent: 2,
12+
ColorsEnabled: true,
13+
UnwrapScalar: true,
14+
}
15+
}
16+
17+
func (p *JsonPreferences) Copy() JsonPreferences {
18+
return JsonPreferences{
19+
Indent: p.Indent,
20+
ColorsEnabled: p.ColorsEnabled,
21+
UnwrapScalar: p.UnwrapScalar,
22+
}
23+
}
24+
25+
var ConfiguredJsonPreferences = NewDefaultJsonPreferences()

pkg/yqlib/json_test.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,27 +85,31 @@ var jsonScenarios = []formatScenario{
8585
skipDoc: true,
8686
input: "[]",
8787
scenarioType: "roundtrip-ndjson",
88+
indent: 0,
8889
expected: "[]\n",
8990
},
9091
{
9192
description: "array has scalar",
9293
skipDoc: true,
9394
input: "[3]",
9495
scenarioType: "roundtrip-ndjson",
96+
indent: 0,
9597
expected: "[3]\n",
9698
},
9799
{
98100
description: "array has object",
99101
skipDoc: true,
100102
input: `[{"x": 3}]`,
101103
scenarioType: "roundtrip-ndjson",
104+
indent: 0,
102105
expected: "[{\"x\":3}]\n",
103106
},
104107
{
105108
description: "array null",
106109
skipDoc: true,
107110
input: "[null]",
108111
scenarioType: "roundtrip-ndjson",
112+
indent: 0,
109113
expected: "[null]\n",
110114
},
111115
{
@@ -114,6 +118,7 @@ var jsonScenarios = []formatScenario{
114118
input: "[{}]",
115119
expression: `[.. | type]`,
116120
scenarioType: "roundtrip-ndjson",
121+
indent: 0,
117122
expected: "[\"!!seq\",\"!!map\"]\n",
118123
},
119124
{
@@ -220,6 +225,7 @@ var jsonScenarios = []formatScenario{
220225
input: sampleNdJson,
221226
expected: expectedRoundTripSampleNdJson,
222227
scenarioType: "roundtrip-ndjson",
228+
indent: 0,
223229
},
224230
{
225231
description: "Roundtrip multi-document JSON",
@@ -322,8 +328,11 @@ func documentRoundtripNdJsonScenario(w *bufio.Writer, s formatScenario, indent i
322328
}
323329

324330
writeOrPanic(w, "will output\n")
331+
prefs := ConfiguredJsonPreferences.Copy()
332+
prefs.Indent = indent
333+
prefs.UnwrapScalar = false
325334

326-
writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", mustProcessFormatScenario(s, NewJSONDecoder(), NewJSONEncoder(indent, false, false))))
335+
writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", mustProcessFormatScenario(s, NewJSONDecoder(), NewJSONEncoder(prefs))))
327336
}
328337

329338
func documentDecodeNdJsonScenario(w *bufio.Writer, s formatScenario) {
@@ -377,19 +386,23 @@ func decodeJSON(t *testing.T, jsonString string) *CandidateNode {
377386
}
378387

379388
func testJSONScenario(t *testing.T, s formatScenario) {
389+
prefs := ConfiguredJsonPreferences.Copy()
390+
prefs.Indent = s.indent
391+
prefs.UnwrapScalar = false
380392
switch s.scenarioType {
381393
case "encode":
382-
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewJSONEncoder(s.indent, false, false)), s.description)
394+
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewJSONEncoder(prefs)), s.description)
383395
case "decode":
384-
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewJSONDecoder(), NewJSONEncoder(s.indent, false, false)), s.description)
396+
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewJSONDecoder(), NewJSONEncoder(prefs)), s.description)
385397
case "decode-ndjson":
386398
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewJSONDecoder(), NewYamlEncoder(ConfiguredYamlPreferences)), s.description)
387399
case "roundtrip-ndjson":
388-
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewJSONDecoder(), NewJSONEncoder(0, false, false)), s.description)
400+
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewJSONDecoder(), NewJSONEncoder(prefs)), s.description)
389401
case "roundtrip-multi":
390-
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewJSONDecoder(), NewJSONEncoder(2, false, false)), s.description)
402+
prefs.Indent = 2
403+
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewJSONDecoder(), NewJSONEncoder(prefs)), s.description)
391404
case "decode-error":
392-
result, err := processFormatScenario(s, NewJSONDecoder(), NewJSONEncoder(2, false, false))
405+
result, err := processFormatScenario(s, NewJSONDecoder(), NewJSONEncoder(prefs))
393406
if err == nil {
394407
t.Errorf("Expected error '%v' but it worked: %v", s.expectedError, result)
395408
} else {
@@ -475,8 +488,11 @@ func documentJSONEncodeScenario(w *bufio.Writer, s formatScenario) {
475488
writeOrPanic(w, fmt.Sprintf("```bash\nyq -o=json -I=%v '%v' sample.yml\n```\n", s.indent, expression))
476489
}
477490
writeOrPanic(w, "will output\n")
491+
prefs := ConfiguredJsonPreferences.Copy()
492+
prefs.Indent = s.indent
493+
prefs.UnwrapScalar = false
478494

479-
writeOrPanic(w, fmt.Sprintf("```json\n%v```\n\n", mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewJSONEncoder(s.indent, false, false))))
495+
writeOrPanic(w, fmt.Sprintf("```json\n%v```\n\n", mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewJSONEncoder(prefs))))
480496
}
481497

482498
func TestJSONScenarios(t *testing.T) {

pkg/yqlib/operator_encoder_decoder.go

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

1414
switch format {
1515
case JSONOutputFormat:
16-
return NewJSONEncoder(indent, false, false)
16+
prefs := ConfiguredJsonPreferences.Copy()
17+
prefs.Indent = indent
18+
prefs.ColorsEnabled = false
19+
prefs.UnwrapScalar = false
20+
return NewJSONEncoder(prefs)
1721
case PropsOutputFormat:
1822
return NewPropertiesEncoder(ConfiguredPropertiesPreferences)
1923
case CSVOutputFormat:

pkg/yqlib/operators_test.go

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

pkg/yqlib/printer_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,9 @@ 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-
encoder := NewJSONEncoder(0, false, false)
317+
prefs := ConfiguredJsonPreferences.Copy()
318+
prefs.Indent = 0
319+
encoder := NewJSONEncoder(prefs)
318320
if encoder == nil {
319321
t.Skipf("no support for %s output format", "json")
320322
}
@@ -366,7 +368,9 @@ func TestPrinterNulSeparatorWithJson(t *testing.T) {
366368
var writer = bufio.NewWriter(&output)
367369
// note printDocSeparators is true, it should still not print document separators
368370
// when outputting JSON.
369-
encoder := NewJSONEncoder(0, false, false)
371+
prefs := ConfiguredJsonPreferences.Copy()
372+
prefs.Indent = 0
373+
encoder := NewJSONEncoder(prefs)
370374
if encoder == nil {
371375
t.Skipf("no support for %s output format", "json")
372376
}

0 commit comments

Comments
 (0)