Skip to content

Commit

Permalink
Refactoring Yaml encoder prefs
Browse files Browse the repository at this point in the history
  • Loading branch information
mikefarah committed Feb 24, 2024
1 parent f44d47a commit 7a01e21
Show file tree
Hide file tree
Showing 21 changed files with 103 additions and 86 deletions.
14 changes: 8 additions & 6 deletions cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,6 @@ func initCommand(cmd *cobra.Command, args []string) (string, []string, error) {
unwrapScalar = unwrapScalarFlag.IsSet()
}

//copy preference form global setting
yqlib.ConfiguredYamlPreferences.UnwrapScalar = unwrapScalar

yqlib.ConfiguredYamlPreferences.PrintDocSeparators = !noDocSeparators

return expression, args, nil
}

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

func createEncoder(format *yqlib.PrinterOutputFormat) (yqlib.Encoder, error) {
yqlib.ConfiguredXMLPreferences.Indent = indent
yqlib.ConfiguredYamlPreferences.Indent = indent

yqlib.ConfiguredYamlPreferences.UnwrapScalar = unwrapScalar
yqlib.ConfiguredPropertiesPreferences.UnwrapScalar = unwrapScalar

yqlib.ConfiguredYamlPreferences.ColorsEnabled = colorsEnabled

yqlib.ConfiguredYamlPreferences.PrintDocSeparators = !noDocSeparators

switch format {
case yqlib.JSONOutputFormat:
return yqlib.NewJSONEncoder(indent, colorsEnabled, unwrapScalar), nil
Expand All @@ -196,7 +198,7 @@ func createEncoder(format *yqlib.PrinterOutputFormat) (yqlib.Encoder, error) {
case yqlib.TSVOutputFormat:
return yqlib.NewCsvEncoder(yqlib.ConfiguredTsvPreferences), nil
case yqlib.YamlOutputFormat:
return yqlib.NewYamlEncoder(indent, colorsEnabled, yqlib.ConfiguredYamlPreferences), nil
return yqlib.NewYamlEncoder(yqlib.ConfiguredYamlPreferences), nil
case yqlib.XMLOutputFormat:
return yqlib.NewXMLEncoder(yqlib.ConfiguredXMLPreferences), nil
case yqlib.TomlOutputFormat:
Expand Down
2 changes: 1 addition & 1 deletion pkg/yqlib/all_at_once_evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestAllAtOnceEvaluateNodes(t *testing.T) {
var evaluator = NewAllAtOnceEvaluator()
// logging.SetLevel(logging.DEBUG, "")
for _, tt := range evaluateNodesScenario {
decoder := NewYamlDecoder(NewDefaultYamlPreferences())
decoder := NewYamlDecoder(ConfiguredYamlPreferences)
reader := bufio.NewReader(strings.NewReader(tt.document))
err := decoder.Init(reader)
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions pkg/yqlib/csv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,11 @@ func testCSVScenario(t *testing.T, s formatScenario) {
case "encode-tsv":
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewCsvEncoder(ConfiguredTsvPreferences)), s.description)
case "decode-csv":
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewCSVObjectDecoder(ConfiguredCsvPreferences), NewYamlEncoder(2, false, ConfiguredYamlPreferences)), s.description)
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewCSVObjectDecoder(ConfiguredCsvPreferences), NewYamlEncoder(ConfiguredYamlPreferences)), s.description)
case "decode-csv-no-auto":
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewCSVObjectDecoder(CsvPreferences{Separator: ',', AutoParse: false}), NewYamlEncoder(2, false, ConfiguredYamlPreferences)), s.description)
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewCSVObjectDecoder(CsvPreferences{Separator: ',', AutoParse: false}), NewYamlEncoder(ConfiguredYamlPreferences)), s.description)
case "decode-tsv-object":
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewCSVObjectDecoder(ConfiguredTsvPreferences), NewYamlEncoder(2, false, ConfiguredYamlPreferences)), s.description)
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewCSVObjectDecoder(ConfiguredTsvPreferences), NewYamlEncoder(ConfiguredYamlPreferences)), s.description)
case "roundtrip-csv":
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewCSVObjectDecoder(ConfiguredCsvPreferences), NewCsvEncoder(ConfiguredCsvPreferences)), s.description)
default:
Expand Down Expand Up @@ -243,7 +243,7 @@ func documentCSVDecodeObjectScenario(w *bufio.Writer, s formatScenario, formatTy
}

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

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

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

