From 7a01e216c47ba95196d1caa940e0c792640d8a19 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Sat, 24 Feb 2024 15:36:16 +1100 Subject: [PATCH] Refactoring Yaml encoder prefs --- cmd/utils.go | 14 ++++++++------ pkg/yqlib/all_at_once_evaluator_test.go | 2 +- pkg/yqlib/csv_test.go | 10 +++++----- pkg/yqlib/doc/usage/convert.md | 10 +++++----- pkg/yqlib/doc/usage/properties.md | 22 +++++++++++----------- pkg/yqlib/encoder_properties_test.go | 3 --- pkg/yqlib/encoder_yaml.go | 17 ++++++----------- pkg/yqlib/formatting_expressions_test.go | 4 ++-- pkg/yqlib/goccy_yaml_test.go | 2 +- pkg/yqlib/json_test.go | 6 +++--- pkg/yqlib/lua_test.go | 12 ++++++------ pkg/yqlib/operator_comments.go | 5 +++-- pkg/yqlib/operator_encoder_decoder.go | 5 ++++- pkg/yqlib/operators_test.go | 14 ++++++++------ pkg/yqlib/printer_test.go | 20 ++++++++++---------- pkg/yqlib/properties_test.go | 4 ++-- pkg/yqlib/string_evaluator_test.go | 2 +- pkg/yqlib/toml_test.go | 6 +++--- pkg/yqlib/xml_test.go | 14 ++++++++------ pkg/yqlib/yaml.go | 15 +++++++++++++++ pkg/yqlib/yaml_test.go | 2 +- 21 files changed, 103 insertions(+), 86 deletions(-) diff --git a/cmd/utils.go b/cmd/utils.go index 14fdc61d..fe34c8e4 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -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 } @@ -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 @@ -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: diff --git a/pkg/yqlib/all_at_once_evaluator_test.go b/pkg/yqlib/all_at_once_evaluator_test.go index c3e49c45..3567655d 100644 --- a/pkg/yqlib/all_at_once_evaluator_test.go +++ b/pkg/yqlib/all_at_once_evaluator_test.go @@ -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 { diff --git a/pkg/yqlib/csv_test.go b/pkg/yqlib/csv_test.go index c88cc35d..a1cbb423 100644 --- a/pkg/yqlib/csv_test.go +++ b/pkg/yqlib/csv_test.go @@ -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: @@ -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))), ) } @@ -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))), ) } diff --git a/pkg/yqlib/doc/usage/convert.md b/pkg/yqlib/doc/usage/convert.md index 83357dba..4ad54c7c 100644 --- a/pkg/yqlib/doc/usage/convert.md +++ b/pkg/yqlib/doc/usage/convert.md @@ -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 @@ -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 ``` diff --git a/pkg/yqlib/doc/usage/properties.md b/pkg/yqlib/doc/usage/properties.md index 491e014d..6eed4ef0 100644 --- a/pkg/yqlib/doc/usage/properties.md +++ b/pkg/yqlib/doc/usage/properties.md @@ -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 @@ -244,7 +244,7 @@ yq -p=props '.things |= array_to_map' sample.properties will output ```yaml things: - 10: mike + 10: mike ``` ## Roundtrip diff --git a/pkg/yqlib/encoder_properties_test.go b/pkg/yqlib/encoder_properties_test.go index cffdda97..f942c352 100644 --- a/pkg/yqlib/encoder_properties_test.go +++ b/pkg/yqlib/encoder_properties_test.go @@ -3,7 +3,6 @@ package yqlib import ( "bufio" "bytes" - "fmt" "strings" "testing" @@ -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) diff --git a/pkg/yqlib/encoder_yaml.go b/pkg/yqlib/encoder_yaml.go index f98d34c4..5cce044c 100644 --- a/pkg/yqlib/encoder_yaml.go +++ b/pkg/yqlib/encoder_yaml.go @@ -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 { @@ -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() @@ -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 diff --git a/pkg/yqlib/formatting_expressions_test.go b/pkg/yqlib/formatting_expressions_test.go index 74ed7a02..7d7bec88 100644 --- a/pkg/yqlib/formatting_expressions_test.go +++ b/pkg/yqlib/formatting_expressions_test.go @@ -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) @@ -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)) diff --git a/pkg/yqlib/goccy_yaml_test.go b/pkg/yqlib/goccy_yaml_test.go index 018a849a..b017c439 100644 --- a/pkg/yqlib/goccy_yaml_test.go +++ b/pkg/yqlib/goccy_yaml_test.go @@ -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) { diff --git a/pkg/yqlib/json_test.go b/pkg/yqlib/json_test.go index 49ce2330..b13d8bcb 100644 --- a/pkg/yqlib/json_test.go +++ b/pkg/yqlib/json_test.go @@ -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 { @@ -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": @@ -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) diff --git a/pkg/yqlib/lua_test.go b/pkg/yqlib/lua_test.go index a3387c5a..52d8ac97 100644 --- a/pkg/yqlib/lua_test.go +++ b/pkg/yqlib/lua_test.go @@ -25,10 +25,10 @@ var luaScenarios = []formatScenario{ `, expected: `country: Australia cities: - - Sydney - - Melbourne - - Brisbane - - Perth + - Sydney + - Melbourne + - Brisbane + - Perth `, }, { @@ -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": @@ -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) { diff --git a/pkg/yqlib/operator_comments.go b/pkg/yqlib/operator_comments.go index 433d25d3..85c88700 100644 --- a/pkg/yqlib/operator_comments.go +++ b/pkg/yqlib/operator_comments.go @@ -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) @@ -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 } diff --git a/pkg/yqlib/operator_encoder_decoder.go b/pkg/yqlib/operator_encoder_decoder.go index f762626f..068cd72e 100644 --- a/pkg/yqlib/operator_encoder_decoder.go +++ b/pkg/yqlib/operator_encoder_decoder.go @@ -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 diff --git a/pkg/yqlib/operators_test.go b/pkg/yqlib/operators_test.go index 7fa4182e..1224a830 100644 --- a/pkg/yqlib/operators_test.go +++ b/pkg/yqlib/operators_test.go @@ -33,6 +33,7 @@ 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) } @@ -40,11 +41,12 @@ func TestMain(m *testing.M) { 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) { @@ -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 { @@ -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 { @@ -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 { diff --git a/pkg/yqlib/printer_test.go b/pkg/yqlib/printer_test.go index 49e6ce13..f53f1ed4 100644 --- a/pkg/yqlib/printer_test.go +++ b/pkg/yqlib/printer_test.go @@ -36,7 +36,7 @@ func nodeToList(candidate *CandidateNode) *list.List { func TestPrinterMultipleDocsInSequenceOnly(t *testing.T) { var output bytes.Buffer var writer = bufio.NewWriter(&output) - printer := NewSimpleYamlPrinter(writer, true, false, 2, true) + printer := NewSimpleYamlPrinter(writer, true, 2, true) inputs, err := readDocuments(strings.NewReader(multiDocSample), "sample.yml", 0, NewYamlDecoder(ConfiguredYamlPreferences)) if err != nil { @@ -74,7 +74,7 @@ func TestPrinterMultipleDocsInSequenceOnly(t *testing.T) { func TestPrinterMultipleDocsInSequenceWithLeadingContent(t *testing.T) { var output bytes.Buffer var writer = bufio.NewWriter(&output) - printer := NewSimpleYamlPrinter(writer, true, false, 2, true) + printer := NewSimpleYamlPrinter(writer, true, 2, true) inputs, err := readDocuments(strings.NewReader(multiDocSample), "sample.yml", 0, NewYamlDecoder(ConfiguredYamlPreferences)) if err != nil { @@ -116,7 +116,7 @@ func TestPrinterMultipleDocsInSequenceWithLeadingContent(t *testing.T) { func TestPrinterMultipleFilesInSequence(t *testing.T) { var output bytes.Buffer var writer = bufio.NewWriter(&output) - printer := NewSimpleYamlPrinter(writer, true, false, 2, true) + printer := NewSimpleYamlPrinter(writer, true, 2, true) inputs, err := readDocuments(strings.NewReader(multiDocSample), "sample.yml", 0, NewYamlDecoder(ConfiguredYamlPreferences)) if err != nil { @@ -163,7 +163,7 @@ func TestPrinterMultipleFilesInSequence(t *testing.T) { func TestPrinterMultipleFilesInSequenceWithLeadingContent(t *testing.T) { var output bytes.Buffer var writer = bufio.NewWriter(&output) - printer := NewSimpleYamlPrinter(writer, true, false, 2, true) + printer := NewSimpleYamlPrinter(writer, true, 2, true) inputs, err := readDocuments(strings.NewReader(multiDocSample), "sample.yml", 0, NewYamlDecoder(ConfiguredYamlPreferences)) if err != nil { @@ -213,7 +213,7 @@ func TestPrinterMultipleFilesInSequenceWithLeadingContent(t *testing.T) { func TestPrinterMultipleDocsInSinglePrint(t *testing.T) { var output bytes.Buffer var writer = bufio.NewWriter(&output) - printer := NewSimpleYamlPrinter(writer, true, false, 2, true) + printer := NewSimpleYamlPrinter(writer, true, 2, true) inputs, err := readDocuments(strings.NewReader(multiDocSample), "sample.yml", 0, NewYamlDecoder(ConfiguredYamlPreferences)) if err != nil { @@ -232,7 +232,7 @@ func TestPrinterMultipleDocsInSinglePrint(t *testing.T) { func TestPrinterMultipleDocsInSinglePrintWithLeadingDoc(t *testing.T) { var output bytes.Buffer var writer = bufio.NewWriter(&output) - printer := NewSimpleYamlPrinter(writer, true, false, 2, true) + printer := NewSimpleYamlPrinter(writer, true, 2, true) inputs, err := readDocuments(strings.NewReader(multiDocSample), "sample.yml", 0, NewYamlDecoder(ConfiguredYamlPreferences)) if err != nil { @@ -261,7 +261,7 @@ a: coconut func TestPrinterMultipleDocsInSinglePrintWithLeadingDocTrailing(t *testing.T) { var output bytes.Buffer var writer = bufio.NewWriter(&output) - printer := NewSimpleYamlPrinter(writer, true, false, 2, true) + printer := NewSimpleYamlPrinter(writer, true, 2, true) inputs, err := readDocuments(strings.NewReader(multiDocSample), "sample.yml", 0, NewYamlDecoder(ConfiguredYamlPreferences)) if err != nil { @@ -287,7 +287,7 @@ a: coconut func TestPrinterScalarWithLeadingCont(t *testing.T) { var output bytes.Buffer var writer = bufio.NewWriter(&output) - printer := NewSimpleYamlPrinter(writer, true, false, 2, true) + printer := NewSimpleYamlPrinter(writer, true, 2, true) node, err := getExpressionParser().ParseExpression(".a") if err != nil { @@ -344,7 +344,7 @@ func TestPrinterMultipleDocsJson(t *testing.T) { func TestPrinterNulSeparator(t *testing.T) { var output bytes.Buffer var writer = bufio.NewWriter(&output) - printer := NewSimpleYamlPrinter(writer, true, false, 2, false) + printer := NewSimpleYamlPrinter(writer, true, 2, false) printer.SetNulSepOutput(true) node, err := getExpressionParser().ParseExpression(".a") if err != nil { @@ -394,7 +394,7 @@ func TestPrinterNulSeparatorWithJson(t *testing.T) { func TestPrinterRootUnwrap(t *testing.T) { var output bytes.Buffer var writer = bufio.NewWriter(&output) - printer := NewSimpleYamlPrinter(writer, true, false, 2, false) + printer := NewSimpleYamlPrinter(writer, true, 2, false) node, err := getExpressionParser().ParseExpression(".") if err != nil { panic(err) diff --git a/pkg/yqlib/properties_test.go b/pkg/yqlib/properties_test.go index facb71f6..e9673450 100644 --- a/pkg/yqlib/properties_test.go +++ b/pkg/yqlib/properties_test.go @@ -379,7 +379,7 @@ func documentDecodePropertyScenario(w *bufio.Writer, s formatScenario) { writeOrPanic(w, "will output\n") - writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", mustProcessFormatScenario(s, NewPropertiesDecoder(), NewYamlEncoder(s.indent, false, ConfiguredYamlPreferences)))) + writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", mustProcessFormatScenario(s, NewPropertiesDecoder(), NewYamlEncoder(ConfiguredYamlPreferences)))) } func documentRoundTripPropertyScenario(w *bufio.Writer, s formatScenario) { @@ -433,7 +433,7 @@ func TestPropertyScenarios(t *testing.T) { case "": test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewPropertiesEncoder(ConfiguredPropertiesPreferences)), s.description) case "decode": - test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewPropertiesDecoder(), NewYamlEncoder(2, false, ConfiguredYamlPreferences)), s.description) + test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewPropertiesDecoder(), NewYamlEncoder(ConfiguredYamlPreferences)), s.description) case "encode-wrapped": prefs := ConfiguredPropertiesPreferences.Copy() prefs.UnwrapScalar = false diff --git a/pkg/yqlib/string_evaluator_test.go b/pkg/yqlib/string_evaluator_test.go index e8e7ddf5..34906040 100644 --- a/pkg/yqlib/string_evaluator_test.go +++ b/pkg/yqlib/string_evaluator_test.go @@ -18,7 +18,7 @@ func TestStringEvaluator_Evaluate_Nominal(t *testing.T) { `---` + "\n" + ` - name: jq` + "\n" + ` description: Command-line JSON processor` + "\n" - encoder := NewYamlEncoder(2, true, ConfiguredYamlPreferences) + encoder := NewYamlEncoder(ConfiguredYamlPreferences) decoder := NewYamlDecoder(ConfiguredYamlPreferences) result, err := NewStringEvaluator().Evaluate(expression, input, encoder, decoder) diff --git a/pkg/yqlib/toml_test.go b/pkg/yqlib/toml_test.go index 0dda9773..60eb4c83 100644 --- a/pkg/yqlib/toml_test.go +++ b/pkg/yqlib/toml_test.go @@ -243,9 +243,9 @@ var tomlScenarios = []formatScenario{ func testTomlScenario(t *testing.T, s formatScenario) { switch s.scenarioType { case "", "decode": - test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewTomlDecoder(), NewYamlEncoder(2, false, ConfiguredYamlPreferences)), s.description) + test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewTomlDecoder(), NewYamlEncoder(ConfiguredYamlPreferences)), s.description) case "decode-error": - result, err := processFormatScenario(s, NewTomlDecoder(), NewYamlEncoder(2, false, ConfiguredYamlPreferences)) + result, err := processFormatScenario(s, NewTomlDecoder(), NewYamlEncoder(ConfiguredYamlPreferences)) if err == nil { t.Errorf("Expected error '%v' but it worked: %v", s.expectedError, result) } else { @@ -275,7 +275,7 @@ func documentTomlDecodeScenario(w *bufio.Writer, s formatScenario) { writeOrPanic(w, fmt.Sprintf("```bash\nyq -oy '%v' sample.toml\n```\n", expression)) writeOrPanic(w, "will output\n") - writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", mustProcessFormatScenario(s, NewTomlDecoder(), NewYamlEncoder(2, false, ConfiguredYamlPreferences)))) + writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", mustProcessFormatScenario(s, NewTomlDecoder(), NewYamlEncoder(ConfiguredYamlPreferences)))) } func documentTomlRoundtripScenario(w *bufio.Writer, s formatScenario) { diff --git a/pkg/yqlib/xml_test.go b/pkg/yqlib/xml_test.go index 40e8e384..08e1f0ae 100644 --- a/pkg/yqlib/xml_test.go +++ b/pkg/yqlib/xml_test.go @@ -617,7 +617,9 @@ var xmlScenarios = []formatScenario{ func testXMLScenario(t *testing.T, s formatScenario) { switch s.scenarioType { case "", "decode": - test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewXMLDecoder(ConfiguredXMLPreferences), NewYamlEncoder(4, false, ConfiguredYamlPreferences)), s.description) + yamlPrefs := ConfiguredYamlPreferences.Copy() + yamlPrefs.Indent = 4 + test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewXMLDecoder(ConfiguredXMLPreferences), NewYamlEncoder(yamlPrefs)), s.description) case "encode": test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewXMLEncoder(ConfiguredXMLPreferences)), s.description) case "roundtrip": @@ -625,21 +627,21 @@ func testXMLScenario(t *testing.T, s formatScenario) { case "decode-keep-ns": prefs := NewDefaultXmlPreferences() prefs.KeepNamespace = true - test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewXMLDecoder(prefs), NewYamlEncoder(2, false, ConfiguredYamlPreferences)), s.description) + test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewXMLDecoder(prefs), NewYamlEncoder(ConfiguredYamlPreferences)), s.description) case "decode-raw-token": prefs := NewDefaultXmlPreferences() prefs.UseRawToken = true - test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewXMLDecoder(prefs), NewYamlEncoder(2, false, ConfiguredYamlPreferences)), s.description) + test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewXMLDecoder(prefs), NewYamlEncoder(ConfiguredYamlPreferences)), s.description) case "decode-raw-token-off": prefs := NewDefaultXmlPreferences() prefs.UseRawToken = false - test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewXMLDecoder(prefs), NewYamlEncoder(2, false, ConfiguredYamlPreferences)), s.description) + test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewXMLDecoder(prefs), NewYamlEncoder(ConfiguredYamlPreferences)), s.description) case "roundtrip-skip-directives": prefs := NewDefaultXmlPreferences() prefs.SkipDirectives = true test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewXMLDecoder(prefs), NewXMLEncoder(prefs)), s.description) case "decode-error": - result, err := processFormatScenario(s, NewXMLDecoder(NewDefaultXmlPreferences()), NewYamlEncoder(2, false, ConfiguredYamlPreferences)) + result, err := processFormatScenario(s, NewXMLDecoder(NewDefaultXmlPreferences()), NewYamlEncoder(ConfiguredYamlPreferences)) if err == nil { t.Errorf("Expected error '%v' but it worked: %v", s.expectedError, result) } else { @@ -702,7 +704,7 @@ func documentXMLDecodeScenario(w *bufio.Writer, s formatScenario) { writeOrPanic(w, fmt.Sprintf("```bash\nyq -oy '%v' sample.xml\n```\n", expression)) writeOrPanic(w, "will output\n") - writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", mustProcessFormatScenario(s, NewXMLDecoder(ConfiguredXMLPreferences), NewYamlEncoder(2, false, ConfiguredYamlPreferences)))) + writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", mustProcessFormatScenario(s, NewXMLDecoder(ConfiguredXMLPreferences), NewYamlEncoder(ConfiguredYamlPreferences)))) } func documentXMLDecodeKeepNsScenario(w *bufio.Writer, s formatScenario) { diff --git a/pkg/yqlib/yaml.go b/pkg/yqlib/yaml.go index d7c8863a..71a2c3e6 100644 --- a/pkg/yqlib/yaml.go +++ b/pkg/yqlib/yaml.go @@ -1,6 +1,8 @@ package yqlib type YamlPreferences struct { + Indent int + ColorsEnabled bool LeadingContentPreProcessing bool PrintDocSeparators bool UnwrapScalar bool @@ -9,6 +11,8 @@ type YamlPreferences struct { func NewDefaultYamlPreferences() YamlPreferences { return YamlPreferences{ + Indent: 2, + ColorsEnabled: true, LeadingContentPreProcessing: true, PrintDocSeparators: true, UnwrapScalar: true, @@ -16,4 +20,15 @@ func NewDefaultYamlPreferences() YamlPreferences { } } +func (p *YamlPreferences) Copy() YamlPreferences { + return YamlPreferences{ + Indent: p.Indent, + ColorsEnabled: p.ColorsEnabled, + LeadingContentPreProcessing: p.LeadingContentPreProcessing, + PrintDocSeparators: p.PrintDocSeparators, + UnwrapScalar: p.UnwrapScalar, + EvaluateTogether: p.EvaluateTogether, + } +} + var ConfiguredYamlPreferences = NewDefaultYamlPreferences() diff --git a/pkg/yqlib/yaml_test.go b/pkg/yqlib/yaml_test.go index f276bea1..acb978a0 100644 --- a/pkg/yqlib/yaml_test.go +++ b/pkg/yqlib/yaml_test.go @@ -112,7 +112,7 @@ var yamlParseScenarios = []expressionScenario{ } func testYamlScenario(t *testing.T, s formatScenario) { - test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewYamlDecoder(NewDefaultYamlPreferences()), NewYamlEncoder(2, false, ConfiguredYamlPreferences)), s.description) + test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewYamlEncoder(ConfiguredYamlPreferences)), s.description) } func TestYamlParseScenarios(t *testing.T) {