Skip to content

Commit 7a01e21

Browse files
committed
Refactoring Yaml encoder prefs
1 parent f44d47a commit 7a01e21

21 files changed

+103
-86
lines changed

cmd/utils.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,6 @@ func initCommand(cmd *cobra.Command, args []string) (string, []string, error) {
109109
unwrapScalar = unwrapScalarFlag.IsSet()
110110
}
111111

112-
//copy preference form global setting
113-
yqlib.ConfiguredYamlPreferences.UnwrapScalar = unwrapScalar
114-
115-
yqlib.ConfiguredYamlPreferences.PrintDocSeparators = !noDocSeparators
116-
117112
return expression, args, nil
118113
}
119114

@@ -184,8 +179,15 @@ func configureEncoder() (yqlib.Encoder, error) {
184179

185180
func createEncoder(format *yqlib.PrinterOutputFormat) (yqlib.Encoder, error) {
186181
yqlib.ConfiguredXMLPreferences.Indent = indent
182+
yqlib.ConfiguredYamlPreferences.Indent = indent
183+
184+
yqlib.ConfiguredYamlPreferences.UnwrapScalar = unwrapScalar
187185
yqlib.ConfiguredPropertiesPreferences.UnwrapScalar = unwrapScalar
188186

187+
yqlib.ConfiguredYamlPreferences.ColorsEnabled = colorsEnabled
188+
189+
yqlib.ConfiguredYamlPreferences.PrintDocSeparators = !noDocSeparators
190+
189191
switch format {
190192
case yqlib.JSONOutputFormat:
191193
return yqlib.NewJSONEncoder(indent, colorsEnabled, unwrapScalar), nil
@@ -196,7 +198,7 @@ func createEncoder(format *yqlib.PrinterOutputFormat) (yqlib.Encoder, error) {
196198
case yqlib.TSVOutputFormat:
197199
return yqlib.NewCsvEncoder(yqlib.ConfiguredTsvPreferences), nil
198200
case yqlib.YamlOutputFormat:
199-
return yqlib.NewYamlEncoder(indent, colorsEnabled, yqlib.ConfiguredYamlPreferences), nil
201+
return yqlib.NewYamlEncoder(yqlib.ConfiguredYamlPreferences), nil
200202
case yqlib.XMLOutputFormat:
201203
return yqlib.NewXMLEncoder(yqlib.ConfiguredXMLPreferences), nil
202204
case yqlib.TomlOutputFormat:

pkg/yqlib/all_at_once_evaluator_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func TestAllAtOnceEvaluateNodes(t *testing.T) {
3636
var evaluator = NewAllAtOnceEvaluator()
3737
// logging.SetLevel(logging.DEBUG, "")
3838
for _, tt := range evaluateNodesScenario {
39-
decoder := NewYamlDecoder(NewDefaultYamlPreferences())
39+
decoder := NewYamlDecoder(ConfiguredYamlPreferences)
4040
reader := bufio.NewReader(strings.NewReader(tt.document))
4141
err := decoder.Init(reader)
4242
if err != nil {

pkg/yqlib/csv_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,11 @@ func testCSVScenario(t *testing.T, s formatScenario) {
210210
case "encode-tsv":
211211
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewCsvEncoder(ConfiguredTsvPreferences)), s.description)
212212
case "decode-csv":
213-
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewCSVObjectDecoder(ConfiguredCsvPreferences), NewYamlEncoder(2, false, ConfiguredYamlPreferences)), s.description)
213+
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewCSVObjectDecoder(ConfiguredCsvPreferences), NewYamlEncoder(ConfiguredYamlPreferences)), s.description)
214214
case "decode-csv-no-auto":
215-
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewCSVObjectDecoder(CsvPreferences{Separator: ',', AutoParse: false}), NewYamlEncoder(2, false, ConfiguredYamlPreferences)), s.description)
215+
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewCSVObjectDecoder(CsvPreferences{Separator: ',', AutoParse: false}), NewYamlEncoder(ConfiguredYamlPreferences)), s.description)
216216
case "decode-tsv-object":
217-
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewCSVObjectDecoder(ConfiguredTsvPreferences), NewYamlEncoder(2, false, ConfiguredYamlPreferences)), s.description)
217+
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewCSVObjectDecoder(ConfiguredTsvPreferences), NewYamlEncoder(ConfiguredYamlPreferences)), s.description)
218218
case "roundtrip-csv":
219219
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewCSVObjectDecoder(ConfiguredCsvPreferences), NewCsvEncoder(ConfiguredCsvPreferences)), s.description)
220220
default:
@@ -243,7 +243,7 @@ func documentCSVDecodeObjectScenario(w *bufio.Writer, s formatScenario, formatTy
243243
}
244244

