mirror of
https://github.com/mikefarah/yq.git
synced 2026-09-01 14:14:52 +08:00
Can specify indent in encode ops
This commit is contained in:
@@ -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))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user