Can specify indent in encode ops

This commit is contained in:
Mike Farah
2021-10-24 11:35:40 +11:00
parent 587af7f722
commit 91717b3c5d
5 changed files with 115 additions and 20 deletions
+8 -1
View File
@@ -12,13 +12,15 @@ import (
func yamlToString(candidate *CandidateNode, prefs encoderPreferences) (string, error) {
var output bytes.Buffer
printer := NewPrinter(bufio.NewWriter(&output), prefs.format, true, false, 2, true)
log.Debug("printing with indent: %v", prefs.indent)
printer := NewPrinter(bufio.NewWriter(&output), prefs.format, true, false, prefs.indent, true)
err := printer.PrintResults(candidate.AsList())
return output.String(), err
}
type encoderPreferences struct {
format PrinterOutputFormat
indent int
}
/* encodes object as yaml string */
@@ -52,6 +54,11 @@ func encodeOperator(d *dataTreeNavigator, context Context, expressionNode *Expre
}
}
// dont print a new line when printing json on a single line.
if preferences.format == JsonOutputFormat && preferences.indent == 0 {
stringValue = chomper.ReplaceAllString(stringValue, "")
}
stringContentNode := &yaml.Node{Kind: yaml.ScalarNode, Tag: "!!str", Value: stringValue}
results.PushBack(candidate.CreateChild(nil, stringContentNode))
}