Expand Down
10 changes: 5 additions & 5 deletions pkg/yqlib/doc/usage/convert.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ will output
```yaml
a: Easy! as one two three
b:
c: 2
d:
- 3
- 4
c: 2
d:
- 3
- 4
```
## Encode json: simple
Expand Down Expand Up @@ -244,7 +244,7 @@ will output
this: is a multidoc json file
---
each:
- line is a valid json document
- line is a valid json document
---
a number: 4
```
Expand Down
22 changes: 11 additions & 11 deletions pkg/yqlib/doc/usage/properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,16 +218,16 @@ yq -p=props sample.properties
will output
```yaml
person:
# block comments come through
# comments on values appear
name: Mike Wazowski
pets:
# comments on array values appear
- cat
- nested:
- list entry
food:
- pizza
# block comments come through
# comments on values appear
name: Mike Wazowski
pets:
# comments on array values appear
- cat
- nested:
- list entry
food:
- pizza
```
## Decode properties - array should be a map
Expand All @@ -244,7 +244,7 @@ yq -p=props '.things |= array_to_map' sample.properties
will output
```yaml
things:
10: mike
10: mike
```
## Roundtrip
Expand Down
3 changes: 0 additions & 3 deletions pkg/yqlib/encoder_properties_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package yqlib
import (
"bufio"
"bytes"
"fmt"
"strings"
"testing"

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

for _, unwrap := range wraps {
fmt.Println(props)
fmt.Println(unwrap)
for _, sep := range []string{" = ", ";", "=", " "} {
var actualProps = yamlToProps(sampleYaml, unwrap, sep)
test.AssertResult(t, props.String(unwrap, sep), actualProps)
Expand Down
17 changes: 6 additions & 11 deletions pkg/yqlib/encoder_yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,11 @@ import (
)

type yamlEncoder struct {
indent int
colorise bool
prefs YamlPreferences
prefs YamlPreferences
}

func NewYamlEncoder(indent int, colorise bool, prefs YamlPreferences) Encoder {
if indent < 0 {
indent = 0
}
return &yamlEncoder{indent, colorise, prefs}
func NewYamlEncoder(prefs YamlPreferences) Encoder {
return &yamlEncoder{prefs}
}

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

destination := writer
tempBuffer := bytes.NewBuffer(nil)
if ye.colorise {
if ye.prefs.ColorsEnabled {
destination = tempBuffer
}

var encoder = yaml.NewEncoder(destination)

encoder.SetIndent(ye.indent)
encoder.SetIndent(ye.prefs.Indent)

target, err := node.MarshalYAML()

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

if ye.colorise {
if ye.prefs.ColorsEnabled {
return colorizeAndPrint(tempBuffer.Bytes(), writer)
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/yqlib/formatting_expressions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func documentExpressionScenario(_ *testing.T, w *bufio.Writer, i interface{}) {
writeOrPanic(w, "```bash\nyq --from-file update.yq sample.yml\n```\n")
}
writeOrPanic(w, "will output\n")
encoder := NewYamlEncoder(2, false, ConfiguredYamlPreferences)
encoder := NewYamlEncoder(ConfiguredYamlPreferences)

if s.scenarioType == "shebang-json" {
encoder = NewJSONEncoder(2, false, false)
Expand All @@ -80,7 +80,7 @@ func documentExpressionScenario(_ *testing.T, w *bufio.Writer, i interface{}) {
func TestExpressionCommentScenarios(t *testing.T) {
for _, tt := range formattingExpressionScenarios {
test.AssertResultComplexWithContext(t, tt.expected,
mustProcessFormatScenario(tt, NewYamlDecoder(ConfiguredYamlPreferences), NewYamlEncoder(2, false, ConfiguredYamlPreferences)),
mustProcessFormatScenario(tt, NewYamlDecoder(ConfiguredYamlPreferences), NewYamlEncoder(ConfiguredYamlPreferences)),
tt.description)
}
genericScenarios := make([]interface{}, len(formattingExpressionScenarios))
Expand Down
2 changes: 1 addition & 1 deletion pkg/yqlib/goccy_yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ var goccyYamlFormatScenarios = []formatScenario{
}

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

func TestGoccyYmlFormatScenarios(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions pkg/yqlib/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ func documentDecodeNdJsonScenario(w *bufio.Writer, s formatScenario) {

writeOrPanic(w, "will output\n")

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

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

var output bytes.Buffer
printer := NewSimpleYamlPrinter(bufio.NewWriter(&output), true, false, 2, true)
printer := NewSimpleYamlPrinter(bufio.NewWriter(&output), true, 2, true)

node := decodeJSON(t, s.input)

Expand Down
12 changes: 6 additions & 6 deletions pkg/yqlib/lua_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ var luaScenarios = []formatScenario{
`,
expected: `country: Australia
cities:
- Sydney
- Melbourne
- Brisbane
- Perth
- Sydney
- Melbourne
- Brisbane
- Perth
`,
},
{
Expand Down Expand Up @@ -253,7 +253,7 @@ numbers:
func testLuaScenario(t *testing.T, s formatScenario) {
switch s.scenarioType {
case "", "decode":
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewLuaDecoder(ConfiguredLuaPreferences), NewYamlEncoder(4, false, ConfiguredYamlPreferences)), s.description)
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewLuaDecoder(ConfiguredLuaPreferences), NewYamlEncoder(ConfiguredYamlPreferences)), s.description)
case "encode":
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewLuaEncoder(ConfiguredLuaPreferences)), s.description)
case "roundtrip":
Expand Down Expand Up @@ -314,7 +314,7 @@ func documentLuaDecodeScenario(w *bufio.Writer, s formatScenario) {
writeOrPanic(w, fmt.Sprintf("```bash\nyq -oy '%v' sample.lua\n```\n", expression))
writeOrPanic(w, "will output\n")

writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", mustProcessFormatScenario(s, NewLuaDecoder(ConfiguredLuaPreferences), NewYamlEncoder(2, false, ConfiguredYamlPreferences))))
writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", mustProcessFormatScenario(s, NewLuaDecoder(ConfiguredLuaPreferences), NewYamlEncoder(ConfiguredYamlPreferences))))
}

