mirror of
https://github.com/mikefarah/yq.git
synced 2026-08-24 00:04:50 +08:00
Pr 2825 (#2836)
* disable hard line wrapping The formatting defaults of the used YAML library changed. Since yq has no mechanism for the user to override them, change them back to what we used before. * Clamp YAML indent and close dumper after encoding. Clamp indent to the library's 2-9 range, wrap dumper setup errors with %w, and always close the dumper after dump. Co-authored-by: Cursor <cursoragent@cursor.com> --------- Co-authored-by: Maximilian Gaß <m.gass@babiel.com> Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
co-authored by
Cursor
Maximilian Gaß
parent
ce7026e319
commit
ce3131723a
@@ -2,6 +2,7 @@ package yqlib
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
@@ -49,23 +50,37 @@ func (ye *yamlEncoder) Encode(writer io.Writer, node *CandidateNode) error {
|
||||
destination = tempBuffer
|
||||
}
|
||||
|
||||
var encoder = yaml.NewEncoder(destination)
|
||||
indent := ye.prefs.Indent
|
||||
if indent < 2 {
|
||||
indent = 2
|
||||
} else if indent > 9 {
|
||||
indent = 9
|
||||
}
|
||||
|
||||
encoder.SetIndent(ye.prefs.Indent)
|
||||
if ye.prefs.CompactSequenceIndent {
|
||||
encoder.CompactSeqIndent()
|
||||
dumper, err := yaml.NewDumper(destination,
|
||||
yaml.WithV3Defaults(),
|
||||
yaml.WithIndent(indent),
|
||||
yaml.WithCompactSeqIndent(ye.prefs.CompactSequenceIndent),
|
||||
yaml.WithLineWidth(-1),
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("configure YAML encoding: %w", err)
|
||||
}
|
||||
|
||||
target, err := node.MarshalYAML()
|
||||
|
||||
if err != nil {
|
||||
_ = dumper.Close()
|
||||
return err
|
||||
}
|
||||
|
||||
trailingContent := target.FootComment
|
||||
target.FootComment = ""
|
||||
|
||||
if err := encoder.Encode(target); err != nil {
|
||||
err = dumper.Dump(target)
|
||||
if closeErr := dumper.Close(); err == nil {
|
||||
err = closeErr
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
@@ -77,7 +77,7 @@ var loadScenarios = []expressionScenario{
|
||||
document: `{something: {file: "thing.yml"}, over: {here: [{file: "thing.yml"}]}}`,
|
||||
expression: `(.. | select(has("file"))) |= load("../../examples/" + .file)`,
|
||||
expected: []string{
|
||||
"D0, P[], (!!map)::{something: {a: apple is included, b: cool.}, over: {here: [{a: apple is included,\n b: cool.}]}}\n",
|
||||
"D0, P[], (!!map)::{something: {a: apple is included, b: cool.}, over: {here: [{a: apple is included, b: cool.}]}}\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
|
||||
@@ -83,7 +83,7 @@ var nodeWithFooter = `a: apple
|
||||
|
||||
var document = `a: &cat {name: cat}
|
||||
b: {name: dog}
|
||||
c:
|
||||
c:
|
||||
<<: *cat
|
||||
`
|
||||
|
||||
@@ -514,7 +514,7 @@ var multiplyOperatorScenarios = []expressionScenario{
|
||||
environmentVariables: map[string]string{"originalPath": ".myArray", "otherPath": ".newArray", "idPath": ".a"},
|
||||
expression: mergeExpression,
|
||||
expected: []string{
|
||||
"D0, P[], (!!map)::{myArray: [{a: apple, b: appleB2}, {a: kiwi, b: kiwiB}, {a: banana, b: bananaB, c: bananaC},\n {a: dingo, c: dingoC}], something: else}\n",
|
||||
"D0, P[], (!!map)::{myArray: [{a: apple, b: appleB2}, {a: kiwi, b: kiwiB}, {a: banana, b: bananaB, c: bananaC}, {a: dingo, c: dingoC}], something: else}\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
|
||||
@@ -98,7 +98,7 @@ var selectOperatorScenarios = []expressionScenario{
|
||||
document: `[{animal: cat, legs: {cool: true}}, {animal: fish}]`,
|
||||
expression: `(.[] | select(.legs.cool == true).canWalk) = true | (.[] | .alive.things) = "yes"`,
|
||||
expected: []string{
|
||||
"D0, P[], (!!seq)::[{animal: cat, legs: {cool: true}, canWalk: true, alive: {things: yes}}, {animal: fish,\n alive: {things: yes}}]\n",
|
||||
"D0, P[], (!!seq)::[{animal: cat, legs: {cool: true}, canWalk: true, alive: {things: yes}}, {animal: fish, alive: {things: yes}}]\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
|
||||
@@ -37,7 +37,7 @@ var sortKeysOperatorScenarios = []expressionScenario{
|
||||
document: `{bParent: {c: dog, array: [3,1,2]}, aParent: {z: donkey, x: [{c: yum, b: delish}, {b: ew, a: apple}]}}`,
|
||||
expression: `sort_keys(..)`,
|
||||
expected: []string{
|
||||
"D0, P[], (!!map)::{aParent: {x: [{b: delish, c: yum}, {a: apple, b: ew}], z: donkey}, bParent: {array: [\n 3, 1, 2], c: dog}}\n",
|
||||
"D0, P[], (!!map)::{aParent: {x: [{b: delish, c: yum}, {a: apple, b: ew}], z: donkey}, bParent: {array: [3, 1, 2], c: dog}}\n",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
@@ -106,6 +106,12 @@ var yamlFormatScenarios = []formatScenario{
|
||||
input: "[1, 2]",
|
||||
expected: "[1, 2]\n",
|
||||
},
|
||||
{
|
||||
description: "long line",
|
||||
skipDoc: true,
|
||||
input: "field: Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt",
|
||||
expected: "field: Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt\n",
|
||||
},
|
||||
}
|
||||
|
||||
var yamlParseScenarios = []expressionScenario{
|
||||
|
||||
Reference in New Issue
Block a user