Refactoring Yaml encoder prefs

This commit is contained in:
Mike Farah 2024-02-24 15:36:16 +11:00
parent f44d47a204
commit 7a01e216c4
21 changed files with 103 additions and 86 deletions

View File

@ -109,11 +109,6 @@ func initCommand(cmd *cobra.Command, args []string) (string, []string, error) {
unwrapScalar = unwrapScalarFlag.IsSet() unwrapScalar = unwrapScalarFlag.IsSet()
} }
//copy preference form global setting
yqlib.ConfiguredYamlPreferences.UnwrapScalar = unwrapScalar
yqlib.ConfiguredYamlPreferences.PrintDocSeparators = !noDocSeparators
return expression, args, nil return expression, args, nil
} }
@ -184,8 +179,15 @@ func configureEncoder() (yqlib.Encoder, error) {
func createEncoder(format *yqlib.PrinterOutputFormat) (yqlib.Encoder, error) { func createEncoder(format *yqlib.PrinterOutputFormat) (yqlib.Encoder, error) {
yqlib.ConfiguredXMLPreferences.Indent = indent yqlib.ConfiguredXMLPreferences.Indent = indent
yqlib.ConfiguredYamlPreferences.Indent = indent
yqlib.ConfiguredYamlPreferences.UnwrapScalar = unwrapScalar
yqlib.ConfiguredPropertiesPreferences.UnwrapScalar = unwrapScalar yqlib.ConfiguredPropertiesPreferences.UnwrapScalar = unwrapScalar
yqlib.ConfiguredYamlPreferences.ColorsEnabled = colorsEnabled
yqlib.ConfiguredYamlPreferences.PrintDocSeparators = !noDocSeparators
switch format { switch format {
case yqlib.JSONOutputFormat: case yqlib.JSONOutputFormat:
return yqlib.NewJSONEncoder(indent, colorsEnabled, unwrapScalar), nil return yqlib.NewJSONEncoder(indent, colorsEnabled, unwrapScalar), nil
@ -196,7 +198,7 @@ func createEncoder(format *yqlib.PrinterOutputFormat) (yqlib.Encoder, error) {
case yqlib.TSVOutputFormat: case yqlib.TSVOutputFormat:
return yqlib.NewCsvEncoder(yqlib.ConfiguredTsvPreferences), nil return yqlib.NewCsvEncoder(yqlib.ConfiguredTsvPreferences), nil
case yqlib.YamlOutputFormat: case yqlib.YamlOutputFormat:
return yqlib.NewYamlEncoder(indent, colorsEnabled, yqlib.ConfiguredYamlPreferences), nil return yqlib.NewYamlEncoder(yqlib.ConfiguredYamlPreferences), nil
case yqlib.XMLOutputFormat: case yqlib.XMLOutputFormat:
return yqlib.NewXMLEncoder(yqlib.ConfiguredXMLPreferences), nil return yqlib.NewXMLEncoder(yqlib.ConfiguredXMLPreferences), nil
case yqlib.TomlOutputFormat: case yqlib.TomlOutputFormat:

View File

@ -36,7 +36,7 @@ func TestAllAtOnceEvaluateNodes(t *testing.T) {
var evaluator = NewAllAtOnceEvaluator() var evaluator = NewAllAtOnceEvaluator()
// logging.SetLevel(logging.DEBUG, "") // logging.SetLevel(logging.DEBUG, "")
for _, tt := range evaluateNodesScenario { for _, tt := range evaluateNodesScenario {
decoder := NewYamlDecoder(NewDefaultYamlPreferences()) decoder := NewYamlDecoder(ConfiguredYamlPreferences)
reader := bufio.NewReader(strings.NewReader(tt.document)) reader := bufio.NewReader(strings.NewReader(tt.document))
err := decoder.Init(reader) err := decoder.Init(reader)
if err != nil { if err != nil {

View File

@ -210,11 +210,11 @@ func testCSVScenario(t *testing.T, s formatScenario) {
case "encode-tsv": case "encode-tsv":
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewCsvEncoder(ConfiguredTsvPreferences)), s.description) test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewCsvEncoder(ConfiguredTsvPreferences)), s.description)
case "decode-csv": 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": 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": 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": case "roundtrip-csv":
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewCSVObjectDecoder(ConfiguredCsvPreferences), NewCsvEncoder(ConfiguredCsvPreferences)), s.description) test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewCSVObjectDecoder(ConfiguredCsvPreferences), NewCsvEncoder(ConfiguredCsvPreferences)), s.description)
default: default:
@ -243,7 +243,7 @@ func documentCSVDecodeObjectScenario(w *bufio.Writer, s formatScenario, formatTy
} }
writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", 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", 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))),
) )
} }