func documentLuaEncodeScenario(w *bufio.Writer, s formatScenario) {
Expand Down
5 changes: 3 additions & 2 deletions pkg/yqlib/operator_comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,10 @@ func getCommentsOperator(_ *dataTreeNavigator, context Context, expressionNode *
log.Debugf("GetComments operator!")
var results = list.New()

yamlPrefs := NewDefaultYamlPreferences()
yamlPrefs := ConfiguredYamlPreferences.Copy()
yamlPrefs.PrintDocSeparators = false
yamlPrefs.UnwrapScalar = false
yamlPrefs.ColorsEnabled = false

for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
candidate := el.Value.(*CandidateNode)
Expand All @@ -94,7 +95,7 @@ func getCommentsOperator(_ *dataTreeNavigator, context Context, expressionNode *
var chompRegexp = regexp.MustCompile(`\n$`)
var output bytes.Buffer
var writer = bufio.NewWriter(&output)
var encoder = NewYamlEncoder(2, false, yamlPrefs)
var encoder = NewYamlEncoder(yamlPrefs)
if err := encoder.PrintLeadingContent(writer, candidate.LeadingContent); err != nil {
return Context{}, err
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/yqlib/operator_encoder_decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ func configureEncoder(format *PrinterOutputFormat, indent int) Encoder {
case TSVOutputFormat:
return NewCsvEncoder(ConfiguredTsvPreferences)
case YamlOutputFormat:
return NewYamlEncoder(indent, false, ConfiguredYamlPreferences)
var prefs = ConfiguredYamlPreferences.Copy()
prefs.Indent = indent
prefs.ColorsEnabled = false
return NewYamlEncoder(prefs)
case XMLOutputFormat:
var xmlPrefs = ConfiguredXMLPreferences.Copy()
xmlPrefs.Indent = indent
Expand Down
14 changes: 8 additions & 6 deletions pkg/yqlib/operators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,20 @@ type expressionScenario struct {

func TestMain(m *testing.M) {
logging.SetLevel(logging.ERROR, "")
ConfiguredYamlPreferences.ColorsEnabled = false
Now = func() time.Time {
return time.Date(2021, time.May, 19, 1, 2, 3, 4, time.UTC)
}
code := m.Run()
os.Exit(code)
}

func NewSimpleYamlPrinter(writer io.Writer, unwrapScalar bool, colorsEnabled bool, indent int, printDocSeparators bool) Printer {
prefs := NewDefaultYamlPreferences()
func NewSimpleYamlPrinter(writer io.Writer, unwrapScalar bool, indent int, printDocSeparators bool) Printer {
prefs := ConfiguredYamlPreferences.Copy()
prefs.PrintDocSeparators = printDocSeparators
prefs.UnwrapScalar = unwrapScalar
return NewPrinter(NewYamlEncoder(indent, colorsEnabled, prefs), NewSinglePrinterWriter(writer))
prefs.Indent = indent
return NewPrinter(NewYamlEncoder(prefs), NewSinglePrinterWriter(writer))
}

func readDocument(content string, fakefilename string, fakeFileIndex int) (*list.List, error) {
Expand Down Expand Up @@ -132,7 +134,7 @@ func testScenario(t *testing.T, s *expressionScenario) {
func resultToString(t *testing.T, n *CandidateNode) string {
var valueBuffer bytes.Buffer
log.Debugf("printing result %v", NodeToString(n))
printer := NewSimpleYamlPrinter(bufio.NewWriter(&valueBuffer), true, false, 4, true)
printer := NewSimpleYamlPrinter(bufio.NewWriter(&valueBuffer), true, 4, true)

err := printer.PrintResults(n.AsList())
if err != nil {
Expand Down Expand Up @@ -182,7 +184,7 @@ func copySnippet(source string, out *os.File) error {

func formatYaml(yaml string, filename string) string {
var output bytes.Buffer
printer := NewSimpleYamlPrinter(bufio.NewWriter(&output), true, false, 2, true)
printer := NewSimpleYamlPrinter(bufio.NewWriter(&output), true, 2, true)

node, err := getExpressionParser().ParseExpression(".. style= \"\"")
if err != nil {
Expand Down Expand Up @@ -331,7 +333,7 @@ func documentInput(w *bufio.Writer, s expressionScenario) (string, string) {
func documentOutput(t *testing.T, w *bufio.Writer, s expressionScenario, formattedDoc string, formattedDoc2 string) {
var output bytes.Buffer
var err error
printer := NewSimpleYamlPrinter(bufio.NewWriter(&output), true, false, 2, true)
printer := NewSimpleYamlPrinter(bufio.NewWriter(&output), true, 2, true)

node, err := getExpressionParser().ParseExpression(s.expression)
if err != nil {
Expand Down
Loading

0 comments on commit 7a01e21

Please sign in to comment.