245245
writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n",
246-
mustProcessFormatScenario(s, NewCSVObjectDecoder(CsvPreferences{Separator: separator, AutoParse: true}), NewYamlEncoder(s.indent, false, ConfiguredYamlPreferences))),
246+
mustProcessFormatScenario(s, NewCSVObjectDecoder(CsvPreferences{Separator: separator, AutoParse: true}), NewYamlEncoder(ConfiguredYamlPreferences))),
247247
)
248248
}
249249

@@ -268,7 +268,7 @@ func documentCSVDecodeObjectNoAutoScenario(w *bufio.Writer, s formatScenario, fo
268268
}
269269

270270
writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n",
271-
mustProcessFormatScenario(s, NewCSVObjectDecoder(CsvPreferences{Separator: separator, AutoParse: false}), NewYamlEncoder(s.indent, false, ConfiguredYamlPreferences))),
271+
mustProcessFormatScenario(s, NewCSVObjectDecoder(CsvPreferences{Separator: separator, AutoParse: false}), NewYamlEncoder(ConfiguredYamlPreferences))),
272272
)
273273
}
274274

pkg/yqlib/doc/usage/convert.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ will output
3636
```yaml
3737
a: Easy! as one two three
3838
b:
39-
c: 2
40-
d:
41-
- 3
42-
- 4
39+
c: 2
40+
d:
41+
- 3
42+
- 4
4343
```
4444
4545
## Encode json: simple
@@ -244,7 +244,7 @@ will output
244244
this: is a multidoc json file
245245
---
246246
each:
247-
- line is a valid json document
247+
- line is a valid json document
248248
---
249249
a number: 4
250250
```

pkg/yqlib/doc/usage/properties.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -218,16 +218,16 @@ yq -p=props sample.properties
218218
will output
219219
```yaml
220220
person:
221-
# block comments come through
222-
# comments on values appear
223-
name: Mike Wazowski
224-
pets:
225-
# comments on array values appear
226-
- cat
227-
- nested:
228-
- list entry
229-
food:
230-
- pizza
221+
# block comments come through
222+
# comments on values appear
223+
name: Mike Wazowski
224+
pets:
225+
# comments on array values appear
226+
- cat
227+
- nested:
228+
- list entry
229+
food:
230+
- pizza
231231
```
232232
233233
## Decode properties - array should be a map
@@ -244,7 +244,7 @@ yq -p=props '.things |= array_to_map' sample.properties
244244
will output
245245
```yaml
246246
things:
247-
10: mike
247+
10: mike
248248
```
249249
250250
## Roundtrip

pkg/yqlib/encoder_properties_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package yqlib
33
import (
44
"bufio"
55
"bytes"
6-
"fmt"
76
"strings"
87
"testing"
98

@@ -81,8 +80,6 @@ func doTest(t *testing.T, sampleYaml string, props testProperties, testUnwrapped
8180
}
8281

8382
for _, unwrap := range wraps {
84-
fmt.Println(props)
85-
fmt.Println(unwrap)
8683
for _, sep := range []string{" = ", ";", "=", " "} {
8784
var actualProps = yamlToProps(sampleYaml, unwrap, sep)
8885
test.AssertResult(t, props.String(unwrap, sep), actualProps)

pkg/yqlib/encoder_yaml.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,11 @@ import (
1212
)
1313

1414
type yamlEncoder struct {
15-
indent int
16-
colorise bool
17-
prefs YamlPreferences
15+
prefs YamlPreferences
1816
}
1917

20-
func NewYamlEncoder(indent int, colorise bool, prefs YamlPreferences) Encoder {
21-
if indent < 0 {
22-
indent = 0
23-
}
24-
return &yamlEncoder{indent, colorise, prefs}
18+
func NewYamlEncoder(prefs YamlPreferences) Encoder {
19+
return &yamlEncoder{prefs}
2520
}
2621

2722
func (ye *yamlEncoder) CanHandleAliases() bool {
@@ -90,13 +85,13 @@ func (ye *yamlEncoder) Encode(writer io.Writer, node *CandidateNode) error {
9085

9186
destination := writer
9287
tempBuffer := bytes.NewBuffer(nil)
93-
if ye.colorise {
88+
if ye.prefs.ColorsEnabled {
9489
destination = tempBuffer
9590
}
9691

9792
var encoder = yaml.NewEncoder(destination)
9893

99-
encoder.SetIndent(ye.indent)
94+
encoder.SetIndent(ye.prefs.Indent)
10095

10196
target, err := node.MarshalYAML()
10297

@@ -115,7 +110,7 @@ func (ye *yamlEncoder) Encode(writer io.Writer, node *CandidateNode) error {
115110
return err
116111
}
117112

118-
if ye.colorise {
113+
if ye.prefs.ColorsEnabled {
119114
return colorizeAndPrint(tempBuffer.Bytes(), writer)
120115
}
121116
return nil

pkg/yqlib/formatting_expressions_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func documentExpressionScenario(_ *testing.T, w *bufio.Writer, i interface{}) {
6868
writeOrPanic(w, "```bash\nyq --from-file update.yq sample.yml\n```\n")
6969
}
7070
writeOrPanic(w, "will output\n")
71-
encoder := NewYamlEncoder(2, false, ConfiguredYamlPreferences)
71+
encoder := NewYamlEncoder(ConfiguredYamlPreferences)
7272

7373
if s.scenarioType == "shebang-json" {
7474
encoder = NewJSONEncoder(2, false, false)
@@ -80,7 +80,7 @@ func documentExpressionScenario(_ *testing.T, w *bufio.Writer, i interface{}) {
8080
func TestExpressionCommentScenarios(t *testing.T) {
8181
for _, tt := range formattingExpressionScenarios {
8282
test.AssertResultComplexWithContext(t, tt.expected,
83-
mustProcessFormatScenario(tt, NewYamlDecoder(ConfiguredYamlPreferences), NewYamlEncoder(2, false, ConfiguredYamlPreferences)),
83+
mustProcessFormatScenario(tt, NewYamlDecoder(ConfiguredYamlPreferences), NewYamlEncoder(ConfiguredYamlPreferences)),
8484
tt.description)
8585
}
8686
genericScenarios := make([]interface{}, len(formattingExpressionScenarios))

pkg/yqlib/goccy_yaml_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ var goccyYamlFormatScenarios = []formatScenario{
160160
}
161161

162162
func testGoccyYamlScenario(t *testing.T, s formatScenario) {
163-
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewGoccyYAMLDecoder(), NewYamlEncoder(2, false, ConfiguredYamlPreferences)), s.description)
163+
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewGoccyYAMLDecoder(), NewYamlEncoder(ConfiguredYamlPreferences)), s.description)
164164
}
165165

166166
func TestGoccyYmlFormatScenarios(t *testing.T) {

pkg/yqlib/json_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ func documentDecodeNdJsonScenario(w *bufio.Writer, s formatScenario) {
348348

349349
writeOrPanic(w, "will output\n")
350350

351-
writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", mustProcessFormatScenario(s, NewJSONDecoder(), NewYamlEncoder(s.indent, false, ConfiguredYamlPreferences))))
351+
writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", mustProcessFormatScenario(s, NewJSONDecoder(), NewYamlEncoder(ConfiguredYamlPreferences))))
352352
}
353353

354354
func decodeJSON(t *testing.T, jsonString string) *CandidateNode {
@@ -383,7 +383,7 @@ func testJSONScenario(t *testing.T, s formatScenario) {
383383
case "decode":
384384
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewJSONDecoder(), NewJSONEncoder(s.indent, false, false)), s.description)
385385
case "decode-ndjson":
386-
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewJSONDecoder(), NewYamlEncoder(2, false, ConfiguredYamlPreferences)), s.description)
386+
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewJSONDecoder(), NewYamlEncoder(ConfiguredYamlPreferences)), s.description)
387387
case "roundtrip-ndjson":
388388
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewJSONDecoder(), NewJSONEncoder(0, false, false)), s.description)
389389
case "roundtrip-multi":
@@ -416,7 +416,7 @@ func documentJSONDecodeScenario(t *testing.T, w *bufio.Writer, s formatScenario)
416416
writeOrPanic(w, "will output\n")
417417

418418
var output bytes.Buffer
419-
printer := NewSimpleYamlPrinter(bufio.NewWriter(&output), true, false, 2, true)
419+
printer := NewSimpleYamlPrinter(bufio.NewWriter(&output), true, 2, true)
420420

421421
node := decodeJSON(t, s.input)
422422

0 commit comments

Comments
 (0)