View File

@ -3,7 +3,6 @@ package yqlib
import ( import (
"bufio" "bufio"
"bytes" "bytes"
"fmt"
"strings" "strings"
"testing" "testing"
@ -81,8 +80,6 @@ func doTest(t *testing.T, sampleYaml string, props testProperties, testUnwrapped
} }
for _, unwrap := range wraps { for _, unwrap := range wraps {
fmt.Println(props)
fmt.Println(unwrap)
for _, sep := range []string{" = ", ";", "=", " "} { for _, sep := range []string{" = ", ";", "=", " "} {
var actualProps = yamlToProps(sampleYaml, unwrap, sep) var actualProps = yamlToProps(sampleYaml, unwrap, sep)
test.AssertResult(t, props.String(unwrap, sep), actualProps) test.AssertResult(t, props.String(unwrap, sep), actualProps)

View File

@ -12,16 +12,11 @@ import (
) )
type yamlEncoder struct { type yamlEncoder struct {
indent int
colorise bool
prefs YamlPreferences prefs YamlPreferences
} }
func NewYamlEncoder(indent int, colorise bool, prefs YamlPreferences) Encoder { func NewYamlEncoder(prefs YamlPreferences) Encoder {
if indent < 0 { return &yamlEncoder{prefs}
indent = 0
}
return &yamlEncoder{indent, colorise, prefs}
} }
func (ye *yamlEncoder) CanHandleAliases() bool { func (ye *yamlEncoder) CanHandleAliases() bool {
@ -90,13 +85,13 @@ func (ye *yamlEncoder) Encode(writer io.Writer, node *CandidateNode) error {
destination := writer destination := writer
tempBuffer := bytes.NewBuffer(nil) tempBuffer := bytes.NewBuffer(nil)
if ye.colorise { if ye.prefs.ColorsEnabled {
destination = tempBuffer destination = tempBuffer
} }
var encoder = yaml.NewEncoder(destination) var encoder = yaml.NewEncoder(destination)
encoder.SetIndent(ye.indent) encoder.SetIndent(ye.prefs.Indent)
target, err := node.MarshalYAML() target, err := node.MarshalYAML()
@ -115,7 +110,7 @@ func (ye *yamlEncoder) Encode(writer io.Writer, node *CandidateNode) error {
return err return err
} }
if ye.colorise { if ye.prefs.ColorsEnabled {
return colorizeAndPrint(tempBuffer.Bytes(), writer) return colorizeAndPrint(tempBuffer.Bytes(), writer)
} }
return nil return nil

View File

@ -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, "```bash\nyq --from-file update.yq sample.yml\n```\n")
} }
writeOrPanic(w, "will output\n") writeOrPanic(w, "will output\n")
encoder := NewYamlEncoder(2, false, ConfiguredYamlPreferences) encoder := NewYamlEncoder(ConfiguredYamlPreferences)
if s.scenarioType == "shebang-json" { if s.scenarioType == "shebang-json" {
encoder = NewJSONEncoder(2, false, false) encoder = NewJSONEncoder(2, false, false)
@ -80,7 +80,7 @@ func documentExpressionScenario(_ *testing.T, w *bufio.Writer, i interface{}) {
func TestExpressionCommentScenarios(t *testing.T) { func TestExpressionCommentScenarios(t *testing.T) {
for _, tt := range formattingExpressionScenarios { for _, tt := range formattingExpressionScenarios {
test.AssertResultComplexWithContext(t, tt.expected, test.AssertResultComplexWithContext(t, tt.expected,
mustProcessFormatScenario(tt, NewYamlDecoder(ConfiguredYamlPreferences), NewYamlEncoder(2, false, ConfiguredYamlPreferences)), mustProcessFormatScenario(tt, NewYamlDecoder(ConfiguredYamlPreferences), NewYamlEncoder(ConfiguredYamlPreferences)),
tt.description) tt.description)
} }
genericScenarios := make([]interface{}, len(formattingExpressionScenarios)) genericScenarios := make([]interface{}, len(formattingExpressionScenarios))

View File

@ -160,7 +160,7 @@ var goccyYamlFormatScenarios = []formatScenario{
} }
func testGoccyYamlScenario(t *testing.T, s 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) { func TestGoccyYmlFormatScenarios(t *testing.T) {

View File

@ -348,7 +348,7 @@ func documentDecodeNdJsonScenario(w *bufio.Writer, s formatScenario) {
writeOrPanic(w, "will output\n") 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 { func decodeJSON(t *testing.T, jsonString string) *CandidateNode {
@ -383,7 +383,7 @@ func testJSONScenario(t *testing.T, s formatScenario) {
case "decode": case "decode":
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewJSONDecoder(), NewJSONEncoder(s.indent, false, false)), s.description) test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewJSONDecoder(), NewJSONEncoder(s.indent, false, false)), s.description)
case "decode-ndjson": 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": case "roundtrip-ndjson":
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewJSONDecoder(), NewJSONEncoder(0, false, false)), s.description) test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewJSONDecoder(), NewJSONEncoder(0, false, false)), s.description)
case "roundtrip-multi": case "roundtrip-multi":
@ -416,7 +416,7 @@ func documentJSONDecodeScenario(t *testing.T, w *bufio.Writer, s formatScenario)
writeOrPanic(w, "will output\n") writeOrPanic(w, "will output\n")
var output bytes.Buffer 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) node := decodeJSON(t, s.input)

View File

@ -253,7 +253,7 @@ numbers:
func testLuaScenario(t *testing.T, s formatScenario) { func testLuaScenario(t *testing.T, s formatScenario) {
switch s.scenarioType { switch s.scenarioType {
case "", "decode": 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": case "encode":
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewLuaEncoder(ConfiguredLuaPreferences)), s.description) test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewLuaEncoder(ConfiguredLuaPreferences)), s.description)
case "roundtrip": 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, fmt.Sprintf("```bash\nyq -oy '%v' sample.lua\n```\n", expression))
writeOrPanic(w, "will output\n") 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) { func documentLuaEncodeScenario(w *bufio.Writer, s formatScenario) {

View File

@ -80,9 +80,10 @@ func getCommentsOperator(_ *dataTreeNavigator, context Context, expressionNode *
log.Debugf("GetComments operator!") log.Debugf("GetComments operator!")
var results = list.New() var results = list.New()
yamlPrefs := NewDefaultYamlPreferences() yamlPrefs := ConfiguredYamlPreferences.Copy()
yamlPrefs.PrintDocSeparators = false yamlPrefs.PrintDocSeparators = false
yamlPrefs.UnwrapScalar = false yamlPrefs.UnwrapScalar = false
yamlPrefs.ColorsEnabled = false
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() { for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
candidate := el.Value.(*CandidateNode) candidate := el.Value.(*CandidateNode)
@ -94,7 +95,7 @@ func getCommentsOperator(_ *dataTreeNavigator, context Context, expressionNode *
var chompRegexp = regexp.MustCompile(`\n$`) var chompRegexp = regexp.MustCompile(`\n$`)
var output bytes.Buffer var output bytes.Buffer
var writer = bufio.NewWriter(&output) var writer = bufio.NewWriter(&output)
var encoder = NewYamlEncoder(2, false, yamlPrefs) var encoder = NewYamlEncoder(yamlPrefs)
if err := encoder.PrintLeadingContent(writer, candidate.LeadingContent); err != nil { if err := encoder.PrintLeadingContent(writer, candidate.LeadingContent); err != nil {
return Context{}, err return Context{}, err
} }

View File

@ -21,7 +21,10 @@ func configureEncoder(format *PrinterOutputFormat, indent int) Encoder {
case TSVOutputFormat: case TSVOutputFormat:
return NewCsvEncoder(ConfiguredTsvPreferences) return NewCsvEncoder(ConfiguredTsvPreferences)
case YamlOutputFormat: case YamlOutputFormat:
return NewYamlEncoder(indent, false, ConfiguredYamlPreferences) var prefs = ConfiguredYamlPreferences.Copy()
prefs.Indent = indent
prefs.ColorsEnabled = false
return NewYamlEncoder(prefs)
case XMLOutputFormat: case XMLOutputFormat:
var xmlPrefs = ConfiguredXMLPreferences.Copy() var xmlPrefs = ConfiguredXMLPreferences.Copy()
xmlPrefs.Indent = indent xmlPrefs.Indent = indent

View File

@ -33,6 +33,7 @@ type expressionScenario struct {
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
logging.SetLevel(logging.ERROR, "") logging.SetLevel(logging.ERROR, "")
ConfiguredYamlPreferences.ColorsEnabled = false
Now = func() time.Time { Now = func() time.Time {
return time.Date(2021, time.May, 19, 1, 2, 3, 4, time.UTC) 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) os.Exit(code)
} }
func NewSimpleYamlPrinter(writer io.Writer, unwrapScalar bool, colorsEnabled bool, indent int, printDocSeparators bool) Printer { func NewSimpleYamlPrinter(writer io.Writer, unwrapScalar bool, indent int, printDocSeparators bool) Printer {
prefs := NewDefaultYamlPreferences() prefs := ConfiguredYamlPreferences.Copy()
prefs.PrintDocSeparators = printDocSeparators prefs.PrintDocSeparators = printDocSeparators
prefs.UnwrapScalar = unwrapScalar 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) { 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 { func resultToString(t *testing.T, n *CandidateNode) string {
var valueBuffer bytes.Buffer var valueBuffer bytes.Buffer
log.Debugf("printing result %v", NodeToString(n)) 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()) err := printer.PrintResults(n.AsList())
if err != nil { if err != nil {
@ -182,7 +184,7 @@ func copySnippet(source string, out *os.File) error {
func formatYaml(yaml string, filename string) string { func formatYaml(yaml string, filename string) string {
var output bytes.Buffer 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= \"\"") node, err := getExpressionParser().ParseExpression(".. style= \"\"")
if err != nil { 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) { func documentOutput(t *testing.T, w *bufio.Writer, s expressionScenario, formattedDoc string, formattedDoc2 string) {
var output bytes.Buffer var output bytes.Buffer
var err error 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) node, err := getExpressionParser().ParseExpression(s.expression)
if err != nil { if err != nil {

View File

@ -36,7 +36,7 @@ func nodeToList(candidate *CandidateNode) *list.List {
func TestPrinterMultipleDocsInSequenceOnly(t *testing.T) { func TestPrinterMultipleDocsInSequenceOnly(t *testing.T) {
var output bytes.Buffer var output bytes.Buffer
var writer = bufio.NewWriter(&output) 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)) inputs, err := readDocuments(strings.NewReader(multiDocSample), "sample.yml", 0, NewYamlDecoder(ConfiguredYamlPreferences))
if err != nil { if err != nil {
@ -74,7 +74,7 @@ func TestPrinterMultipleDocsInSequenceOnly(t *testing.T) {
func TestPrinterMultipleDocsInSequenceWithLeadingContent(t *testing.T) { func TestPrinterMultipleDocsInSequenceWithLeadingContent(t *testing.T) {
var output bytes.Buffer var output bytes.Buffer
var writer = bufio.NewWriter(&output) 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)) inputs, err := readDocuments(strings.NewReader(multiDocSample), "sample.yml", 0, NewYamlDecoder(ConfiguredYamlPreferences))
if err != nil { if err != nil {
@ -116,7 +116,7 @@ func TestPrinterMultipleDocsInSequenceWithLeadingContent(t *testing.T) {
func TestPrinterMultipleFilesInSequence(t *testing.T) { func TestPrinterMultipleFilesInSequence(t *testing.T) {
var output bytes.Buffer var output bytes.Buffer
var writer = bufio.NewWriter(&output) 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)) inputs, err := readDocuments(strings.NewReader(multiDocSample), "sample.yml", 0, NewYamlDecoder(ConfiguredYamlPreferences))
if err != nil { if err != nil {
@ -163,7 +163,7 @@ func TestPrinterMultipleFilesInSequence(t *testing.T) {
func TestPrinterMultipleFilesInSequenceWithLeadingContent(t *testing.T) { func TestPrinterMultipleFilesInSequenceWithLeadingContent(t *testing.T) {
var output bytes.Buffer var output bytes.Buffer
var writer = bufio.NewWriter(&output) 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)) inputs, err := readDocuments(strings.NewReader(multiDocSample), "sample.yml", 0, NewYamlDecoder(ConfiguredYamlPreferences))
if err != nil { if err != nil {
@ -213,7 +213,7 @@ func TestPrinterMultipleFilesInSequenceWithLeadingContent(t *testing.T) {
func TestPrinterMultipleDocsInSinglePrint(t *testing.T) { func TestPrinterMultipleDocsInSinglePrint(t *testing.T) {
var output bytes.Buffer var output bytes.Buffer
var writer = bufio.NewWriter(&output) 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)) inputs, err := readDocuments(strings.NewReader(multiDocSample), "sample.yml", 0, NewYamlDecoder(ConfiguredYamlPreferences))
if err != nil { if err != nil {
@ -232,7 +232,7 @@ func TestPrinterMultipleDocsInSinglePrint(t *testing.T) {
func TestPrinterMultipleDocsInSinglePrintWithLeadingDoc(t *testing.T) { func TestPrinterMultipleDocsInSinglePrintWithLeadingDoc(t *testing.T) {
var output bytes.Buffer var output bytes.Buffer
var writer = bufio.NewWriter(&output) 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)) inputs, err := readDocuments(strings.NewReader(multiDocSample), "sample.yml", 0, NewYamlDecoder(ConfiguredYamlPreferences))
if err != nil { if err != nil {
@ -261,7 +261,7 @@ a: coconut
func TestPrinterMultipleDocsInSinglePrintWithLeadingDocTrailing(t *testing.T) { func TestPrinterMultipleDocsInSinglePrintWithLeadingDocTrailing(t *testing.T) {
var output bytes.Buffer var output bytes.Buffer
var writer = bufio.NewWriter(&output) 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)) inputs, err := readDocuments(strings.NewReader(multiDocSample), "sample.yml", 0, NewYamlDecoder(ConfiguredYamlPreferences))
if err != nil { if err != nil {
@ -287,7 +287,7 @@ a: coconut
func TestPrinterScalarWithLeadingCont(t *testing.T) { func TestPrinterScalarWithLeadingCont(t *testing.T) {
var output bytes.Buffer var output bytes.Buffer
var writer = bufio.NewWriter(&output) var writer = bufio.NewWriter(&output)
printer := NewSimpleYamlPrinter(writer, true, false, 2, true) printer := NewSimpleYamlPrinter(writer, true, 2, true)
node, err := getExpressionParser().ParseExpression(".a") node, err := getExpressionParser().ParseExpression(".a")
if err != nil { if err != nil {
@ -344,7 +344,7 @@ func TestPrinterMultipleDocsJson(t *testing.T) {
func TestPrinterNulSeparator(t *testing.T) { func TestPrinterNulSeparator(t *testing.T) {
var output bytes.Buffer var output bytes.Buffer
var writer = bufio.NewWriter(&output) var writer = bufio.NewWriter(&output)
printer := NewSimpleYamlPrinter(writer, true, false, 2, false) printer := NewSimpleYamlPrinter(writer, true, 2, false)
printer.SetNulSepOutput(true) printer.SetNulSepOutput(true)
node, err := getExpressionParser().ParseExpression(".a") node, err := getExpressionParser().ParseExpression(".a")
if err != nil { if err != nil {
@ -394,7 +394,7 @@ func TestPrinterNulSeparatorWithJson(t *testing.T) {
func TestPrinterRootUnwrap(t *testing.T) { func TestPrinterRootUnwrap(t *testing.T) {
var output bytes.Buffer var output bytes.Buffer
var writer = bufio.NewWriter(&output) var writer = bufio.NewWriter(&output)
printer := NewSimpleYamlPrinter(writer, true, false, 2, false) printer := NewSimpleYamlPrinter(writer, true, 2, false)
node, err := getExpressionParser().ParseExpression(".") node, err := getExpressionParser().ParseExpression(".")
if err != nil { if err != nil {
panic(err) panic(err)

View File

@ -379,7 +379,7 @@ func documentDecodePropertyScenario(w *bufio.Writer, s formatScenario) {
writeOrPanic(w, "will output\n") 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) { func documentRoundTripPropertyScenario(w *bufio.Writer, s formatScenario) {
@ -433,7 +433,7 @@ func TestPropertyScenarios(t *testing.T) {
case "": case "":
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewPropertiesEncoder(ConfiguredPropertiesPreferences)), s.description) test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewPropertiesEncoder(ConfiguredPropertiesPreferences)), s.description)
case "decode": 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": case "encode-wrapped":
prefs := ConfiguredPropertiesPreferences.Copy() prefs := ConfiguredPropertiesPreferences.Copy()
prefs.UnwrapScalar = false prefs.UnwrapScalar = false

View File

@ -18,7 +18,7 @@ func TestStringEvaluator_Evaluate_Nominal(t *testing.T) {
`---` + "\n" + `---` + "\n" +
` - name: jq` + "\n" + ` - name: jq` + "\n" +
` description: Command-line JSON processor` + "\n" ` description: Command-line JSON processor` + "\n"
encoder := NewYamlEncoder(2, true, ConfiguredYamlPreferences) encoder := NewYamlEncoder(ConfiguredYamlPreferences)
decoder := NewYamlDecoder(ConfiguredYamlPreferences) decoder := NewYamlDecoder(ConfiguredYamlPreferences)
result, err := NewStringEvaluator().Evaluate(expression, input, encoder, decoder) result, err := NewStringEvaluator().Evaluate(expression, input, encoder, decoder)

View File

@ -243,9 +243,9 @@ var tomlScenarios = []formatScenario{
func testTomlScenario(t *testing.T, s formatScenario) { func testTomlScenario(t *testing.T, s formatScenario) {
switch s.scenarioType { switch s.scenarioType {
case "", "decode": 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": case "decode-error":
result, err := processFormatScenario(s, NewTomlDecoder(), NewYamlEncoder(2, false, ConfiguredYamlPreferences)) result, err := processFormatScenario(s, NewTomlDecoder(), NewYamlEncoder(ConfiguredYamlPreferences))
if err == nil { if err == nil {
t.Errorf("Expected error '%v' but it worked: %v", s.expectedError, result) t.Errorf("Expected error '%v' but it worked: %v", s.expectedError, result)
} else { } 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, fmt.Sprintf("```bash\nyq -oy '%v' sample.toml\n```\n", expression))
writeOrPanic(w, "will output\n") 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) { func documentTomlRoundtripScenario(w *bufio.Writer, s formatScenario) {

View File

@ -617,7 +617,9 @@ var xmlScenarios = []formatScenario{
func testXMLScenario(t *testing.T, s formatScenario) { func testXMLScenario(t *testing.T, s formatScenario) {
switch s.scenarioType { switch s.scenarioType {
case "", "decode": 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": case "encode":
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewXMLEncoder(ConfiguredXMLPreferences)), s.description) test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewXMLEncoder(ConfiguredXMLPreferences)), s.description)
case "roundtrip": case "roundtrip":
@ -625,21 +627,21 @@ func testXMLScenario(t *testing.T, s formatScenario) {
case "decode-keep-ns": case "decode-keep-ns":
prefs := NewDefaultXmlPreferences() prefs := NewDefaultXmlPreferences()
prefs.KeepNamespace = true 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": case "decode-raw-token":
prefs := NewDefaultXmlPreferences() prefs := NewDefaultXmlPreferences()
prefs.UseRawToken = true 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": case "decode-raw-token-off":
prefs := NewDefaultXmlPreferences() prefs := NewDefaultXmlPreferences()
prefs.UseRawToken = false 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": case "roundtrip-skip-directives":
prefs := NewDefaultXmlPreferences() prefs := NewDefaultXmlPreferences()
prefs.SkipDirectives = true prefs.SkipDirectives = true
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewXMLDecoder(prefs), NewXMLEncoder(prefs)), s.description) test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewXMLDecoder(prefs), NewXMLEncoder(prefs)), s.description)
case "decode-error": case "decode-error":
result, err := processFormatScenario(s, NewXMLDecoder(NewDefaultXmlPreferences()), NewYamlEncoder(2, false, ConfiguredYamlPreferences)) result, err := processFormatScenario(s, NewXMLDecoder(NewDefaultXmlPreferences()), NewYamlEncoder(ConfiguredYamlPreferences))
if err == nil { if err == nil {
t.Errorf("Expected error '%v' but it worked: %v", s.expectedError, result) t.Errorf("Expected error '%v' but it worked: %v", s.expectedError, result)
} else { } 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, fmt.Sprintf("```bash\nyq -oy '%v' sample.xml\n```\n", expression))
writeOrPanic(w, "will output\n") 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) { func documentXMLDecodeKeepNsScenario(w *bufio.Writer, s formatScenario) {

View File

@ -1,6 +1,8 @@
package yqlib package yqlib
type YamlPreferences struct { type YamlPreferences struct {
Indent int
ColorsEnabled bool
LeadingContentPreProcessing bool LeadingContentPreProcessing bool
PrintDocSeparators bool PrintDocSeparators bool
UnwrapScalar bool UnwrapScalar bool
@ -9,6 +11,8 @@ type YamlPreferences struct {
func NewDefaultYamlPreferences() YamlPreferences { func NewDefaultYamlPreferences() YamlPreferences {
return YamlPreferences{ return YamlPreferences{
Indent: 2,
ColorsEnabled: true,
LeadingContentPreProcessing: true, LeadingContentPreProcessing: true,
PrintDocSeparators: true, PrintDocSeparators: true,
UnwrapScalar: 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() var ConfiguredYamlPreferences = NewDefaultYamlPreferences()

View File

@ -112,7 +112,7 @@ var yamlParseScenarios = []expressionScenario{
} }
func testYamlScenario(t *testing.T, s formatScenario) { 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) { func TestYamlParseScenarios(t *testing.T) {